def test_with_data_as_tuple_with_strings(self): mytuple = ( ('2019-01-01', 0), (timestamp_converter('2019-01-01'), 1), ) ts = TimeSeries(*mytuple, default=10) expected = {timestamp_converter(key): value for key, value in mytuple} assert ts.items() == expected.items() assert ts.default == 10
def test_with_dict_keys_being_strings(self): dct = { '2019-01-01': 1, '2019-02-01': 2, timestamp_converter('2019-03-01'): 3, } ts = TimeSeries(dct, default=10) expected = { timestamp_converter(key): value for key, value in dct.items() } assert ts.items() == expected.items() assert ts.default == 10
def test_get_on_slice_out_of_bounds_left_side(self, smallts): start = CURRENT - 2 * ONEHOUR end = CURRENT data = {CURRENT: 0} sliced_ts = smallts[start:end + 1 * ONEHOUR] expected_ts = TimeSeries(data, default=smallts.default) testing.assert_ts_equal(sliced_ts, expected_ts)
def test_with_data_as_dataframe_raises_when_several_columns(self): df = pd.DataFrame(columns=['Too', 'Many', 'Columns']) with pytest.raises(Exception) as err: TimeSeries(df) expected = "Can't convert a DataFrame with several columns" assert expected in str(err.value)
def test_sample_out_of_left_bound_with_no_default_permissive_shorten_interval( self, smallts): ts = smallts.sample(freq=HALFHOUR, start=CURRENT - ONEHOUR, end=CURRENT + ONEHOUR) expected_ts = TimeSeries({CURRENT: 0, CURRENT + HALFHOUR: 0}) testing.assert_ts_equal(expected_ts, ts)
def test_with_data_as_dataframe(self, smalldict): df = pd.DataFrame(data={'SomeName': list(smalldict.values())}, index=smalldict.keys()) ts = TimeSeries(df, default=10) assert ts[CURRENT] == 0 assert ts.name == 'SomeName' assert len(ts) == df.shape[0] assert ts.default == 10
def test_get_on_slice_add_back_previous_value_if_start_not_in_keys( self, smallts): start = CURRENT + HALFHOUR end = CURRENT + ONEHOUR sliced_ts = smallts[start:end + ONEHOUR] data = {start: 0, end: 1} expected_ts = TimeSeries(data, default=smallts.default) testing.assert_ts_equal(sliced_ts, expected_ts)
def test_get_on_slice_exclude_upper_bound_include_lower_bound( self, smallts): start = CURRENT end = CURRENT + 1 * ONEHOUR sliced_ts = smallts[start:end + ONEHOUR] data = {start: 0, end: 1} expected_ts = TimeSeries(data, default=smallts.default) testing.assert_ts_equal(sliced_ts, expected_ts)
def test_simple_sample_on_default(self, smallts): ts = smallts.sample(HALFHOUR) expected_dict = { **smallts.data, **{key + HALFHOUR: smallts[key] for key in smallts.index[:-1]} } expected_ts = TimeSeries(expected_dict, default=smallts.default) testing.assert_ts_equal(expected_ts, ts)
def test_with_data_as_tuple(self): mytuple = ( (CURRENT, 0), ('2019-02-01', 1), ) ts = TimeSeries(mytuple, default=10) assert ts[CURRENT] == 0 assert ts['2019-02-01'] == 1 assert len(ts) == 2 assert ts.default == 10
def test_with_data_as_tuple(self): mytuple = ( (CURRENT, 0), (CURRENT + ONEHOUR, 1), ) ts = TimeSeries(mytuple, default=10) assert ts[CURRENT] == 0 assert ts[CURRENT + ONEHOUR] == 1 assert len(ts) == 2 assert ts.default == 10
def test_update(self, smallts): og_ts = deepcopy(smallts) last_index = smallts.index[-1] new_index = last_index + pd.Timedelta('2 days') new_value = 10 # Updating with TimeSeries other = TimeSeries({new_index: new_value, last_index: new_value}) og_ts.update(other) assert og_ts[new_index] == new_value assert og_ts[last_index] == new_value # Updating with tuple og_ts = deepcopy(smallts) other_tuple = tuple(other.items()) og_ts.update(other_tuple) assert og_ts[new_index] == new_value # Updating with *args og_ts = deepcopy(smallts) other_new_index = new_index + pd.Timedelta('2 hours') other_new = TimeSeries({other_new_index: new_value}) og_ts.update([other, other_new]) assert og_ts[new_index] == new_value assert og_ts[other_new_index] == new_value # Updating with dict og_ts = deepcopy(smallts) og_ts.update({new_index: new_value}) assert og_ts[new_index] == new_value # Updating with **kwargs og_ts = deepcopy(smallts) with pytest.raises(TypeError): og_ts.update(**{new_index: new_value})
def emptyts_withdefault(): return TimeSeries(default=10)
def emptyts(): return TimeSeries()
def otherts_withdefault(otherict): return TimeSeries(otherict, default=900)
def otherts(otherict): return TimeSeries(otherict)
def maskts(): dct = { CURRENT + 2 * ONEHOUR + HALFHOUR: True, CURRENT + 4 * ONEHOUR: False, } return TimeSeries(dct)
def test_with_data_as_pandas_series(self, smalldict): serie = pd.Series(data=smalldict, name='SomeName') ts = TimeSeries(serie) assert ts.name == 'SomeName' assert ts[CURRENT] == 0 assert len(ts) == len(serie)
def test_simple_sample_with_start_being_string(self, smallts): start = '2019-01-01T09:00:00' ts = smallts.sample(HALFHOUR, start=start) expected_ts = TimeSeries({start: 9}) testing.assert_ts_equal(expected_ts, ts)
from datetime import timedelta from ticts import TimeSeries from ticts.utils import timestamp_converter onehour = timedelta(hours=1) onemin = timedelta(minutes=1) dt1 = timestamp_converter('2019-01-01') dt2 = timestamp_converter('2019-01-02') smalldct = { dt1 + onehour: 1, dt1 + 3 * onehour: 3, dt1 + 6 * onehour: 10, } smallts = TimeSeries(smalldct) otherdct = { dt1 + 2 * onehour: 2, dt1 + 3 * onehour: 3, dt1 + 5 * onehour: 5, } otherts = TimeSeries(otherdct, default=0)
def test_consecutive_setitem(self, smallts): smallts[CURRENT] = 1000 first_time = TimeSeries(smallts) smallts[CURRENT] = 1000 testing.assert_ts_equal(first_time, smallts)
def test_with_data_as_ticts_timeserie_and_name_given(self, smallts): smallts.default = 1000 smallts.name = 'SomeName' newts = TimeSeries(smallts, name='SomeOtherName') testing.assert_ts_equal(smallts, newts, check_name=False) assert newts.name == 'SomeOtherName'
def test_it_has_no_default(self): ts = TimeSeries() assert not ts._has_default
def test_get_item_out_of_left_bound_with_default_zero(self): mytuple = ((CURRENT, 0), (CURRENT + ONEHOUR, 1)) ts = TimeSeries(mytuple, default=0) assert ts[CURRENT + ONEHOUR] == 1 assert ts[CURRENT - ONEHOUR] == 0
def test_get_on_slice_entirely_out_of_bounds_on_right_side(self, smallts): start = CURRENT + 10 * ONEHOUR end = CURRENT + 12 * ONEHOUR sliced_ts = smallts[start:end] expected_ts = TimeSeries({start: smallts[start]}) testing.assert_ts_equal(sliced_ts, expected_ts)
def test_it_returns_timeseries_from_json(self, smallts, tmpdir): path = tmpdir.join('test.json') smallts.to_json(path) ts_read = TimeSeries.from_json(path) testing.assert_ts_equal(smallts, ts_read)
def smallts(smalldict): return TimeSeries(smalldict)
def smallts_withdefault(smalldict): return TimeSeries(smalldict, default=10)
def test_with_data_as_ticts_timeserie(self, smallts): smallts.default = 1000 smallts.name = 'SomeName' testing.assert_ts_equal(smallts, TimeSeries(smallts))
def test_get_on_slice_out_of_bounds_on_right_side(self, smallts): sliced_ts = smallts[CURRENT + 9 * ONEHOUR:CURRENT + 12 * ONEHOUR] expected_dct = {CURRENT + 9 * ONEHOUR: 9} expected_ts = TimeSeries(expected_dct, default=smallts.default) testing.assert_ts_equal(sliced_ts, expected_ts)