Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Handle timezones in Edit Domain #6123

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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