From 6a58e92a6926d2660a3a0664d812a83778c4a983 Mon Sep 17 00:00:00 2001 From: Daan Buekenhout <70980044+dbkhout@users.noreply.github.com> Date: Thu, 20 Jan 2022 20:37:26 +0100 Subject: [PATCH] Add support for datetime with zoneinfo tzinfo By converting input_datetime (string or datetime object) to a string before applying pandas' to_datetime, compatibility issues between pandas and zoneinfo are circumvented (https://github.com/pandas-dev/pandas/issues/37654). + fix typos and remove duplicate code --- src/pywaterinfo/waterinfo.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/pywaterinfo/waterinfo.py b/src/pywaterinfo/waterinfo.py index 3308026..3d16957 100755 --- a/src/pywaterinfo/waterinfo.py +++ b/src/pywaterinfo/waterinfo.py @@ -344,7 +344,7 @@ def _parse_date(input_datetime, timezone="UTC"): we normalize everything to UTC by default. Hence, we interpret the user input as UTC, provide the input to the API as CET and request the returned output data as UTC. If the user provides a timezone, we interpret user input as - the given timezone, doe the request in CET and return th output data in the + the given timezone, do the request in CET and return the output data in the requested timezone. Parameters @@ -359,16 +359,12 @@ def _parse_date(input_datetime, timezone="UTC"): f"{timezone} is not a valid timezone string." ) - input_timestamp = pd.to_datetime(input_datetime) + input_timestamp = pd.to_datetime(str(input_datetime)) - if input_timestamp.tz: # timestamp already contains tz info - return input_timestamp.tz_convert("CET").strftime("%Y-%m-%d %H:%M:%S") - else: - return ( - input_timestamp.tz_localize(timezone) - .tz_convert("CET") - .strftime("%Y-%m-%d %H:%M:%S") - ) + if not input_timestamp.tz: # timestamp does not contain tz info + input_timestamp = input_timestamp.tz_localize(timezone) + + return input_timestamp.tz_convert("CET").strftime("%Y-%m-%d %H:%M:%S") def _parse_period(self, start=None, end=None, period=None, timezone="UTC"): """Check the from/to/period arguments when requesting