2022-02-22 05:59:07 +00:00
|
|
|
import datetime
|
2022-02-24 04:41:58 +00:00
|
|
|
import random
|
2022-06-12 16:05:13 +00:00
|
|
|
from typing import Mapping
|
2022-02-22 05:59:07 +00:00
|
|
|
|
2022-06-12 16:05:13 +00:00
|
|
|
import pyfacts as pft
|
2022-03-29 05:05:41 +00:00
|
|
|
import pytest
|
2022-06-05 17:36:12 +00:00
|
|
|
from pyfacts.utils import PyfactsOptions
|
2022-02-22 05:59:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestFrequency:
|
|
|
|
def test_creation(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
D = pft.Frequency("daily", "days", 1, 1, "D")
|
2022-02-22 05:59:07 +00:00
|
|
|
assert D.days == 1
|
2022-03-29 05:05:41 +00:00
|
|
|
assert D.symbol == "D"
|
|
|
|
assert D.name == "daily"
|
2022-02-22 05:59:07 +00:00
|
|
|
assert D.value == 1
|
2022-03-29 05:05:41 +00:00
|
|
|
assert D.freq_type == "days"
|
2022-02-22 05:59:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestAllFrequencies:
|
|
|
|
def test_attributes(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
assert hasattr(pft.AllFrequencies, "D")
|
|
|
|
assert hasattr(pft.AllFrequencies, "M")
|
|
|
|
assert hasattr(pft.AllFrequencies, "Q")
|
2022-02-22 05:59:07 +00:00
|
|
|
|
|
|
|
def test_days(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
assert pft.AllFrequencies.D.days == 1
|
|
|
|
assert pft.AllFrequencies.M.days == 30
|
|
|
|
assert pft.AllFrequencies.Q.days == 91
|
2022-02-22 05:59:07 +00:00
|
|
|
|
|
|
|
def test_symbol(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
assert pft.AllFrequencies.H.symbol == "H"
|
|
|
|
assert pft.AllFrequencies.W.symbol == "W"
|
2022-02-22 05:59:07 +00:00
|
|
|
|
|
|
|
def test_values(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
assert pft.AllFrequencies.H.value == 6
|
|
|
|
assert pft.AllFrequencies.Y.value == 1
|
2022-02-22 05:59:07 +00:00
|
|
|
|
|
|
|
def test_type(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
assert pft.AllFrequencies.Q.freq_type == "months"
|
|
|
|
assert pft.AllFrequencies.W.freq_type == "days"
|
2022-02-22 05:59:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestSeries:
|
|
|
|
def test_creation(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
series = pft.Series([1, 2, 3, 4, 5, 6, 7], dtype="number")
|
2022-02-22 05:59:07 +00:00
|
|
|
assert series.dtype == float
|
|
|
|
assert series[2] == 3
|
|
|
|
|
2022-06-12 16:05:13 +00:00
|
|
|
dates = pft.create_date_series("2021-01-01", "2021-01-31", frequency="D")
|
|
|
|
series = pft.Series(dates, dtype="date")
|
2022-02-22 07:35:54 +00:00
|
|
|
assert series.dtype == datetime.datetime
|
2022-02-23 18:45:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestTimeSeriesCore:
|
2022-03-29 05:05:41 +00:00
|
|
|
data = [("2021-01-01", 220), ("2021-02-01", 230), ("2021-03-01", 240)]
|
2022-02-23 18:45:00 +00:00
|
|
|
|
2022-06-12 16:05:13 +00:00
|
|
|
def test_repr_str(self, create_test_data):
|
|
|
|
ts = pft.TimeSeriesCore(self.data, frequency="M")
|
2022-03-29 05:05:41 +00:00
|
|
|
assert str(ts) in repr(ts).replace("\t", " ")
|
2022-02-24 04:41:58 +00:00
|
|
|
|
2022-06-12 16:05:13 +00:00
|
|
|
data = create_test_data(frequency=pft.AllFrequencies.D, eomonth=False, num=50, dates_as_string=True)
|
|
|
|
ts = pft.TimeSeriesCore(data, frequency="D")
|
2022-03-29 05:05:41 +00:00
|
|
|
assert "..." in str(ts)
|
|
|
|
assert "..." in repr(ts)
|
2022-02-24 04:41:58 +00:00
|
|
|
|
2022-02-23 18:45:00 +00:00
|
|
|
def test_creation(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, frequency="M")
|
|
|
|
assert isinstance(ts, pft.TimeSeriesCore)
|
2022-04-10 18:21:56 +00:00
|
|
|
assert isinstance(ts, Mapping)
|
2022-02-23 18:45:00 +00:00
|
|
|
|
2022-06-12 16:05:13 +00:00
|
|
|
def test_creation_no_freq(self, create_test_data):
|
|
|
|
data = create_test_data(num=300, frequency=pft.AllFrequencies.D)
|
|
|
|
ts = pft.TimeSeriesCore(data)
|
|
|
|
assert ts.frequency == pft.AllFrequencies.D
|
|
|
|
|
|
|
|
data = create_test_data(num=300, frequency=pft.AllFrequencies.M)
|
|
|
|
ts = pft.TimeSeriesCore(data)
|
|
|
|
assert ts.frequency == pft.AllFrequencies.M
|
|
|
|
|
|
|
|
def test_creation_no_freq_missing_data(self, create_test_data):
|
|
|
|
data = create_test_data(num=300, frequency=pft.AllFrequencies.D)
|
|
|
|
data = random.sample(data, 182)
|
|
|
|
ts = pft.TimeSeriesCore(data)
|
|
|
|
assert ts.frequency == pft.AllFrequencies.D
|
|
|
|
|
|
|
|
data = create_test_data(num=300, frequency=pft.AllFrequencies.D)
|
|
|
|
data = random.sample(data, 175)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ts = pft.TimeSeriesCore(data)
|
|
|
|
|
|
|
|
data = create_test_data(num=100, frequency=pft.AllFrequencies.W)
|
|
|
|
data = random.sample(data, 70)
|
|
|
|
ts = pft.TimeSeriesCore(data)
|
|
|
|
assert ts.frequency == pft.AllFrequencies.W
|
|
|
|
|
|
|
|
data = create_test_data(num=100, frequency=pft.AllFrequencies.W)
|
|
|
|
data = random.sample(data, 68)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
pft.TimeSeriesCore(data)
|
|
|
|
|
|
|
|
def test_creation_wrong_freq(self, create_test_data):
|
|
|
|
data = create_test_data(num=100, frequency=pft.AllFrequencies.W)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
pft.TimeSeriesCore(data, frequency="D")
|
|
|
|
|
|
|
|
data = create_test_data(num=100, frequency=pft.AllFrequencies.D)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
pft.TimeSeriesCore(data, frequency="W")
|
|
|
|
|
2022-04-03 09:57:07 +00:00
|
|
|
|
|
|
|
class TestSlicing:
|
|
|
|
data = [("2021-01-01", 220), ("2021-02-01", 230), ("2021-03-01", 240)]
|
|
|
|
|
2022-02-23 18:45:00 +00:00
|
|
|
def test_getitem(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, frequency="M")
|
2022-02-23 18:45:00 +00:00
|
|
|
assert ts.dates[0] == datetime.datetime(2021, 1, 1, 0, 0)
|
|
|
|
assert ts.values[0] == 220
|
2022-03-29 05:05:41 +00:00
|
|
|
assert ts["2021-01-01"][1] == 220
|
|
|
|
assert len(ts[ts.dates > "2021-01-01"]) == 2
|
|
|
|
assert ts[ts.dates == "2021-02-01"].iloc[0][1] == 230
|
2022-02-23 18:45:00 +00:00
|
|
|
assert ts.iloc[2][0] == datetime.datetime(2021, 3, 1)
|
|
|
|
assert len(ts.iloc[:2]) == 2
|
2022-03-29 05:05:41 +00:00
|
|
|
with pytest.raises(KeyError):
|
|
|
|
ts["2021-02-03"]
|
|
|
|
subset_ts = ts[["2021-01-01", "2021-03-01"]]
|
|
|
|
assert len(subset_ts) == 2
|
2022-06-12 16:05:13 +00:00
|
|
|
assert isinstance(subset_ts, pft.TimeSeriesCore)
|
2022-03-29 05:05:41 +00:00
|
|
|
assert subset_ts.iloc[1][1] == 240
|
|
|
|
|
|
|
|
def test_get(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, frequency="M")
|
2022-03-29 05:05:41 +00:00
|
|
|
assert ts.dates[0] == datetime.datetime(2021, 1, 1, 0, 0)
|
|
|
|
assert ts.values[0] == 220
|
|
|
|
assert ts.get("2021-01-01")[1] == 220
|
|
|
|
assert ts.get("2021-02-15") is None
|
|
|
|
assert ts.get("2021-02-23", -1) == -1
|
|
|
|
assert ts.get("2021-02-10", closest="previous")[1] == 230
|
|
|
|
assert ts.get("2021-02-10", closest="next")[1] == 240
|
2022-06-05 17:36:12 +00:00
|
|
|
PyfactsOptions.get_closest = "previous"
|
2022-03-29 05:05:41 +00:00
|
|
|
assert ts.get("2021-02-10")[1] == 230
|
2022-06-05 17:36:12 +00:00
|
|
|
PyfactsOptions.get_closest = "next"
|
2022-03-29 05:05:41 +00:00
|
|
|
assert ts.get("2021-02-10")[1] == 240
|
2022-02-24 02:46:45 +00:00
|
|
|
|
|
|
|
def test_contains(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, frequency="M")
|
2022-02-24 02:46:45 +00:00
|
|
|
assert datetime.datetime(2021, 1, 1) in ts
|
2022-03-29 05:05:41 +00:00
|
|
|
assert "2021-01-01" in ts
|
|
|
|
assert "2021-01-14" not in ts
|
2022-02-24 02:46:45 +00:00
|
|
|
|
|
|
|
def test_items(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, frequency="M")
|
2022-02-24 02:46:45 +00:00
|
|
|
for i, j in ts.items():
|
|
|
|
assert j == self.data[0][1]
|
|
|
|
break
|
2022-03-29 05:05:41 +00:00
|
|
|
|
|
|
|
def test_special_keys(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, frequency="M")
|
2022-03-29 05:05:41 +00:00
|
|
|
dates = ts["dates"]
|
|
|
|
values = ts["values"]
|
2022-06-12 16:05:13 +00:00
|
|
|
assert isinstance(dates, pft.Series)
|
|
|
|
assert isinstance(values, pft.Series)
|
2022-03-29 05:05:41 +00:00
|
|
|
assert len(dates) == 3
|
|
|
|
assert len(values) == 3
|
|
|
|
assert dates[0] == datetime.datetime(2021, 1, 1, 0, 0)
|
|
|
|
assert values[0] == 220
|
|
|
|
|
2022-04-03 09:57:07 +00:00
|
|
|
def test_iloc_slicing(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, frequency="M")
|
2022-04-03 09:57:07 +00:00
|
|
|
assert ts.iloc[0] == (datetime.datetime(2021, 1, 1), 220)
|
|
|
|
assert ts.iloc[-1] == (datetime.datetime(2021, 3, 1), 240)
|
|
|
|
|
|
|
|
ts_slice = ts.iloc[0:2]
|
2022-06-12 16:05:13 +00:00
|
|
|
assert isinstance(ts_slice, pft.TimeSeriesCore)
|
2022-04-03 09:57:07 +00:00
|
|
|
assert len(ts_slice) == 2
|
|
|
|
|
2022-03-29 05:05:41 +00:00
|
|
|
|
2022-06-12 16:05:13 +00:00
|
|
|
class TestComparativeSlicing:
|
|
|
|
def test_date_gt_daily(self, create_test_data):
|
|
|
|
data = create_test_data(num=300, frequency=pft.AllFrequencies.D)
|
|
|
|
ts = pft.TimeSeries(data, "D")
|
|
|
|
ts_rr = ts.calculate_rolling_returns(return_period_unit="months")
|
|
|
|
assert len(ts_rr) == 269
|
|
|
|
subset = ts_rr[ts_rr.values < 0.1]
|
|
|
|
assert isinstance(subset, pft.TimeSeriesCore)
|
|
|
|
assert subset.frequency == pft.AllFrequencies.D
|
|
|
|
|
|
|
|
def test_date_gt_monthly(self, create_test_data):
|
|
|
|
data = create_test_data(num=60, frequency=pft.AllFrequencies.M)
|
|
|
|
ts = pft.TimeSeries(data, "M")
|
|
|
|
ts_rr = ts.calculate_rolling_returns(return_period_unit="months")
|
|
|
|
assert len(ts_rr) == 59
|
|
|
|
subset = ts_rr[ts_rr.values < 0.1]
|
|
|
|
assert isinstance(subset, pft.TimeSeriesCore)
|
|
|
|
assert subset.frequency == pft.AllFrequencies.M
|
|
|
|
|
|
|
|
|
2022-04-10 18:21:56 +00:00
|
|
|
class TestSetitem:
|
|
|
|
data = [("2021-01-01", 220), ("2021-01-04", 230), ("2021-03-07", 240)]
|
|
|
|
|
|
|
|
def test_setitem(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, frequency="M")
|
2022-04-10 18:21:56 +00:00
|
|
|
assert len(ts) == 3
|
|
|
|
|
|
|
|
ts["2021-01-02"] = 225
|
|
|
|
assert len(ts) == 4
|
|
|
|
assert ts["2021-01-02"][1] == 225
|
|
|
|
|
|
|
|
ts["2021-01-02"] = 227.6
|
|
|
|
assert len(ts) == 4
|
|
|
|
assert ts["2021-01-02"][1] == 227.6
|
|
|
|
|
|
|
|
def test_errors(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, frequency="M")
|
2022-04-10 18:21:56 +00:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
ts["2021-01-03"] = "abc"
|
|
|
|
|
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
ts.iloc[4] = 4
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ts["abc"] = 12
|
|
|
|
|
|
|
|
|
2022-03-29 05:05:41 +00:00
|
|
|
class TestTimeSeriesCoreHeadTail:
|
|
|
|
data = [
|
|
|
|
("2021-01-01", 220),
|
|
|
|
("2021-02-01", 230),
|
|
|
|
("2021-03-01", 240),
|
|
|
|
("2021-04-01", 250),
|
|
|
|
("2021-05-01", 260),
|
|
|
|
("2021-06-01", 270),
|
|
|
|
("2021-07-01", 280),
|
|
|
|
("2021-08-01", 290),
|
|
|
|
("2021-09-01", 300),
|
|
|
|
("2021-10-01", 310),
|
|
|
|
("2021-11-01", 320),
|
|
|
|
("2021-12-01", 330),
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_head(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, frequency="M")
|
2022-03-29 05:05:41 +00:00
|
|
|
assert len(ts.head()) == 6
|
|
|
|
assert len(ts.head(3)) == 3
|
2022-06-12 16:05:13 +00:00
|
|
|
assert isinstance(ts.head(), pft.TimeSeriesCore)
|
2022-03-29 05:05:41 +00:00
|
|
|
head_ts = ts.head(6)
|
|
|
|
assert head_ts.iloc[-1][1] == 270
|
|
|
|
|
|
|
|
def test_tail(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, frequency="M")
|
2022-03-29 05:05:41 +00:00
|
|
|
assert len(ts.tail()) == 6
|
|
|
|
assert len(ts.tail(8)) == 8
|
2022-06-12 16:05:13 +00:00
|
|
|
assert isinstance(ts.tail(), pft.TimeSeriesCore)
|
2022-03-29 05:05:41 +00:00
|
|
|
tail_ts = ts.tail(6)
|
|
|
|
assert tail_ts.iloc[0][1] == 280
|
|
|
|
|
|
|
|
def test_head_tail(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, frequency="M")
|
2022-03-29 05:05:41 +00:00
|
|
|
head_tail_ts = ts.head(8).tail(2)
|
2022-06-12 16:05:13 +00:00
|
|
|
assert isinstance(head_tail_ts, pft.TimeSeriesCore)
|
2022-03-29 05:05:41 +00:00
|
|
|
assert "2021-07-01" in head_tail_ts
|
|
|
|
assert head_tail_ts.iloc[1][1] == 290
|
2022-04-11 16:49:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestDelitem:
|
|
|
|
data = [
|
|
|
|
("2021-01-01", 220),
|
|
|
|
("2021-02-01", 230),
|
|
|
|
("2021-03-01", 240),
|
|
|
|
("2021-04-01", 250),
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_deletion(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, "M")
|
2022-04-11 16:49:17 +00:00
|
|
|
assert len(ts) == 4
|
|
|
|
del ts["2021-03-01"]
|
|
|
|
assert len(ts) == 3
|
|
|
|
assert "2021-03-01" not in ts
|
|
|
|
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
del ts["2021-03-01"]
|
|
|
|
|
|
|
|
|
|
|
|
class TestTimeSeriesComparisons:
|
|
|
|
data1 = [
|
|
|
|
("2021-01-01", 220),
|
|
|
|
("2021-02-01", 230),
|
|
|
|
("2021-03-01", 240),
|
|
|
|
("2021-04-01", 250),
|
|
|
|
]
|
|
|
|
|
|
|
|
data2 = [
|
|
|
|
("2021-01-01", 240),
|
|
|
|
("2021-02-01", 210),
|
|
|
|
("2021-03-01", 240),
|
|
|
|
("2021-04-01", 270),
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_number_comparison(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts1 = pft.TimeSeriesCore(self.data1, "M")
|
|
|
|
assert isinstance(ts1 > 23, pft.TimeSeriesCore)
|
|
|
|
assert (ts1 > 230).values == pft.Series([0.0, 0.0, 1.0, 1.0], "float")
|
|
|
|
assert (ts1 >= 230).values == pft.Series([0.0, 1.0, 1.0, 1.0], "float")
|
|
|
|
assert (ts1 < 240).values == pft.Series([1.0, 1.0, 0.0, 0.0], "float")
|
|
|
|
assert (ts1 <= 240).values == pft.Series([1.0, 1.0, 1.0, 0.0], "float")
|
|
|
|
assert (ts1 == 240).values == pft.Series([0.0, 0.0, 1.0, 0.0], "float")
|
|
|
|
assert (ts1 != 240).values == pft.Series([1.0, 1.0, 0.0, 1.0], "float")
|
2022-04-11 16:49:17 +00:00
|
|
|
|
|
|
|
def test_series_comparison(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts1 = pft.TimeSeriesCore(self.data1, "M")
|
|
|
|
ser = pft.Series([240, 210, 240, 270], dtype="int")
|
2022-04-11 16:49:17 +00:00
|
|
|
|
2022-06-12 16:05:13 +00:00
|
|
|
assert (ts1 > ser).values == pft.Series([0.0, 1.0, 0.0, 0.0], "float")
|
|
|
|
assert (ts1 >= ser).values == pft.Series([0.0, 1.0, 1.0, 0.0], "float")
|
|
|
|
assert (ts1 < ser).values == pft.Series([1.0, 0.0, 0.0, 1.0], "float")
|
|
|
|
assert (ts1 <= ser).values == pft.Series([1.0, 0.0, 1.0, 1.0], "float")
|
|
|
|
assert (ts1 == ser).values == pft.Series([0.0, 0.0, 1.0, 0.0], "float")
|
|
|
|
assert (ts1 != ser).values == pft.Series([1.0, 1.0, 0.0, 1.0], "float")
|
2022-04-11 16:49:17 +00:00
|
|
|
|
|
|
|
def test_tsc_comparison(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts1 = pft.TimeSeriesCore(self.data1, "M")
|
|
|
|
ts2 = pft.TimeSeriesCore(self.data2, "M")
|
2022-04-11 16:49:17 +00:00
|
|
|
|
2022-06-12 16:05:13 +00:00
|
|
|
assert (ts1 > ts2).values == pft.Series([0.0, 1.0, 0.0, 0.0], "float")
|
|
|
|
assert (ts1 >= ts2).values == pft.Series([0.0, 1.0, 1.0, 0.0], "float")
|
|
|
|
assert (ts1 < ts2).values == pft.Series([1.0, 0.0, 0.0, 1.0], "float")
|
|
|
|
assert (ts1 <= ts2).values == pft.Series([1.0, 0.0, 1.0, 1.0], "float")
|
|
|
|
assert (ts1 == ts2).values == pft.Series([0.0, 0.0, 1.0, 0.0], "float")
|
|
|
|
assert (ts1 != ts2).values == pft.Series([1.0, 1.0, 0.0, 1.0], "float")
|
2022-04-11 16:49:17 +00:00
|
|
|
|
|
|
|
def test_errors(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts1 = pft.TimeSeriesCore(self.data1, "M")
|
|
|
|
ts2 = pft.TimeSeriesCore(self.data2, "M")
|
|
|
|
ser = pft.Series([240, 210, 240], dtype="int")
|
|
|
|
ser2 = pft.Series(["2021-01-01", "2021-02-01", "2021-03-01", "2021-04-01"], dtype="date")
|
2022-04-11 16:49:17 +00:00
|
|
|
|
|
|
|
del ts2["2021-04-01"]
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
ts1 == "a"
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ts1 > ts2
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
ts1 == ser2
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ts1 <= ser
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
ts2 < [23, 24, 25, 26]
|
2022-04-12 06:13:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestTimeSeriesArithmatic:
|
|
|
|
data = [
|
|
|
|
("2021-01-01", 220),
|
|
|
|
("2021-02-01", 230),
|
|
|
|
("2021-03-01", 240),
|
|
|
|
("2021-04-01", 250),
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_add(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, "M")
|
2022-04-12 06:13:52 +00:00
|
|
|
ser = ts.values
|
|
|
|
|
|
|
|
num_add_ts = ts + 40
|
|
|
|
assert num_add_ts["2021-01-01"][1] == 260
|
|
|
|
assert num_add_ts["2021-04-01"][1] == 290
|
|
|
|
|
|
|
|
num_radd_ts = 40 + ts
|
|
|
|
assert num_radd_ts["2021-01-01"][1] == 260
|
|
|
|
assert num_radd_ts["2021-04-01"][1] == 290
|
|
|
|
|
|
|
|
ser_add_ts = ts + ser
|
|
|
|
assert ser_add_ts["2021-01-01"][1] == 440
|
|
|
|
assert ser_add_ts["2021-04-01"][1] == 500
|
2022-04-12 17:10:06 +00:00
|
|
|
|
|
|
|
ts_add_ts = ts + num_add_ts
|
|
|
|
assert ts_add_ts["2021-01-01"][1] == 480
|
|
|
|
assert ts_add_ts["2021-04-01"][1] == 540
|
|
|
|
|
|
|
|
def test_sub(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, "M")
|
|
|
|
ser = pft.Series([20, 30, 40, 50], "number")
|
2022-04-12 17:10:06 +00:00
|
|
|
|
|
|
|
num_sub_ts = ts - 40
|
|
|
|
assert num_sub_ts["2021-01-01"][1] == 180
|
|
|
|
assert num_sub_ts["2021-04-01"][1] == 210
|
|
|
|
|
|
|
|
num_rsub_ts = 240 - ts
|
|
|
|
assert num_rsub_ts["2021-01-01"][1] == 20
|
|
|
|
assert num_rsub_ts["2021-04-01"][1] == -10
|
|
|
|
|
|
|
|
ser_sub_ts = ts - ser
|
|
|
|
assert ser_sub_ts["2021-01-01"][1] == 200
|
|
|
|
assert ser_sub_ts["2021-04-01"][1] == 200
|
|
|
|
|
|
|
|
ts_sub_ts = ts - num_sub_ts
|
|
|
|
assert ts_sub_ts["2021-01-01"][1] == 40
|
|
|
|
assert ts_sub_ts["2021-04-01"][1] == 40
|
|
|
|
|
|
|
|
def test_truediv(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, "M")
|
|
|
|
ser = pft.Series([22, 23, 24, 25], "number")
|
2022-04-12 17:10:06 +00:00
|
|
|
|
|
|
|
num_div_ts = ts / 10
|
|
|
|
assert num_div_ts["2021-01-01"][1] == 22
|
|
|
|
assert num_div_ts["2021-04-01"][1] == 25
|
|
|
|
|
|
|
|
num_rdiv_ts = 1000 / ts
|
|
|
|
assert num_rdiv_ts["2021-04-01"][1] == 4
|
|
|
|
|
|
|
|
ser_div_ts = ts / ser
|
|
|
|
assert ser_div_ts["2021-01-01"][1] == 10
|
|
|
|
assert ser_div_ts["2021-04-01"][1] == 10
|
|
|
|
|
|
|
|
ts_div_ts = ts / num_div_ts
|
|
|
|
assert ts_div_ts["2021-01-01"][1] == 10
|
|
|
|
assert ts_div_ts["2021-04-01"][1] == 10
|
|
|
|
|
|
|
|
def test_floordiv(self):
|
2022-06-12 16:05:13 +00:00
|
|
|
ts = pft.TimeSeriesCore(self.data, "M")
|
|
|
|
ser = pft.Series([22, 23, 24, 25], "number")
|
2022-04-12 17:10:06 +00:00
|
|
|
|
|
|
|
num_div_ts = ts // 11
|
|
|
|
assert num_div_ts["2021-02-01"][1] == 20
|
|
|
|
assert num_div_ts["2021-04-01"][1] == 22
|
|
|
|
|
|
|
|
num_rdiv_ts = 1000 // ts
|
|
|
|
assert num_rdiv_ts["2021-01-01"][1] == 4
|
|
|
|
|
|
|
|
ser_div_ts = ts // ser
|
|
|
|
assert ser_div_ts["2021-01-01"][1] == 10
|
|
|
|
assert ser_div_ts["2021-04-01"][1] == 10
|