From bdec3cab5210a5ab44fcddcc600dda4f8a23dea7 Mon Sep 17 00:00:00 2001 From: stringertheory Date: Mon, 5 Feb 2024 22:13:52 -0600 Subject: [PATCH] separating external tests --- tests/test_traces.py | 110 +--------------------------------- tests/test_traces_external.py | 110 +++++++++++++++++++++++++++++++++- 2 files changed, 110 insertions(+), 110 deletions(-) diff --git a/tests/test_traces.py b/tests/test_traces.py index 3099a3f..27f7160 100644 --- a/tests/test_traces.py +++ b/tests/test_traces.py @@ -1,11 +1,9 @@ import csv import os import pickle -from datetime import datetime, timedelta +from datetime import datetime -import pandas as pd import pytest -from pandas.testing import assert_series_equal from traces import TimeSeries @@ -169,112 +167,6 @@ def test_remove_points_from_interval(): assert ts[5] == 0 -def test_sample_interval_days(): - ts = TimeSeries([(datetime(2012, 1, 1), 400), (datetime(2012, 3, 1), 400)]) - ts[datetime(2012, 1, 4) : datetime(2012, 1, 20)] = 10 - ts[datetime(2012, 1, 25) : datetime(2012, 2, 7)] = 50 - ts[datetime(2012, 1, 19) : datetime(2012, 1, 27)] = 0 - - sr = ts.sample_interval( - sampling_period=timedelta(days=1), end=datetime(2012, 2, 1) - ) - assert list(sr.items()) == [ - (pd.Timestamp("2012-01-01 00:00:00"), 400.0), - (pd.Timestamp("2012-01-02 00:00:00"), 400.0), - (pd.Timestamp("2012-01-03 00:00:00"), 400.0), - (pd.Timestamp("2012-01-04 00:00:00"), 10.0), - (pd.Timestamp("2012-01-05 00:00:00"), 10.0), - (pd.Timestamp("2012-01-06 00:00:00"), 10.0), - (pd.Timestamp("2012-01-07 00:00:00"), 10.0), - (pd.Timestamp("2012-01-08 00:00:00"), 10.0), - (pd.Timestamp("2012-01-09 00:00:00"), 10.0), - (pd.Timestamp("2012-01-10 00:00:00"), 10.0), - (pd.Timestamp("2012-01-11 00:00:00"), 10.0), - (pd.Timestamp("2012-01-12 00:00:00"), 10.0), - (pd.Timestamp("2012-01-13 00:00:00"), 10.0), - (pd.Timestamp("2012-01-14 00:00:00"), 10.0), - (pd.Timestamp("2012-01-15 00:00:00"), 10.0), - (pd.Timestamp("2012-01-16 00:00:00"), 10.0), - (pd.Timestamp("2012-01-17 00:00:00"), 10.0), - (pd.Timestamp("2012-01-18 00:00:00"), 10.0), - (pd.Timestamp("2012-01-19 00:00:00"), 0.0), - (pd.Timestamp("2012-01-20 00:00:00"), 0.0), - (pd.Timestamp("2012-01-21 00:00:00"), 0.0), - (pd.Timestamp("2012-01-22 00:00:00"), 0.0), - (pd.Timestamp("2012-01-23 00:00:00"), 0.0), - (pd.Timestamp("2012-01-24 00:00:00"), 0.0), - (pd.Timestamp("2012-01-25 00:00:00"), 0.0), - (pd.Timestamp("2012-01-26 00:00:00"), 0.0), - (pd.Timestamp("2012-01-27 00:00:00"), 50.0), - (pd.Timestamp("2012-01-28 00:00:00"), 50.0), - (pd.Timestamp("2012-01-29 00:00:00"), 50.0), - (pd.Timestamp("2012-01-30 00:00:00"), 50.0), - (pd.Timestamp("2012-01-31 00:00:00"), 50.0), - ] - - -def test_sample_interval_hours(): - ts = TimeSeries([(datetime(2012, 1, 1), 400), (datetime(2012, 1, 10), 400)]) - - ts[datetime(2012, 1, 4, 12) : datetime(2012, 1, 6, 20)] = 10 - ts[datetime(2012, 1, 7, 9) : datetime(2012, 1, 10)] = 50 - - sr = ts.sample_interval(sampling_period=timedelta(days=1)) - assert list(sr.items()) == [ - (pd.Timestamp("2012-01-01 00:00:00"), 400.0), - (pd.Timestamp("2012-01-02 00:00:00"), 400.0), - (pd.Timestamp("2012-01-03 00:00:00"), 400.0), - (pd.Timestamp("2012-01-04 00:00:00"), 205.0), - (pd.Timestamp("2012-01-05 00:00:00"), 10.0), - (pd.Timestamp("2012-01-06 00:00:00"), 75.0), - (pd.Timestamp("2012-01-07 00:00:00"), 181.25), - (pd.Timestamp("2012-01-08 00:00:00"), 50.0), - (pd.Timestamp("2012-01-09 00:00:00"), 50.0), - ] - - sr = ts.sample_interval(sampling_period=timedelta(days=1), operation="max") - assert list(sr.items()) == [ - (pd.Timestamp("2012-01-01 00:00:00"), 400.0), - (pd.Timestamp("2012-01-02 00:00:00"), 400.0), - (pd.Timestamp("2012-01-03 00:00:00"), 400.0), - (pd.Timestamp("2012-01-04 00:00:00"), 400.0), - (pd.Timestamp("2012-01-05 00:00:00"), 10.0), - (pd.Timestamp("2012-01-06 00:00:00"), 400.0), - (pd.Timestamp("2012-01-07 00:00:00"), 400.0), - (pd.Timestamp("2012-01-08 00:00:00"), 50.0), - (pd.Timestamp("2012-01-09 00:00:00"), 50.0), - ] - - sr = ts.sample_interval(sampling_period=timedelta(days=1), operation="min") - assert list(sr.items()) == [ - (pd.Timestamp("2012-01-01 00:00:00"), 400.0), - (pd.Timestamp("2012-01-02 00:00:00"), 400.0), - (pd.Timestamp("2012-01-03 00:00:00"), 400.0), - (pd.Timestamp("2012-01-04 00:00:00"), 10.0), - (pd.Timestamp("2012-01-05 00:00:00"), 10.0), - (pd.Timestamp("2012-01-06 00:00:00"), 10.0), - (pd.Timestamp("2012-01-07 00:00:00"), 50.0), - (pd.Timestamp("2012-01-08 00:00:00"), 50.0), - (pd.Timestamp("2012-01-09 00:00:00"), 50.0), - ] - - -def test_sample_interval_index(): - start = datetime(2012, 1, 1) - end = datetime(2012, 1, 10) - - ts = TimeSeries([(start, 400), (end, 400)]) - - ts[datetime(2012, 1, 4, 12) : datetime(2012, 1, 6, 20)] = 10 - ts[datetime(2012, 1, 7, 9) : datetime(2012, 1, 10)] = 50 - - idx = pd.date_range(start, end, freq="D") - sr = ts.sample_interval(sampling_period=timedelta(days=1)) - sr2 = ts.sample_interval(idx=idx) - - assert_series_equal(sr, sr2) - - def test_pickle(): ts = TimeSeries(default=False) ts[1] = True diff --git a/tests/test_traces_external.py b/tests/test_traces_external.py index 3f018f1..9a6812c 100644 --- a/tests/test_traces_external.py +++ b/tests/test_traces_external.py @@ -1,8 +1,10 @@ import csv import os -from datetime import datetime +from datetime import datetime, timedelta +import pandas as pd from dateutil.parser import parse as date_parse +from pandas.testing import assert_series_equal from traces import TimeSeries @@ -25,3 +27,109 @@ def test_csv(): assert ts[datetime(2000, 1, 1, 9)] is None assert ts[datetime(2000, 1, 1, 10, 30)] == "15" assert ts[datetime(2000, 1, 1, 20)] == "nan" + + +def test_sample_interval_days(): + ts = TimeSeries([(datetime(2012, 1, 1), 400), (datetime(2012, 3, 1), 400)]) + ts[datetime(2012, 1, 4) : datetime(2012, 1, 20)] = 10 + ts[datetime(2012, 1, 25) : datetime(2012, 2, 7)] = 50 + ts[datetime(2012, 1, 19) : datetime(2012, 1, 27)] = 0 + + sr = ts.sample_interval( + sampling_period=timedelta(days=1), end=datetime(2012, 2, 1) + ) + assert list(sr.items()) == [ + (pd.Timestamp("2012-01-01 00:00:00"), 400.0), + (pd.Timestamp("2012-01-02 00:00:00"), 400.0), + (pd.Timestamp("2012-01-03 00:00:00"), 400.0), + (pd.Timestamp("2012-01-04 00:00:00"), 10.0), + (pd.Timestamp("2012-01-05 00:00:00"), 10.0), + (pd.Timestamp("2012-01-06 00:00:00"), 10.0), + (pd.Timestamp("2012-01-07 00:00:00"), 10.0), + (pd.Timestamp("2012-01-08 00:00:00"), 10.0), + (pd.Timestamp("2012-01-09 00:00:00"), 10.0), + (pd.Timestamp("2012-01-10 00:00:00"), 10.0), + (pd.Timestamp("2012-01-11 00:00:00"), 10.0), + (pd.Timestamp("2012-01-12 00:00:00"), 10.0), + (pd.Timestamp("2012-01-13 00:00:00"), 10.0), + (pd.Timestamp("2012-01-14 00:00:00"), 10.0), + (pd.Timestamp("2012-01-15 00:00:00"), 10.0), + (pd.Timestamp("2012-01-16 00:00:00"), 10.0), + (pd.Timestamp("2012-01-17 00:00:00"), 10.0), + (pd.Timestamp("2012-01-18 00:00:00"), 10.0), + (pd.Timestamp("2012-01-19 00:00:00"), 0.0), + (pd.Timestamp("2012-01-20 00:00:00"), 0.0), + (pd.Timestamp("2012-01-21 00:00:00"), 0.0), + (pd.Timestamp("2012-01-22 00:00:00"), 0.0), + (pd.Timestamp("2012-01-23 00:00:00"), 0.0), + (pd.Timestamp("2012-01-24 00:00:00"), 0.0), + (pd.Timestamp("2012-01-25 00:00:00"), 0.0), + (pd.Timestamp("2012-01-26 00:00:00"), 0.0), + (pd.Timestamp("2012-01-27 00:00:00"), 50.0), + (pd.Timestamp("2012-01-28 00:00:00"), 50.0), + (pd.Timestamp("2012-01-29 00:00:00"), 50.0), + (pd.Timestamp("2012-01-30 00:00:00"), 50.0), + (pd.Timestamp("2012-01-31 00:00:00"), 50.0), + ] + + +def test_sample_interval_hours(): + ts = TimeSeries([(datetime(2012, 1, 1), 400), (datetime(2012, 1, 10), 400)]) + + ts[datetime(2012, 1, 4, 12) : datetime(2012, 1, 6, 20)] = 10 + ts[datetime(2012, 1, 7, 9) : datetime(2012, 1, 10)] = 50 + + sr = ts.sample_interval(sampling_period=timedelta(days=1)) + assert list(sr.items()) == [ + (pd.Timestamp("2012-01-01 00:00:00"), 400.0), + (pd.Timestamp("2012-01-02 00:00:00"), 400.0), + (pd.Timestamp("2012-01-03 00:00:00"), 400.0), + (pd.Timestamp("2012-01-04 00:00:00"), 205.0), + (pd.Timestamp("2012-01-05 00:00:00"), 10.0), + (pd.Timestamp("2012-01-06 00:00:00"), 75.0), + (pd.Timestamp("2012-01-07 00:00:00"), 181.25), + (pd.Timestamp("2012-01-08 00:00:00"), 50.0), + (pd.Timestamp("2012-01-09 00:00:00"), 50.0), + ] + + sr = ts.sample_interval(sampling_period=timedelta(days=1), operation="max") + assert list(sr.items()) == [ + (pd.Timestamp("2012-01-01 00:00:00"), 400.0), + (pd.Timestamp("2012-01-02 00:00:00"), 400.0), + (pd.Timestamp("2012-01-03 00:00:00"), 400.0), + (pd.Timestamp("2012-01-04 00:00:00"), 400.0), + (pd.Timestamp("2012-01-05 00:00:00"), 10.0), + (pd.Timestamp("2012-01-06 00:00:00"), 400.0), + (pd.Timestamp("2012-01-07 00:00:00"), 400.0), + (pd.Timestamp("2012-01-08 00:00:00"), 50.0), + (pd.Timestamp("2012-01-09 00:00:00"), 50.0), + ] + + sr = ts.sample_interval(sampling_period=timedelta(days=1), operation="min") + assert list(sr.items()) == [ + (pd.Timestamp("2012-01-01 00:00:00"), 400.0), + (pd.Timestamp("2012-01-02 00:00:00"), 400.0), + (pd.Timestamp("2012-01-03 00:00:00"), 400.0), + (pd.Timestamp("2012-01-04 00:00:00"), 10.0), + (pd.Timestamp("2012-01-05 00:00:00"), 10.0), + (pd.Timestamp("2012-01-06 00:00:00"), 10.0), + (pd.Timestamp("2012-01-07 00:00:00"), 50.0), + (pd.Timestamp("2012-01-08 00:00:00"), 50.0), + (pd.Timestamp("2012-01-09 00:00:00"), 50.0), + ] + + +def test_sample_interval_index(): + start = datetime(2012, 1, 1) + end = datetime(2012, 1, 10) + + ts = TimeSeries([(start, 400), (end, 400)]) + + ts[datetime(2012, 1, 4, 12) : datetime(2012, 1, 6, 20)] = 10 + ts[datetime(2012, 1, 7, 9) : datetime(2012, 1, 10)] = 50 + + idx = pd.date_range(start, end, freq="D") + sr = ts.sample_interval(sampling_period=timedelta(days=1)) + sr2 = ts.sample_interval(idx=idx) + + assert_series_equal(sr, sr2)