From 7b40090a9ccd235bd84fa3eda1feed0e259621ab Mon Sep 17 00:00:00 2001 From: PrimozGodec Date: Fri, 23 Sep 2022 09:30:18 +0200 Subject: [PATCH] Edit domain - fix converting to time variable when timezone set --- Orange/widgets/data/oweditdomain.py | 10 +++++++++- Orange/widgets/data/tests/test_oweditdomain.py | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Orange/widgets/data/oweditdomain.py b/Orange/widgets/data/oweditdomain.py index ccb73061e05..c69450f60fb 100644 --- a/Orange/widgets/data/oweditdomain.py +++ b/Orange/widgets/data/oweditdomain.py @@ -2828,7 +2828,15 @@ def transform(self, c): def datetime_to_epoch(dti: pd.DatetimeIndex, only_time) -> np.ndarray: """Convert datetime to epoch""" - delta = dti - (dti.normalize() if only_time else pd.Timestamp("1970-01-01")) + # when dti has timezone info also the subtracted timestamp must have it + # otherwise subtracting fails + initial_ts = pd.Timestamp("1970-01-01", tz=None if dti.tz is None else "UTC") + # pandas in versions before 1.4 don't support subtracting different timezones + # remove next two lines when read-the-docs start supporting config files + # for subprojects, or they change default python version to 3.8 + if dti.tz is not None: + dti = dti.tz_convert("UTC") + delta = dti - (dti.normalize() if only_time else initial_ts) return (delta / pd.Timedelta("1s")).values diff --git a/Orange/widgets/data/tests/test_oweditdomain.py b/Orange/widgets/data/tests/test_oweditdomain.py index b4a7f7342d2..01b9d5fa445 100644 --- a/Orange/widgets/data/tests/test_oweditdomain.py +++ b/Orange/widgets/data/tests/test_oweditdomain.py @@ -921,13 +921,18 @@ def test_as_time(self): times = ( ["07.02.2022", "18.04.2021"], # date only ["07.02.2022 01:02:03", "18.04.2021 01:02:03"], # datetime + # datetime with timezone + ["2021-02-08 01:02:03+01:00", "2021-02-07 01:02:03+01:00"], ["010203", "010203"], # time ["02-07", "04-18"], ) - formats = ["25.11.2021", "25.11.2021 00:00:00", "000000", "11-25"] + formats = [ + "25.11.2021", "25.11.2021 00:00:00", "2021-11-25 00:00:00", "000000", "11-25" + ] expected = [ [d("2022-02-07"), d("2021-04-18")], [d("2022-02-07 01:02:03"), d("2021-04-18 01:02:03")], + [d("2021-02-08 01:02:03+0100"), d("2021-02-07 01:02:03+0100")], [d("01:02:03"), d("01:02:03")], [d("1900-02-07"), d("1900-04-18")], ] @@ -952,6 +957,16 @@ def test_as_time(self): np.array(list(chain(expected, expected)), dtype=float).transpose() ) + def test_raise_pandas_version(self): + """ + When this test start to fail: + - remove this test + - remove if clause in datetime_to_epoch function and supporting comments + - set pandas dependency version to pandas>=1.4 + """ + from datetime import datetime + self.assertLess(datetime.today(), datetime(2023, 1, 1)) + def test_reinterpret_string(self): table = self.data_str domain = table.domain