Skip to content

Commit

Permalink
partial fix for python datetime conversions (fractional seconds curre…
Browse files Browse the repository at this point in the history
…ntly get dropped) (#1857)
  • Loading branch information
alexander-beedie committed Nov 22, 2021
1 parent b3a2c20 commit 45de79c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
5 changes: 2 additions & 3 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3440,8 +3440,7 @@ def to_python_datetime(self) -> Series:
"""
Go from Date/Datetime to python DateTime objects
"""

return (self.timestamp() // 1000).apply(
return (self.timestamp() / 1000).apply(
lambda ts: datetime.utcfromtimestamp(ts), Object
)

Expand Down Expand Up @@ -3510,7 +3509,7 @@ def _to_python_datetime(
return datetime.utcfromtimestamp(value * 3600 * 24).date()
elif dtype == Datetime:
# ms to seconds
return datetime.utcfromtimestamp(value // 1000)
return datetime.utcfromtimestamp(value / 1000)
else:
raise NotImplementedError

Expand Down
3 changes: 2 additions & 1 deletion py-polars/tests/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ def test_to_python_datetime() -> None:


def test_datetime_consistency() -> None:
dt = datetime(2021, 1, 1)
# dt = datetime(2021, 1, 1, 10, 30, 45, 123456)
dt = datetime(2021, 1, 1, 10, 30, 45, 123000)
df = pl.DataFrame({"date": [dt]})
assert df["date"].dt[0] == dt
assert df.select(pl.lit(dt))["literal"].dt[0] == dt
2 changes: 1 addition & 1 deletion py-polars/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_parquet_chunks() -> None:

def test_parquet_datetime() -> None:
"""
This failed because parquet writers cast datetimeto Date
This failed because parquet writers cast datetime to Date
"""
f = io.BytesIO()
data = {
Expand Down

0 comments on commit 45de79c

Please sign in to comment.