Skip to content

Commit

Permalink
Fix user requested timezones with mixed offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhoey committed Aug 16, 2024
1 parent 169c286 commit f56e407
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/pywaterinfo/waterinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,9 @@ def get_timeseries_values(
df[key_name] = section[key_name]
# convert datetime objects to Pandas timestamp
if "Timestamp" in df.columns:
df["Timestamp"] = pd.to_datetime(df["Timestamp"])
# round trip via UTC to handle mixed time series
df["Timestamp"] = pd.to_datetime(
df["Timestamp"], utc=True).dt.tz_convert(timezone)
time_series.append(df)

return pd.concat(time_series)
Expand Down
21 changes: 17 additions & 4 deletions tests/test_waterinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,23 @@ def test_overwrite_timezone(self, connection, request):
end="20190501 14:10",
timezone="CET",
)
assert is_datetime64tz_dtype(df["Timestamp"])
assert df["Timestamp"].dt.tz == datetime.timezone(
datetime.timedelta(seconds=7200)
)
assert isinstance(df["Timestamp"].dtype, pd.DatetimeTZDtype)
assert df["Timestamp"].dt.tz == pytz.timezone("CET")

def test_timezone_mixed_timezone(self, connection, request):
"""Queries resulting in mixed timezone offsets are handled without warning
See also https://github.com/fluves/pywaterinfo/issues/67
"""
connection = request.getfixturevalue(connection)
df = connection.get_timeseries_values('69928042',
start='2013-03-21 00:00:00',
end='2013-04-01 23:00:00',
timezone='CET',
returnfields="Timestamp,Value"
)
assert isinstance(df["Timestamp"].dtype, pd.DatetimeTZDtype)
assert df["Timestamp"].dt.tz == pytz.timezone("CET")

def test_start_end_timezone(self, connection, request):
"""pywaterinfo can handle start/end dates already containing tz info"""
Expand Down

0 comments on commit f56e407

Please sign in to comment.