Skip to content

Commit

Permalink
Edit Domain - handle timezones when tranforming to datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Sep 5, 2022
1 parent fd70c60 commit 0e924a6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Orange/widgets/data/oweditdomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2828,7 +2828,10 @@ 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")
delta = dti - (dti.normalize() if only_time else initial_ts)
return (delta / pd.Timedelta("1s")).values


Expand Down
7 changes: 6 additions & 1 deletion Orange/widgets/data/tests/test_oweditdomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")],
]
Expand Down

0 comments on commit 0e924a6

Please sign in to comment.