Skip to content

Commit 24eb873

Browse files
committed
try rename with copy=True
Maybe addresses (#2680 (comment))
1 parent cfeac25 commit 24eb873

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

narwhals/_pandas_like/dataframe.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,15 @@ def with_columns(self, *exprs: PandasLikeExpr) -> Self:
459459
df.columns.name = self.native.columns.name
460460
return self._with_native(df, validate_column_names=False)
461461

462-
def rename(self, mapping: Mapping[str, str]) -> Self:
462+
def rename(self, mapping: Mapping[str, str], *, copy: bool = False) -> Self:
463463
if mapping:
464464
return self._with_native(
465-
rename(self.native, columns=mapping, implementation=self._implementation)
465+
rename(
466+
self.native,
467+
columns=mapping,
468+
implementation=self._implementation,
469+
copy=copy,
470+
)
466471
)
467472
return self._with_native(self.native, validate_column_names=False)
468473

narwhals/_pandas_like/group_by.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from functools import lru_cache
55
from itertools import chain
66
from operator import methodcaller
7-
from typing import TYPE_CHECKING, Any, ClassVar, Literal, overload
7+
from typing import TYPE_CHECKING, Any, ClassVar, Literal, cast, overload
88

99
from narwhals._compliant import EagerGroupBy
1010
from narwhals._expression_parsing import evaluate_output_names_and_aliases
@@ -255,7 +255,9 @@ def __init__(
255255
frame, self._keys, self._output_key_names = self._parse_keys(df, keys=keys)
256256
self._exclude: tuple[str, ...] = (*self._keys, *self._output_key_names)
257257
self._remap_non_str_columns = _remap_non_str(self)
258-
self._compliant_frame = frame.rename(self._remap_non_str_columns)
258+
self._compliant_frame = cast("PandasLikeDataFrame", frame).rename(
259+
self._remap_non_str_columns, copy=True
260+
)
259261
# Drop index to avoid potential collisions:
260262
# https://github.com/narwhals-dev/narwhals/issues/1907.
261263
native = self.compliant.native

narwhals/_pandas_like/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,18 @@ def set_index(
182182

183183

184184
def rename(
185-
obj: NativeNDFrameT, *args: Any, implementation: Implementation, **kwargs: Any
185+
obj: NativeNDFrameT,
186+
*args: Any,
187+
implementation: Implementation,
188+
copy: bool = False,
189+
**kwargs: Any,
186190
) -> NativeNDFrameT:
187191
"""Wrapper around pandas' rename so that we can set `copy` based on implementation/version."""
188192
if implementation is Implementation.PANDAS and (
189193
implementation._backend_version() >= (3,)
190194
): # pragma: no cover
191195
return obj.rename(*args, **kwargs, inplace=False)
192-
return obj.rename(*args, **kwargs, copy=False, inplace=False)
196+
return obj.rename(*args, **kwargs, copy=copy, inplace=False)
193197

194198

195199
@functools.lru_cache(maxsize=16)

0 commit comments

Comments
 (0)