Skip to content

Commit

Permalink
Edit domain - fix converting to time variable when timezone set
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Sep 26, 2022
1 parent 22565bd commit 7b40090
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 9 additions & 1 deletion Orange/widgets/data/oweditdomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
17 changes: 16 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 All @@ -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
Expand Down

0 comments on commit 7b40090

Please sign in to comment.