Skip to content

Fix bugs in neo4j.time.DateTime handling #1100

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

Merged
merged 2 commits into from
Oct 4, 2024
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
20 changes: 13 additions & 7 deletions src/neo4j/_async/work/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,7 @@ async def to_df(
consumed.
"""
import pandas as pd # type: ignore[import]
import pytz

if not expand:
df = pd.DataFrame(await self.values(), columns=self._keys)
Expand Down Expand Up @@ -933,14 +934,19 @@ async def to_df(
).all()
)
]

def datetime_to_timestamp(x):
if not x:
return pd.NaT
tzinfo = getattr(x, "tzinfo", None)
if tzinfo is None:
return pd.Timestamp(x.iso_format())
return pd.Timestamp(
x.astimezone(pytz.UTC).iso_format()
).astimezone(tzinfo)

df[dt_columns] = df[dt_columns].apply(
lambda col: col.map(
lambda x: pd.Timestamp(x.iso_format()).replace(
tzinfo=getattr(x, "tzinfo", None)
)
if x
else pd.NaT
)
lambda col: col.map(datetime_to_timestamp)
)
return df

Expand Down
20 changes: 13 additions & 7 deletions src/neo4j/_sync/work/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,7 @@ def to_df(
consumed.
"""
import pandas as pd # type: ignore[import]
import pytz

if not expand:
df = pd.DataFrame(self.values(), columns=self._keys)
Expand Down Expand Up @@ -933,14 +934,19 @@ def to_df(
).all()
)
]

def datetime_to_timestamp(x):
if not x:
return pd.NaT
tzinfo = getattr(x, "tzinfo", None)
if tzinfo is None:
return pd.Timestamp(x.iso_format())
return pd.Timestamp(
x.astimezone(pytz.UTC).iso_format()
).astimezone(tzinfo)

df[dt_columns] = df[dt_columns].apply(
lambda col: col.map(
lambda x: pd.Timestamp(x.iso_format()).replace(
tzinfo=getattr(x, "tzinfo", None)
)
if x
else pd.NaT
)
lambda col: col.map(datetime_to_timestamp)
)
return df

Expand Down
11 changes: 8 additions & 3 deletions src/neo4j/time/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2560,7 +2560,7 @@ def __ne__(self, other: object) -> bool:

Accepts :class:`.DateTime` and :class:`datetime.datetime`.
"""
if not isinstance(other, (Date, date)):
if not isinstance(other, (DateTime, datetime)):
return NotImplemented
return not self.__eq__(other)

Expand Down Expand Up @@ -2641,7 +2641,9 @@ def __gt__( # type: ignore[override]
def __add__(self, other: timedelta | Duration) -> DateTime:
"""Add a :class:`datetime.timedelta`."""
if isinstance(other, Duration):
t = self.to_clock_time() + ClockTime(
if other == (0, 0, 0, 0):
return self
t = self.time().to_clock_time() + ClockTime(
other.seconds, other.nanoseconds
)
days, seconds = symmetric_divmod(t.seconds, 86400)
Expand All @@ -2651,8 +2653,11 @@ def __add__(self, other: timedelta | Duration) -> DateTime:
time_ = Time.from_ticks(seconds * NANO_SECONDS + t.nanoseconds)
return self.combine(date_, time_).replace(tzinfo=self.tzinfo)
if isinstance(other, timedelta):
if other.total_seconds() == 0:
return self
t = self.to_clock_time() + ClockTime(
86400 * other.days + other.seconds, other.microseconds * 1000
86400 * other.days + other.seconds,
other.microseconds * 1000,
)
days, seconds = symmetric_divmod(t.seconds, 86400)
date_ = Date.from_ordinal(days + 1)
Expand Down
43 changes: 42 additions & 1 deletion tests/unit/async_/work/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from __future__ import annotations

import datetime
import logging
import typing as t
import uuid
Expand Down Expand Up @@ -1082,6 +1083,18 @@ async def test_to_df_expand(
assert df.equals(expected_df)


DTS_AROUND_SWEDISH_DST_CHANGE: tuple[datetime.datetime, ...] = (
datetime.datetime(2024, 3, 31, 0, 30, 0),
datetime.datetime(2024, 3, 31, 1, 30, 0),
datetime.datetime(2024, 3, 31, 2, 30, 0),
datetime.datetime(2024, 3, 31, 3, 30, 0),
datetime.datetime(2024, 10, 27, 0, 30, 0),
datetime.datetime(2024, 10, 27, 1, 30, 0),
datetime.datetime(2024, 10, 27, 2, 30, 0),
datetime.datetime(2024, 10, 27, 3, 30, 0),
)


@pytest.mark.parametrize(
("keys", "values", "expected_df"),
(
Expand Down Expand Up @@ -1209,7 +1222,7 @@ async def test_to_df_expand(
columns=["mixed"],
),
),
# Column with only None (should not be transfomred to NaT)
# Column with only None (should not be transformed to NaT)
(
["all_none"],
[
Expand Down Expand Up @@ -1252,6 +1265,34 @@ async def test_to_df_expand(
columns=["all_none", "mixed", "n"],
),
),
# Difficult timezones
*(
(
["dt_tz"],
[
[
pytz.UTC.localize(
neo4j_time.DateTime.from_native(dt)
).astimezone(pytz.timezone("Europe/Stockholm"))
+ neo4j_time.Duration(nanoseconds=add_ns)
]
for dt in DTS_AROUND_SWEDISH_DST_CHANGE
],
pd.DataFrame(
[
[
pytz.UTC.localize(pd.Timestamp(dt)).astimezone(
pytz.timezone("Europe/Stockholm")
)
+ pd.Timedelta(add_ns, unit="ns")
]
for dt in DTS_AROUND_SWEDISH_DST_CHANGE
],
columns=["dt_tz"],
),
)
for add_ns in (0, 1)
),
),
)
@pytest.mark.parametrize("expand", [True, False])
Expand Down
Loading