From 434bd5e573e6a48ff4eb76d3b18e858206d4287f Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sat, 2 Nov 2024 11:13:34 +0000 Subject: [PATCH 01/12] fix: consistent to_numpy behaviour for tz-aware --- narwhals/_pandas_like/series.py | 28 ++++++++++++---------------- tests/conftest.py | 4 ++-- tests/series_only/to_numpy_test.py | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/narwhals/_pandas_like/series.py b/narwhals/_pandas_like/series.py index 35df78e2f..078e857b9 100644 --- a/narwhals/_pandas_like/series.py +++ b/narwhals/_pandas_like/series.py @@ -511,34 +511,30 @@ def to_numpy(self, dtype: Any = None, copy: bool | None = None) -> Any: # the default is meant to be None, but pandas doesn't allow it? # https://numpy.org/doc/stable/reference/generated/numpy.ndarray.__array__.html copy = copy or self._implementation is Implementation.CUDF + if self.dtype == self._dtypes.Datetime and self.dtype.time_zone is not None: # type: ignore[attr-defined] + s = self.dt.convert_time_zone("UTC").dt.replace_time_zone(None)._native_series + else: + s = self._native_series - has_missing = self._native_series.isna().any() - if ( - has_missing - and str(self._native_series.dtype) in PANDAS_TO_NUMPY_DTYPE_MISSING - ): + has_missing = s.isna().any() + if has_missing and str(s.dtype) in PANDAS_TO_NUMPY_DTYPE_MISSING: if self._implementation is Implementation.PANDAS and self._backend_version < ( 1, ): # pragma: no cover kwargs = {} else: kwargs = {"na_value": float("nan")} - return self._native_series.to_numpy( - dtype=dtype - or PANDAS_TO_NUMPY_DTYPE_MISSING[str(self._native_series.dtype)], + return s.to_numpy( + dtype=dtype or PANDAS_TO_NUMPY_DTYPE_MISSING[str(s.dtype)], copy=copy, **kwargs, ) - if ( - not has_missing - and str(self._native_series.dtype) in PANDAS_TO_NUMPY_DTYPE_NO_MISSING - ): - return self._native_series.to_numpy( - dtype=dtype - or PANDAS_TO_NUMPY_DTYPE_NO_MISSING[str(self._native_series.dtype)], + if not has_missing and str(s.dtype) in PANDAS_TO_NUMPY_DTYPE_NO_MISSING: + return s.to_numpy( + dtype=dtype or PANDAS_TO_NUMPY_DTYPE_NO_MISSING[str(s.dtype)], copy=copy, ) - return self._native_series.to_numpy(dtype=dtype, copy=copy) + return s.to_numpy(dtype=dtype, copy=copy) def to_pandas(self) -> Any: if self._implementation is Implementation.PANDAS: diff --git a/tests/conftest.py b/tests/conftest.py index d40d1027e..922d69e94 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,7 +3,6 @@ import contextlib from typing import TYPE_CHECKING from typing import Any -from typing import Callable import pandas as pd import polars as pl @@ -19,6 +18,7 @@ from narwhals.typing import IntoDataFrame from narwhals.typing import IntoFrame from tests.utils import Constructor + from tests.utils import ConstructorEager with contextlib.suppress(ImportError): import modin.pandas # noqa: F401 @@ -117,7 +117,7 @@ def pyarrow_table_constructor(obj: Any) -> IntoDataFrame: @pytest.fixture(params=eager_constructors) def constructor_eager( request: pytest.FixtureRequest, -) -> Callable[[Any], IntoDataFrame]: +) -> ConstructorEager: return request.param # type: ignore[no-any-return] diff --git a/tests/series_only/to_numpy_test.py b/tests/series_only/to_numpy_test.py index 966a44449..1e83bb2c7 100644 --- a/tests/series_only/to_numpy_test.py +++ b/tests/series_only/to_numpy_test.py @@ -1,5 +1,6 @@ from __future__ import annotations +from datetime import datetime from typing import TYPE_CHECKING import numpy as np @@ -30,3 +31,21 @@ def test_to_numpy( assert s.shape == (3,) assert_array_equal(s.to_numpy(), np.array(data, dtype=float)) + + +def test_to_numpy_tz_aware(constructor_eager: ConstructorEager) -> None: + df = nw.from_native( + constructor_eager({"a": [datetime(2020, 1, 1), datetime(2020, 1, 2)]}), + eager_only=True, + ) + df = df.select(nw.col("a").dt.replace_time_zone("Asia/Kathmandu")) + result = df["a"].to_numpy() + # for some reason, NumPy uses 'M' for datetimes + assert result.dtype.kind == "M" + assert ( + result + == np.array( + ["2019-12-31T18:15:00.000000", "2020-01-01T18:15:00.000000"], + dtype=result.dtype, + ) + ).all() From 75badf82e92d4ab8292661c608a01b3dd0e437c3 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sat, 2 Nov 2024 11:24:05 +0000 Subject: [PATCH 02/12] sort out dataframe.to_numpy too --- narwhals/_pandas_like/dataframe.py | 18 ++++++++++++++++-- tests/frame/to_numpy_test.py | 19 +++++++++++++++++++ tests/series_only/to_numpy_test.py | 7 ++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/narwhals/_pandas_like/dataframe.py b/narwhals/_pandas_like/dataframe.py index 7be808afd..f1e8ec904 100644 --- a/narwhals/_pandas_like/dataframe.py +++ b/narwhals/_pandas_like/dataframe.py @@ -701,18 +701,32 @@ def to_numpy(self, dtype: Any = None, copy: bool | None = None) -> Any: if dtype is not None: return self._native_frame.to_numpy(dtype=dtype, copy=copy) + convert = set() + for key, val in self.schema.items(): + if val == self._dtypes.Datetime and val.time_zone is not None: # type: ignore[attr-defined] + convert.add(key) + df = self.with_columns( + *[ + self.__narwhals_namespace__() + .col(x) + .dt.convert_time_zone("UTC") + .dt.replace_time_zone(None) + for x in convert + ] + )._native_frame + # pandas return `object` dtype for nullable dtypes if dtype=None, # so we cast each Series to numpy and let numpy find a common dtype. # If there aren't any dtypes where `to_numpy()` is "broken" (i.e. it # returns Object) then we just call `to_numpy()` on the DataFrame. - for col_dtype in self._native_frame.dtypes: + for col_dtype in df.dtypes: if str(col_dtype) in PANDAS_TO_NUMPY_DTYPE_MISSING: import numpy as np # ignore-banned-import return np.hstack( [self[col].to_numpy(copy=copy)[:, None] for col in self.columns] ) - return self._native_frame.to_numpy(copy=copy) + return df.to_numpy(copy=copy) def to_pandas(self) -> Any: if self._implementation is Implementation.PANDAS: diff --git a/tests/frame/to_numpy_test.py b/tests/frame/to_numpy_test.py index aa3dfc2e4..8da5ffc83 100644 --- a/tests/frame/to_numpy_test.py +++ b/tests/frame/to_numpy_test.py @@ -1,5 +1,6 @@ from __future__ import annotations +from datetime import datetime from typing import TYPE_CHECKING import numpy as np @@ -18,3 +19,21 @@ def test_to_numpy(constructor_eager: ConstructorEager) -> None: expected = np.array([[1, 3, 2], [4, 4, 6], [7.1, 8, 9]]).T np.testing.assert_array_equal(result, expected) assert result.dtype == "float64" + + +def test_to_numpy_tz_aware(constructor_eager: ConstructorEager) -> None: + df = nw.from_native( + constructor_eager({"a": [datetime(2020, 1, 1), datetime(2020, 1, 2)]}), + eager_only=True, + ) + df = df.select(nw.col("a").dt.replace_time_zone("Asia/Kathmandu")) + result = df.to_numpy() + # for some reason, NumPy uses 'M' for datetimes + assert result.dtype.kind == "M" + assert ( + result + == np.array( + [["2019-12-31T18:15:00.000000"], ["2020-01-01T18:15:00.000000"]], + dtype=result.dtype, + ) + ).all() diff --git a/tests/series_only/to_numpy_test.py b/tests/series_only/to_numpy_test.py index 1e83bb2c7..e4a5b1360 100644 --- a/tests/series_only/to_numpy_test.py +++ b/tests/series_only/to_numpy_test.py @@ -8,6 +8,7 @@ from numpy.testing import assert_array_equal import narwhals.stable.v1 as nw +from tests.utils import PYARROW_VERSION if TYPE_CHECKING: from tests.utils import ConstructorEager @@ -33,7 +34,11 @@ def test_to_numpy( assert_array_equal(s.to_numpy(), np.array(data, dtype=float)) -def test_to_numpy_tz_aware(constructor_eager: ConstructorEager) -> None: +def test_to_numpy_tz_aware( + constructor_eager: ConstructorEager, request: pytest.FixtureRequest +) -> None: + if "pyarrow_table" in str(constructor_eager) and PYARROW_VERSION < (12,): + request.applymarker(pytest.mark.xfail) df = nw.from_native( constructor_eager({"a": [datetime(2020, 1, 1), datetime(2020, 1, 2)]}), eager_only=True, From 461244bb54629adced59efbf06a85c5e364278bd Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sat, 2 Nov 2024 11:25:04 +0000 Subject: [PATCH 03/12] sort out dataframe.to_numpy too --- narwhals/_pandas_like/dataframe.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/narwhals/_pandas_like/dataframe.py b/narwhals/_pandas_like/dataframe.py index f1e8ec904..b9080322d 100644 --- a/narwhals/_pandas_like/dataframe.py +++ b/narwhals/_pandas_like/dataframe.py @@ -701,17 +701,14 @@ def to_numpy(self, dtype: Any = None, copy: bool | None = None) -> Any: if dtype is not None: return self._native_frame.to_numpy(dtype=dtype, copy=copy) - convert = set() - for key, val in self.schema.items(): - if val == self._dtypes.Datetime and val.time_zone is not None: # type: ignore[attr-defined] - convert.add(key) df = self.with_columns( *[ self.__narwhals_namespace__() - .col(x) + .col(key) .dt.convert_time_zone("UTC") .dt.replace_time_zone(None) - for x in convert + for key, val in self.schema.items() + if val == self._dtypes.Datetime and val.time_zone is not None # type: ignore[attr-defined] ] )._native_frame From 70809e5f1a80d9d7cdf9815f91cc3590df2dafb5 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sat, 2 Nov 2024 11:25:49 +0000 Subject: [PATCH 04/12] sort out dataframe.to_numpy too --- narwhals/_pandas_like/dataframe.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/narwhals/_pandas_like/dataframe.py b/narwhals/_pandas_like/dataframe.py index b9080322d..08bb88d58 100644 --- a/narwhals/_pandas_like/dataframe.py +++ b/narwhals/_pandas_like/dataframe.py @@ -698,9 +698,6 @@ def to_numpy(self, dtype: Any = None, copy: bool | None = None) -> Any: # pandas default differs from Polars, but cuDF default is True copy = self._implementation is Implementation.CUDF - if dtype is not None: - return self._native_frame.to_numpy(dtype=dtype, copy=copy) - df = self.with_columns( *[ self.__narwhals_namespace__() @@ -712,6 +709,9 @@ def to_numpy(self, dtype: Any = None, copy: bool | None = None) -> Any: ] )._native_frame + if dtype is not None: + return df.to_numpy(dtype=dtype, copy=copy) + # pandas return `object` dtype for nullable dtypes if dtype=None, # so we cast each Series to numpy and let numpy find a common dtype. # If there aren't any dtypes where `to_numpy()` is "broken" (i.e. it From 5c030026a06baf9cb81d101a6a30a5f4400c6090 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sat, 2 Nov 2024 11:31:04 +0000 Subject: [PATCH 05/12] sort out dataframe.to_numpy too --- tests/frame/to_numpy_test.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/frame/to_numpy_test.py b/tests/frame/to_numpy_test.py index 8da5ffc83..7dbc438e8 100644 --- a/tests/frame/to_numpy_test.py +++ b/tests/frame/to_numpy_test.py @@ -4,8 +4,10 @@ from typing import TYPE_CHECKING import numpy as np +import pytest import narwhals.stable.v1 as nw +from tests.utils import PYARROW_VERSION if TYPE_CHECKING: from tests.utils import ConstructorEager @@ -21,7 +23,11 @@ def test_to_numpy(constructor_eager: ConstructorEager) -> None: assert result.dtype == "float64" -def test_to_numpy_tz_aware(constructor_eager: ConstructorEager) -> None: +def test_to_numpy_tz_aware( + constructor_eager: ConstructorEager, request: pytest.FixtureRequest +) -> None: + if "pyarrow_table" in str(constructor_eager) and PYARROW_VERSION < (12,): + request.applymarker(pytest.mark.xfail) df = nw.from_native( constructor_eager({"a": [datetime(2020, 1, 1), datetime(2020, 1, 2)]}), eager_only=True, From 5975a48cea537050d71b643a1cfb8e1f294e65c3 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sat, 2 Nov 2024 11:33:02 +0000 Subject: [PATCH 06/12] sort out dataframe.to_numpy too --- narwhals/_pandas_like/dataframe.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/narwhals/_pandas_like/dataframe.py b/narwhals/_pandas_like/dataframe.py index 08bb88d58..cd3f82d79 100644 --- a/narwhals/_pandas_like/dataframe.py +++ b/narwhals/_pandas_like/dataframe.py @@ -698,16 +698,22 @@ def to_numpy(self, dtype: Any = None, copy: bool | None = None) -> Any: # pandas default differs from Polars, but cuDF default is True copy = self._implementation is Implementation.CUDF - df = self.with_columns( - *[ - self.__narwhals_namespace__() - .col(key) - .dt.convert_time_zone("UTC") - .dt.replace_time_zone(None) - for key, val in self.schema.items() - if val == self._dtypes.Datetime and val.time_zone is not None # type: ignore[attr-defined] - ] - )._native_frame + if any( + dtype == self._dtypes.Datetime and dtype.time_zone is not None # type: ignore[attr-defined] + for dtype in self.schema.values() + ): + df = self.with_columns( + *[ + self.__narwhals_namespace__() + .col(key) + .dt.convert_time_zone("UTC") + .dt.replace_time_zone(None) + for key, val in self.schema.items() + if val == self._dtypes.Datetime and val.time_zone is not None # type: ignore[attr-defined] + ] + )._native_frame + else: + df = self._native_frame if dtype is not None: return df.to_numpy(dtype=dtype, copy=copy) From d60ab1cb103941adb0fbf06d98593e696754aeb1 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sat, 2 Nov 2024 11:34:18 +0000 Subject: [PATCH 07/12] sort out dataframe.to_numpy too --- narwhals/_pandas_like/dataframe.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/narwhals/_pandas_like/dataframe.py b/narwhals/_pandas_like/dataframe.py index cd3f82d79..f5497f728 100644 --- a/narwhals/_pandas_like/dataframe.py +++ b/narwhals/_pandas_like/dataframe.py @@ -698,18 +698,18 @@ def to_numpy(self, dtype: Any = None, copy: bool | None = None) -> Any: # pandas default differs from Polars, but cuDF default is True copy = self._implementation is Implementation.CUDF - if any( - dtype == self._dtypes.Datetime and dtype.time_zone is not None # type: ignore[attr-defined] - for dtype in self.schema.values() - ): + to_convert = set() + for key, val in self.schema.items(): + if val == self._dtypes.Datetime and val.time_zone is not None: # type: ignore[attr-defined] + to_convert.add(key) + if to_convert: df = self.with_columns( *[ self.__narwhals_namespace__() .col(key) .dt.convert_time_zone("UTC") .dt.replace_time_zone(None) - for key, val in self.schema.items() - if val == self._dtypes.Datetime and val.time_zone is not None # type: ignore[attr-defined] + for key in to_convert ] )._native_frame else: From f47b9f287aeffc51fba14191860721c7fc198645 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sat, 2 Nov 2024 11:39:52 +0000 Subject: [PATCH 08/12] version fixup --- tests/frame/to_numpy_test.py | 5 ++++- tests/series_only/to_numpy_test.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/frame/to_numpy_test.py b/tests/frame/to_numpy_test.py index 7dbc438e8..9b8d19eb9 100644 --- a/tests/frame/to_numpy_test.py +++ b/tests/frame/to_numpy_test.py @@ -7,6 +7,7 @@ import pytest import narwhals.stable.v1 as nw +from tests.utils import PANDAS_VERSION from tests.utils import PYARROW_VERSION if TYPE_CHECKING: @@ -26,7 +27,9 @@ def test_to_numpy(constructor_eager: ConstructorEager) -> None: def test_to_numpy_tz_aware( constructor_eager: ConstructorEager, request: pytest.FixtureRequest ) -> None: - if "pyarrow_table" in str(constructor_eager) and PYARROW_VERSION < (12,): + if ("pyarrow_table" in str(constructor_eager) and PYARROW_VERSION < (12,)) or ( + "pandas_pyarrow" in str(constructor_eager) and PANDAS_VERSION < (2, 2) + ): request.applymarker(pytest.mark.xfail) df = nw.from_native( constructor_eager({"a": [datetime(2020, 1, 1), datetime(2020, 1, 2)]}), diff --git a/tests/series_only/to_numpy_test.py b/tests/series_only/to_numpy_test.py index e4a5b1360..c8c72be3a 100644 --- a/tests/series_only/to_numpy_test.py +++ b/tests/series_only/to_numpy_test.py @@ -8,6 +8,7 @@ from numpy.testing import assert_array_equal import narwhals.stable.v1 as nw +from tests.utils import PANDAS_VERSION from tests.utils import PYARROW_VERSION if TYPE_CHECKING: @@ -37,7 +38,9 @@ def test_to_numpy( def test_to_numpy_tz_aware( constructor_eager: ConstructorEager, request: pytest.FixtureRequest ) -> None: - if "pyarrow_table" in str(constructor_eager) and PYARROW_VERSION < (12,): + if ("pyarrow_table" in str(constructor_eager) and PYARROW_VERSION < (12,)) or ( + "pandas_pyarrow" in str(constructor_eager) and PANDAS_VERSION < (2, 2) + ): request.applymarker(pytest.mark.xfail) df = nw.from_native( constructor_eager({"a": [datetime(2020, 1, 1), datetime(2020, 1, 2)]}), From c5d8836945771b3054e8951d9e48c6a470f43430 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sat, 2 Nov 2024 11:55:11 +0000 Subject: [PATCH 09/12] winzoz --- tests/frame/to_numpy_test.py | 10 ++++++++-- tests/series_only/to_numpy_test.py | 11 +++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/frame/to_numpy_test.py b/tests/frame/to_numpy_test.py index 9b8d19eb9..0b631a3db 100644 --- a/tests/frame/to_numpy_test.py +++ b/tests/frame/to_numpy_test.py @@ -9,6 +9,7 @@ import narwhals.stable.v1 as nw from tests.utils import PANDAS_VERSION from tests.utils import PYARROW_VERSION +from tests.utils import is_windows if TYPE_CHECKING: from tests.utils import ConstructorEager @@ -27,8 +28,13 @@ def test_to_numpy(constructor_eager: ConstructorEager) -> None: def test_to_numpy_tz_aware( constructor_eager: ConstructorEager, request: pytest.FixtureRequest ) -> None: - if ("pyarrow_table" in str(constructor_eager) and PYARROW_VERSION < (12,)) or ( - "pandas_pyarrow" in str(constructor_eager) and PANDAS_VERSION < (2, 2) + if ( + ("pyarrow_table" in str(constructor_eager) and PYARROW_VERSION < (12,)) + or ("pandas_pyarrow" in str(constructor_eager) and PANDAS_VERSION < (2, 2)) + or ( + any(x in str(constructor_eager) for x in ("pyarrow", "modin")) + and is_windows() + ) ): request.applymarker(pytest.mark.xfail) df = nw.from_native( diff --git a/tests/series_only/to_numpy_test.py b/tests/series_only/to_numpy_test.py index c8c72be3a..8e36ac128 100644 --- a/tests/series_only/to_numpy_test.py +++ b/tests/series_only/to_numpy_test.py @@ -10,6 +10,7 @@ import narwhals.stable.v1 as nw from tests.utils import PANDAS_VERSION from tests.utils import PYARROW_VERSION +from tests.utils import is_windows if TYPE_CHECKING: from tests.utils import ConstructorEager @@ -38,10 +39,16 @@ def test_to_numpy( def test_to_numpy_tz_aware( constructor_eager: ConstructorEager, request: pytest.FixtureRequest ) -> None: - if ("pyarrow_table" in str(constructor_eager) and PYARROW_VERSION < (12,)) or ( - "pandas_pyarrow" in str(constructor_eager) and PANDAS_VERSION < (2, 2) + if ( + ("pyarrow_table" in str(constructor_eager) and PYARROW_VERSION < (12,)) + or ("pandas_pyarrow" in str(constructor_eager) and PANDAS_VERSION < (2, 2)) + or ( + any(x in str(constructor_eager) for x in ("pyarrow", "modin")) + and is_windows() + ) ): request.applymarker(pytest.mark.xfail) + request.applymarker(pytest.mark.xfail) df = nw.from_native( constructor_eager({"a": [datetime(2020, 1, 1), datetime(2020, 1, 2)]}), eager_only=True, From b393e4282a106ab57099197e5df777e3403323ff Mon Sep 17 00:00:00 2001 From: Marco Edward Gorelli Date: Sat, 2 Nov 2024 13:13:05 +0000 Subject: [PATCH 10/12] Apply suggestions from code review Co-authored-by: Francesco Bruzzesi <42817048+FBruzzesi@users.noreply.github.com> --- narwhals/_pandas_like/dataframe.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/narwhals/_pandas_like/dataframe.py b/narwhals/_pandas_like/dataframe.py index f5497f728..1c90eca79 100644 --- a/narwhals/_pandas_like/dataframe.py +++ b/narwhals/_pandas_like/dataframe.py @@ -698,19 +698,16 @@ def to_numpy(self, dtype: Any = None, copy: bool | None = None) -> Any: # pandas default differs from Polars, but cuDF default is True copy = self._implementation is Implementation.CUDF - to_convert = set() - for key, val in self.schema.items(): - if val == self._dtypes.Datetime and val.time_zone is not None: # type: ignore[attr-defined] - to_convert.add(key) + to_convert = { + key for key, val in self.schema.items() + if val == self._dtypes.Datetime and val.time_zone is not None # type: ignore[attr-defined] + } if to_convert: df = self.with_columns( - *[ self.__narwhals_namespace__() - .col(key) + .col(list(to_convert)) .dt.convert_time_zone("UTC") .dt.replace_time_zone(None) - for key in to_convert - ] )._native_frame else: df = self._native_frame From c0ae6844abbddbd6c3b953c204b3f237a78cc17d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 2 Nov 2024 13:13:38 +0000 Subject: [PATCH 11/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- narwhals/_pandas_like/dataframe.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/narwhals/_pandas_like/dataframe.py b/narwhals/_pandas_like/dataframe.py index 1c90eca79..6040da84b 100644 --- a/narwhals/_pandas_like/dataframe.py +++ b/narwhals/_pandas_like/dataframe.py @@ -699,15 +699,16 @@ def to_numpy(self, dtype: Any = None, copy: bool | None = None) -> Any: copy = self._implementation is Implementation.CUDF to_convert = { - key for key, val in self.schema.items() + key + for key, val in self.schema.items() if val == self._dtypes.Datetime and val.time_zone is not None # type: ignore[attr-defined] - } + } if to_convert: df = self.with_columns( - self.__narwhals_namespace__() - .col(list(to_convert)) - .dt.convert_time_zone("UTC") - .dt.replace_time_zone(None) + self.__narwhals_namespace__() + .col(list(to_convert)) + .dt.convert_time_zone("UTC") + .dt.replace_time_zone(None) )._native_frame else: df = self._native_frame From 123afb17217be8f6f727125b22d3af02803dfbfc Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sat, 2 Nov 2024 13:16:50 +0000 Subject: [PATCH 12/12] fixup --- narwhals/_pandas_like/dataframe.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/narwhals/_pandas_like/dataframe.py b/narwhals/_pandas_like/dataframe.py index 6040da84b..796386fad 100644 --- a/narwhals/_pandas_like/dataframe.py +++ b/narwhals/_pandas_like/dataframe.py @@ -698,15 +698,15 @@ def to_numpy(self, dtype: Any = None, copy: bool | None = None) -> Any: # pandas default differs from Polars, but cuDF default is True copy = self._implementation is Implementation.CUDF - to_convert = { + to_convert = [ key for key, val in self.schema.items() if val == self._dtypes.Datetime and val.time_zone is not None # type: ignore[attr-defined] - } + ] if to_convert: df = self.with_columns( self.__narwhals_namespace__() - .col(list(to_convert)) + .col(*to_convert) .dt.convert_time_zone("UTC") .dt.replace_time_zone(None) )._native_frame