Skip to content

Commit

Permalink
order matters?
Browse files Browse the repository at this point in the history
  • Loading branch information
FBruzzesi committed Sep 12, 2024
1 parent 121f6f8 commit 4896df2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
17 changes: 9 additions & 8 deletions narwhals/_polars/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ def translate_dtype(dtype: Any) -> dtypes.DType:
return dtypes.Categorical()
if dtype == pl.Enum:
return dtypes.Enum()
if isinstance_or_issubclass(dtype, pl.Datetime):
time_unit: Literal["us", "ns", "ms"] = getattr(dtype, "time_unit", "us")
time_zone = getattr(dtype, "time_zone", None)
return dtypes.Datetime(time_unit=time_unit, time_zone=time_zone)
if dtype == pl.Duration:
return dtypes.Duration()
if dtype == pl.Date:
return dtypes.Date()
if isinstance_or_issubclass(dtype, pl.Datetime):
time_unit: Literal["us", "ns", "ms"] = getattr(dtype, "time_unit", "us")
time_zone = getattr(dtype, "time_zone", None)
return dtypes.Datetime(time_unit=time_unit, time_zone=time_zone)
return dtypes.Unknown()


Expand Down Expand Up @@ -107,12 +107,13 @@ def narwhals_to_native_dtype(dtype: dtypes.DType | type[dtypes.DType]) -> Any:
if dtype == dtypes.Enum:
msg = "Converting to Enum is not (yet) supported"
raise NotImplementedError(msg)
if isinstance_or_issubclass(dtype, dtypes.Datetime):
time_unit = getattr(dtype, "time_unit", "us")
time_zone = getattr(dtype, "time_zone", None)
return pl.Datetime(time_unit, time_zone)
if dtype == dtypes.Duration:
return pl.Duration()
if dtype == dtypes.Date:
return pl.Date()
if isinstance_or_issubclass(dtype, dtypes.Datetime):
time_unit = getattr(dtype, "time_unit", "us")
time_zone = getattr(dtype, "time_zone", None)
return pl.Datetime(time_unit, time_zone)

return pl.Unknown() # pragma: no cover
6 changes: 3 additions & 3 deletions tests/series_only/cast_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ def test_cast_date_datetime_pandas() -> None:
df = df.select(nw.col("a").cast(nw.Datetime))
result = nw.to_native(df)
expected = pd.DataFrame({"a": [datetime(2020, 1, 1), datetime(2020, 1, 2)]}).astype(
{"a": "timestamp[ns][pyarrow]"}
{"a": "timestamp[us][pyarrow]"}
)
pd.testing.assert_frame_equal(result, expected)

# pandas: pyarrow datetime to date
# # pandas: pyarrow datetime to date
dfpd = pd.DataFrame({"a": [datetime(2020, 1, 1), datetime(2020, 1, 2)]}).astype(
{"a": "timestamp[ns][pyarrow]"}
{"a": "timestamp[us][pyarrow]"}
)
df = nw.from_native(dfpd)
df = df.select(nw.col("a").cast(nw.Date))
Expand Down

0 comments on commit 4896df2

Please sign in to comment.