Browse Source

TimeSeries type v2 (#666)

* TimeSeries -> TimeSeries2 , better inheritance and tests
pull/1/head
Coin de Gamma 6 years ago committed by GitHub
parent
commit
e19d8b13b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      analytics/analytics/analytic_types/__init__.py
  2. 16
      analytics/tests/test_analytic_types.py

15
analytics/analytics/analytic_types/__init__.py

@ -28,9 +28,12 @@ ts = TimeSeries([4, 5, 6], tsis)
Timestamp = Union[str, pd.Timestamp] Timestamp = Union[str, pd.Timestamp]
class TimeSeriesIndex(pd.DatetimeIndex): class TimeSeriesIndex(pd.DatetimeIndex):
def __init__(self, timestamps: List[Timestamp]): def __new__(cls, *args, **kwargs):
super().__init__(timestamps, dtype='datetime64[ns]') return pd.DatetimeIndex.__new__(cls, *args, **kwargs)
class TimeSeries(pd.Series): # TODO: make generic type for values. See List definition for example of generic class
def __init__(self, values: List[object], tsindex: TimeSeriesIndex): # TODO: constructor from DataFrame
super().__init__(values, index=tsindex) # TODO: repleace TimeSeries (above) with this class: rename TimeSeries2 to TimeSeries
class TimeSeries2(pd.Series):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

16
analytics/tests/test_analytic_types.py

@ -0,0 +1,16 @@
from analytic_types import TimeSeriesIndex, TimeSeries2
import unittest
class TestDataset(unittest.TestCase):
def test_basic_timeseries_index(self):
tsi = TimeSeriesIndex(['2017-12-31 16:00:00-08:00'])
self.assertEqual(len(tsi), 1)
tsi2 = TimeSeriesIndex(['2017-12-31 16:00:00-08:00', '2017-12-31 17:00:00-08:00', '2017-12-31 18:00:00-08:00'])
self.assertEqual(len(tsi2), 3)
def test_basic_timeseries(self):
tsis = TimeSeriesIndex(['2017-12-31 16:00:00-08:00', '2017-12-31 17:00:00-08:00', '2017-12-31 18:00:00-08:00'])
ts = TimeSeries2([4, 5, 6], tsis)
self.assertEqual(len(ts), 3)
Loading…
Cancel
Save