From 4896df23a21a8e7f82c40a975f9d77e1cdeec54c Mon Sep 17 00:00:00 2001 From: FBruzzesi Date: Thu, 12 Sep 2024 23:09:31 +0200 Subject: [PATCH] order matters? --- narwhals/_polars/utils.py | 17 +++++++++-------- tests/series_only/cast_test.py | 6 +++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/narwhals/_polars/utils.py b/narwhals/_polars/utils.py index dc8696fa6..084651ac9 100644 --- a/narwhals/_polars/utils.py +++ b/narwhals/_polars/utils.py @@ -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() @@ -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 diff --git a/tests/series_only/cast_test.py b/tests/series_only/cast_test.py index 37ae55a01..db0242fb5 100644 --- a/tests/series_only/cast_test.py +++ b/tests/series_only/cast_test.py @@ -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))