From ac368016930eb3ef83dd73cc1de3fe9bf0617bdf Mon Sep 17 00:00:00 2001 From: Marco Edward Gorelli Date: Tue, 4 Jun 2024 10:59:38 +0100 Subject: [PATCH] fixup cast to string for pandas (#254) --- narwhals/_pandas_like/utils.py | 3 +-- tests/series/cast_test.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/series/cast_test.py diff --git a/narwhals/_pandas_like/utils.py b/narwhals/_pandas_like/utils.py index 74dd96823..37c69173d 100644 --- a/narwhals/_pandas_like/utils.py +++ b/narwhals/_pandas_like/utils.py @@ -437,7 +437,6 @@ def reverse_translate_dtype( # noqa: PLR0915 from narwhals import dtypes dtype_backend = get_dtype_backend(starting_dtype, implementation) - if isinstance_or_issubclass(dtype, dtypes.Float64): if dtype_backend == "pyarrow-nullable": return "Float64[pyarrow]" @@ -514,7 +513,7 @@ def reverse_translate_dtype( # noqa: PLR0915 if dtype_backend == "pandas-nullable": return "string" else: - return object + return str if isinstance_or_issubclass(dtype, dtypes.Boolean): if dtype_backend == "pyarrow-nullable": return "boolean[pyarrow]" diff --git a/tests/series/cast_test.py b/tests/series/cast_test.py new file mode 100644 index 000000000..23bdf8e41 --- /dev/null +++ b/tests/series/cast_test.py @@ -0,0 +1,18 @@ +import pandas as pd +import polars as pl + +import narwhals as nw + + +def test_cast_253() -> None: + df_polars = pl.DataFrame({"a": [1]}) + result = nw.from_native(df_polars, eager_only=True).select( + nw.col("a").cast(nw.String) + "hi" + )["a"][0] + assert result == "1hi" + + df_pandas = pd.DataFrame({"a": [1]}) + result = nw.from_native(df_pandas, eager_only=True).select( + nw.col("a").cast(nw.String) + "hi" + )["a"][0] + assert result == "1hi"