Skip to content

Commit c489c5e

Browse files
authored
Avoid pandas resample crash in Windows Python 3.12 forecast tests (#1566)
* Initial plan * test: avoid pandas resample in forecast dataset setup --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 38457fb commit c489c5e

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

test/automl/test_extra_models.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import unittest
55
import warnings
66
from collections import defaultdict
7+
from unittest import mock
78

89
import mlflow
910
import numpy as np
@@ -206,17 +207,45 @@ def load_multi_dataset():
206207
"https://raw.githubusercontent.com/srivatsan88/YouTubeLI/master/dataset/nyc_energy_consumption.csv"
207208
)
208209
# preprocessing data
209-
df["timeStamp"] = pd.to_datetime(df["timeStamp"])
210-
df = df.set_index("timeStamp")
211-
df = df.resample("D").mean()
210+
df["timeStamp"] = pd.to_datetime(df["timeStamp"]).dt.floor("D")
211+
df = df.groupby("timeStamp", as_index=False).mean(numeric_only=True)
212212
df["temp"] = df["temp"].ffill()
213213
df["precip"] = df["precip"].ffill()
214214
df = df[:-2] # last two rows are NaN for 'demand' column so remove them
215-
df = df.reset_index()
216215

217216
return df
218217

219218

219+
def test_load_multi_dataset_aggregates_without_resample():
220+
sample_df = pd.DataFrame(
221+
{
222+
"timeStamp": [
223+
"2024-01-01 01:00:00",
224+
"2024-01-01 10:00:00",
225+
"2024-01-02 12:00:00",
226+
"2024-01-03 12:00:00",
227+
"2024-01-04 12:00:00",
228+
],
229+
"demand": [10.0, 30.0, 20.0, np.nan, np.nan],
230+
"temp": [1.0, 3.0, np.nan, 5.0, 7.0],
231+
"precip": [0.0, 2.0, 4.0, np.nan, 8.0],
232+
}
233+
)
234+
235+
with mock.patch("pandas.read_csv", return_value=sample_df):
236+
df = load_multi_dataset()
237+
238+
expected = pd.DataFrame(
239+
{
240+
"timeStamp": pd.to_datetime(["2024-01-01", "2024-01-02"]),
241+
"demand": [20.0, 20.0],
242+
"temp": [2.0, 2.0],
243+
"precip": [1.0, 4.0],
244+
}
245+
)
246+
pd.testing.assert_frame_equal(df.reset_index(drop=True), expected)
247+
248+
220249
def _test_forecast(estimator_list, budget=10):
221250
if isinstance(estimator_list, str):
222251
estimator_list = [estimator_list]

test/automl/test_forecast.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,11 @@ def load_multi_dataset():
184184
"https://raw.githubusercontent.com/srivatsan88/YouTubeLI/master/dataset/nyc_energy_consumption.csv"
185185
)
186186
# preprocessing data
187-
df["timeStamp"] = pd.to_datetime(df["timeStamp"])
188-
df = df.set_index("timeStamp")
189-
df = df.resample("D").mean()
187+
df["timeStamp"] = pd.to_datetime(df["timeStamp"]).dt.floor("D")
188+
df = df.groupby("timeStamp", as_index=False).mean(numeric_only=True)
190189
df["temp"] = df["temp"].ffill()
191190
df["precip"] = df["precip"].ffill()
192191
df = df[:-2] # last two rows are NaN for 'demand' column so remove them
193-
df = df.reset_index()
194192

195193
return df
196194

0 commit comments

Comments
 (0)