diff --git a/lilio/calendar.py b/lilio/calendar.py index cf18e1f..7fb6c56 100644 --- a/lilio/calendar.py +++ b/lilio/calendar.py @@ -472,9 +472,15 @@ def _infer_time_bound( left = input_data.index.min() freq = xr.infer_freq(input_data.index) if freq is not None: - time_delta = pd.Timedelta(freq) - right += time_delta / 2 - left -= time_delta / 2 + try: + time_delta = pd.to_timedelta(freq) + right += time_delta / 2 + left -= time_delta / 2 + except ValueError as exc: + if "w/o a number" in str(exc) or "only leading negative" in str(exc): + pass + else: + raise exc return left, right def map_to_data( diff --git a/tests/test_calendar.py b/tests/test_calendar.py index ebcf448..b3f5251 100644 --- a/tests/test_calendar.py +++ b/tests/test_calendar.py @@ -385,7 +385,7 @@ def test_map_to_data_edge_case_first_year(self): # test the edge value when the input covers the anchor date cal = daily_calendar(anchor="10-15", length="180d") # multiple years covered - time_index = pd.date_range("20191010", "20211225", freq="60d") + time_index = pd.date_range("20190101", "20211225", freq="60d") test_data = np.random.random(len(time_index)) timeseries = pd.Series(test_data, index=time_index) cal.map_to_data(timeseries) @@ -408,7 +408,7 @@ def test_map_to_data_edge_case_first_year(self): def test_map_to_data_input_time_backward(self): # test when the input data has reverse order time index cal = daily_calendar(anchor="10-15", length="180d") - time_index = pd.date_range("20201010", "20211225", freq="60d") + time_index = pd.date_range("20200101", "20211225", freq="60d") test_data = np.random.random(len(time_index)) timeseries = pd.Series(test_data, index=time_index[::-1]) cal.map_to_data(timeseries) @@ -427,7 +427,7 @@ def test_map_to_data_input_time_backward(self): def test_map_to_data_xarray_input(self): # test when the input data has reverse order time index cal = daily_calendar(anchor="10-15", length="180d") - time_index = pd.date_range("20201010", "20211225", freq="60d") + time_index = pd.date_range("20200101", "20211225", freq="60d") test_data = np.random.random(len(time_index)) dataarray = xr.DataArray(data=test_data, coords={"time": time_index}) cal.map_to_data(dataarray)