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

Make DatetimeIndex timezone naive for compatibility with xarray/numpy engine #1343

Merged
merged 5 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions darts/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,55 @@ def test_time_col_convert_datetime_strings(self):
self.assertEqual(ts.time_index.dtype, "datetime64[ns]")
self.assertEqual(ts.time_index.name, "Time")

def test_time_col_with_tz(self):
# numpy and xarray don't support "timezone aware" pd.DatetimeIndex
# the BUGFIX removes timezone information without conversion

time_range_MS = pd.date_range(
start="20180501", end="20200301", freq="MS", tz="CET"
)
values = np.random.uniform(low=-10, high=10, size=len(time_range_MS))
# pd.DataFrame loses the tz information unless it is contained in its index
# (other columns are silently converted to UTC, with tz attribute set to None)
df = pd.DataFrame(data=values, index=time_range_MS)
ts = TimeSeries.from_dataframe(df=df)
self.assertEqual(list(ts.time_index), list(time_range_MS.tz_localize(None)))
self.assertEqual(list(ts.time_index.tz_localize("CET")), list(time_range_MS))
self.assertEqual(ts.time_index.tz, None)

serie = pd.Series(data=values, index=time_range_MS)
ts = TimeSeries.from_series(pd_series=serie)
self.assertEqual(list(ts.time_index), list(time_range_MS.tz_localize(None)))
self.assertEqual(list(ts.time_index.tz_localize("CET")), list(time_range_MS))
self.assertEqual(ts.time_index.tz, None)

ts = TimeSeries.from_times_and_values(times=time_range_MS, values=values)
self.assertEqual(list(ts.time_index), list(time_range_MS.tz_localize(None)))
self.assertEqual(list(ts.time_index.tz_localize("CET")), list(time_range_MS))
self.assertEqual(ts.time_index.tz, None)

time_range_H = pd.date_range(
start="20200518", end="20200521", freq="H", tz="CET"
)
values = np.random.uniform(low=-10, high=10, size=len(time_range_H))

df = pd.DataFrame(data=values, index=time_range_H)
ts = TimeSeries.from_dataframe(df=df)
self.assertEqual(list(ts.time_index), list(time_range_H.tz_localize(None)))
self.assertEqual(list(ts.time_index.tz_localize("CET")), list(time_range_H))
self.assertEqual(ts.time_index.tz, None)

serie = pd.Series(data=values, index=time_range_H)
ts = TimeSeries.from_series(pd_series=serie)
self.assertEqual(list(ts.time_index), list(time_range_H.tz_localize(None)))
self.assertEqual(list(ts.time_index.tz_localize("CET")), list(time_range_H))
self.assertEqual(ts.time_index.tz, None)

ts = TimeSeries.from_times_and_values(times=time_range_H, values=values)
self.assertEqual(list(ts.time_index), list(time_range_H.tz_localize(None)))
self.assertEqual(list(ts.time_index.tz_localize("CET")), list(time_range_H))
self.assertEqual(ts.time_index.tz, None)

def test_time_col_convert_garbage(self):
expected = [
"2312312asdfdw",
Expand Down
29 changes: 28 additions & 1 deletion darts/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,17 @@ def from_dataframe(
"a DatetimeIndex, or with a RangeIndex.",
logger,
)
time_index = df.index
# BUGFIX : force time-index to be timezone naive as xarray doesn't support it
# pandas.DataFrame loses the tz information if it's not its index
if isinstance(df.index, pd.DatetimeIndex) and df.index.tz is not None:
print(
madtoinou marked this conversation as resolved.
Show resolved Hide resolved
"The provided DatetimeIndex was associated with a timezone, which is currently not supported "
"by xarray. To avoid unexpected behaviour, the tz information was removed, don't forget to "
madtoinou marked this conversation as resolved.
Show resolved Hide resolved
f"call `ts.time_index.tz_localize({df.index.tz})` when exporting the results."
madtoinou marked this conversation as resolved.
Show resolved Hide resolved
)
time_index = df.index.tz_localize(None)
else:
time_index = df.index

if not time_index.name:
time_index.name = time_col if time_col else DIMS[0]
Expand Down Expand Up @@ -831,6 +841,14 @@ def from_series(
TimeSeries
A univariate and deterministic TimeSeries constructed from the inputs.
"""
# BUGFIX : force time-index to be timezone naive as xarray doesn't support it
if isinstance(pd_series, pd.DatetimeIndex) and pd_series.dt.tz is not None:
madtoinou marked this conversation as resolved.
Show resolved Hide resolved
print(
"The `times` argument was associated with a timezone, which is currently not supported "
"by xarray. To avoid unexpected behaviour, the tz information was removed, don't forget to "
f"call `ts.time_index.tz_localize({pd_series.dt.tz})` when exporting the results."
)
pd_series = pd_series.dt.tz_localize(None)

df = pd.DataFrame(pd_series)
return cls.from_dataframe(
Expand Down Expand Up @@ -924,6 +942,15 @@ def from_times_and_values(
"TimeSeries.from_values() if you want to use an automatic RangeIndex.",
)

# BUGFIX : force time-index to be timezone naive as xarray doesn't support it
if isinstance(times, pd.DatetimeIndex) and times.tz is not None:
print(
"The `times` argument was associated with a timezone, which is currently not supported "
"by xarray. To avoid unexpected behaviour, the tz information was removed, don't forget to "
f"call `ts.time_index.tz_localize({times.tz})` when exporting the results."
)
times = times.tz_localize(None)

times_name = DIMS[0] if not times.name else times.name

# avoid copying if data is already np.ndarray:
Expand Down