Skip to content

Commit

Permalink
Adjust to_numpy dtype based on Series time_unit (#2250)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhconradt committed Jan 2, 2022
1 parent cb5b36e commit bdaf4d1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1973,7 +1973,7 @@ def convert_to_date(arr): # type: ignore
if self.dtype == Date:
tp = "datetime64[D]"
else:
tp = "datetime64[ns]"
tp = f"datetime64[{self.time_unit}]"
return arr.astype(tp)

if _PYARROW_AVAILABLE and not self.is_datelike():
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,18 @@ def test_to_numpy() -> None:
s1 = pl.Series(
"datetime", [datetime(2021, 1, 2, 3, 4, 5), datetime(2021, 2, 3, 4, 5, 6)]
)
s2 = pl.date_range(
datetime(2021, 1, 1, 0), datetime(2021, 1, 1, 1), interval="1h", time_unit="ms"
)
assert str(s0.to_numpy()) == "['2308-04-02' '2746-02-20' '1973-05-28']"
assert (
str(s1.to_numpy()[:2])
== "['2021-01-02T03:04:05.000000000' '2021-02-03T04:05:06.000000000']"
)
assert (
str(s2.to_numpy()[:2])
== "['2021-01-01T00:00:00.000' '2021-01-01T01:00:00.000']"
)


def test_truncate() -> None:
Expand Down

0 comments on commit bdaf4d1

Please sign in to comment.