Skip to content

Commit 850a56c

Browse files
committed
Edit domain - fix converting to time variable when timezone set
1 parent 59ede28 commit 850a56c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

Orange/widgets/data/oweditdomain.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2828,7 +2828,15 @@ def transform(self, c):
28282828

28292829
def datetime_to_epoch(dti: pd.DatetimeIndex, only_time) -> np.ndarray:
28302830
"""Convert datetime to epoch"""
2831-
delta = dti - (dti.normalize() if only_time else pd.Timestamp("1970-01-01"))
2831+
# when dti has timezone info also the subtracted timestamp must have it
2832+
# otherwise subtracting fails
2833+
initial_ts = pd.Timestamp("1970-01-01", tz=None if dti.tz is None else "UTC")
2834+
# pandas in versions before 1.4 don't support subtracting different timezones
2835+
# remove next two lines when read-the-docs start supporting config files
2836+
# for subprojects, or they change default python version to 3.8
2837+
if dti.tz is not None:
2838+
dti = dti.tz_convert("UTC")
2839+
delta = dti - (dti.normalize() if only_time else initial_ts)
28322840
return (delta / pd.Timedelta("1s")).values
28332841

28342842

Orange/widgets/data/tests/test_oweditdomain.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,13 +921,18 @@ def test_as_time(self):
921921
times = (
922922
["07.02.2022", "18.04.2021"], # date only
923923
["07.02.2022 01:02:03", "18.04.2021 01:02:03"], # datetime
924+
# datetime with timezone
925+
["2021-02-08 01:02:03+01:00", "2021-02-07 01:02:03+01:00"],
924926
["010203", "010203"], # time
925927
["02-07", "04-18"],
926928
)
927-
formats = ["25.11.2021", "25.11.2021 00:00:00", "000000", "11-25"]
929+
formats = [
930+
"25.11.2021", "25.11.2021 00:00:00", "2021-11-25 00:00:00", "000000", "11-25"
931+
]
928932
expected = [
929933
[d("2022-02-07"), d("2021-04-18")],
930934
[d("2022-02-07 01:02:03"), d("2021-04-18 01:02:03")],
935+
[d("2021-02-08 01:02:03+0100"), d("2021-02-07 01:02:03+0100")],
931936
[d("01:02:03"), d("01:02:03")],
932937
[d("1900-02-07"), d("1900-04-18")],
933938
]
@@ -952,6 +957,15 @@ def test_as_time(self):
952957
np.array(list(chain(expected, expected)), dtype=float).transpose()
953958
)
954959

960+
def test_raise_pandas_version(self):
961+
"""
962+
When this test start to fail:
963+
- remove this test
964+
- remote if clause in datetime_to_epoch function and supporting comments
965+
"""
966+
from datetime import datetime
967+
self.assertLess(datetime.today(), datetime(2023, 1, 1))
968+
955969
def test_reinterpret_string(self):
956970
table = self.data_str
957971
domain = table.domain

0 commit comments

Comments
 (0)