pandas.py 908 B

123456789101112131415161718192021222324252627282930
  1. # Copyright 2016 by MPI-SWS and Data-Ken Research.
  2. # Licensed under the Apache 2.0 License.
  3. """
  4. Pandas (http://pandas.pydata.org) is a data analysis library.
  5. This module contains adapters for converting between thingflow
  6. event streams and Pandas data types.
  7. """
  8. import datetime
  9. import pandas as pd
  10. from thingflow.base import InputThing
  11. class PandasSeriesWriter(InputThing):
  12. """Create a pandas Series object corresponding to the
  13. event stream passed to this subscriber.
  14. """
  15. def __init__(self, tz=datetime.timezone.utc):
  16. self.data= []
  17. self.index = []
  18. self.tz = tz
  19. self.result = None # we will store the series here when done
  20. def on_next(self, x):
  21. self.data.append(x.val)
  22. self.index.append(datetime.datetime.fromtimestamp(x.ts, tz=self.tz))
  23. def on_completed(self):
  24. self.result = pd.Series(self.data, index=self.index)