Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/int index warnings #777

Merged
merged 12 commits into from
Feb 15, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ def helper_test_index_types(self, ig: CovariateIndexGenerator):
idx = ig.generate_train_series(self.target_time, None)
self.assertTrue(isinstance(idx, pd.DatetimeIndex))

# pd.Int64Index
# pd.RangeIndex
idx = ig.generate_train_series(self.target_int, self.cov_int_train)
self.assertTrue(isinstance(idx, pd.Int64Index))
self.assertTrue(isinstance(idx, pd.RangeIndex))
idx = ig.generate_inference_series(
self.n_short, self.target_int, self.cov_int_inf_short
)
self.assertTrue(isinstance(idx, pd.Int64Index))
self.assertTrue(isinstance(idx, pd.RangeIndex))
idx = ig.generate_train_series(self.target_int, None)
self.assertTrue(isinstance(idx, pd.Int64Index))
self.assertTrue(isinstance(idx, pd.RangeIndex))

def helper_test_index_generator_train(self, ig: CovariateIndexGenerator):
"""
Expand Down
44 changes: 42 additions & 2 deletions darts/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ def test_integer_indexing(self):
)
)

# check the RangeIndex when indexing with a list
indexed_ts = series_int[[2, 3, 4, 5, 6]]
self.assertTrue(isinstance(indexed_ts.time_index, pd.RangeIndex))
self.assertTrue(
list(indexed_ts.time_index) == list(pd.RangeIndex(2, 7, step=1))
)

def test_column_names(self):
# test the column names resolution
columns_before = [
Expand Down Expand Up @@ -1388,7 +1395,7 @@ def test_from_dataframe_sunny_day(self):
self.assertEqual(data_darts1, data_darts3)

def test_time_col_convert_string_integers(self):
expected = np.random.randint(1, 100000, 10, int)
expected = np.array(list(range(3, 10)))
data_dict = {"Time": expected.astype(str)}
data_dict["Values1"] = np.random.uniform(
low=-10, high=10, size=len(data_dict["Time"])
Expand All @@ -1401,7 +1408,7 @@ def test_time_col_convert_string_integers(self):
self.assertEqual(ts.time_index.name, "Time")

def test_time_col_convert_integers(self):
expected = np.random.randint(1, 100000, 10, int)
expected = np.array(list(range(10)))
data_dict = {"Time": expected}
data_dict["Values1"] = np.random.uniform(
low=-10, high=10, size=len(data_dict["Time"])
Expand All @@ -1413,6 +1420,39 @@ def test_time_col_convert_integers(self):
self.assertEqual(ts.time_index.dtype, int)
self.assertEqual(ts.time_index.name, "Time")

def test_fail_with_bad_integer_time_col(self):
bad_time_col_vals = np.array([4, 0, 1, 2])
data_dict = {"Time": bad_time_col_vals}
data_dict["Values1"] = np.random.uniform(
low=-10, high=10, size=len(data_dict["Time"])
)
df = pd.DataFrame(data_dict)
with self.assertRaises(ValueError):
TimeSeries.from_dataframe(df=df, time_col="Time")

def test_time_col_convert_rangeindex(self):
expected_l = [4, 0, 2, 3, 1]
expected = np.array(expected_l)
data_dict = {"Time": expected}
data_dict["Values1"] = np.random.uniform(
low=-10, high=10, size=len(data_dict["Time"])
)
df = pd.DataFrame(data_dict)
ts = TimeSeries.from_dataframe(df=df, time_col="Time")

# check type (should convert to RangeIndex):
self.assertEqual(type(ts.time_index), pd.RangeIndex)

# check values inside the index (should be sorted correctly):
self.assertEqual(list(ts.time_index), sorted(expected))

# check that values are sorted accordingly:
ar1 = ts.values(copy=False)[:, 0]
ar2 = data_dict["Values1"][
list(expected_l.index(i) for i in range(len(expected)))
]
self.assertTrue(np.all(ar1 == ar2))

def test_time_col_convert_datetime(self):
expected = pd.date_range(start="20180501", end="20200301", freq="MS")
data_dict = {"Time": expected}
Expand Down
2 changes: 1 addition & 1 deletion darts/tests/utils/test_timeseries_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def test_routine(start, end=None, length=None, freq="D"):

for length_assert in [1, 2, 5, 10, 100]:
for start_pos in [0, 1]:
# pandas.Int64Index
# pandas.RangeIndex
start_assert, end_assert = start_pos, start_pos + length_assert - 1
test_routine(start=start_assert, length=length_assert, freq="")
test_routine(start=start_assert, length=length_assert, freq="D")
Expand Down