Skip to content

Commit 987a688

Browse files
committed
revert: undo last commit
Moved to #2368
1 parent bd7fd09 commit 987a688

File tree

4 files changed

+60
-28
lines changed

4 files changed

+60
-28
lines changed

narwhals/_pandas_like/dataframe.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from narwhals._pandas_like.utils import check_column_names_are_unique
2222
from narwhals._pandas_like.utils import convert_str_slice_to_int_slice
2323
from narwhals._pandas_like.utils import get_dtype_backend
24+
from narwhals._pandas_like.utils import horizontal_concat
2425
from narwhals._pandas_like.utils import native_to_narwhals_dtype
2526
from narwhals._pandas_like.utils import object_native_to_narwhals_dtype
2627
from narwhals._pandas_like.utils import pivot_table
@@ -510,8 +511,11 @@ def select(self: PandasLikeDataFrame, *exprs: PandasLikeExpr) -> PandasLikeDataF
510511
# return empty dataframe, like Polars does
511512
return self._with_native(self.native.__class__(), validate_column_names=False)
512513
new_series = align_series_full_broadcast(*new_series)
513-
namespace = self.__narwhals_namespace__()
514-
df = namespace._horizontal_concat([s.native for s in new_series])
514+
df = horizontal_concat(
515+
[s.native for s in new_series],
516+
implementation=self._implementation,
517+
backend_version=self._backend_version,
518+
)
515519
return self._with_native(df, validate_column_names=True)
516520

517521
def drop_nulls(
@@ -534,7 +538,13 @@ def with_row_index(self: Self, name: str) -> Self:
534538
row_index = namespace._series.from_iterable(
535539
range(len(frame)), context=self, index=frame.index
536540
).alias(name)
537-
return self._with_native(namespace._horizontal_concat([row_index.native, frame]))
541+
return self._with_native(
542+
horizontal_concat(
543+
[row_index.native, frame],
544+
implementation=self._implementation,
545+
backend_version=self._backend_version,
546+
)
547+
)
538548

539549
def row(self: Self, index: int) -> tuple[Any, ...]:
540550
return tuple(x for x in self.native.iloc[index])
@@ -568,8 +578,11 @@ def with_columns(
568578
series = self.native[name]
569579
to_concat.append(series)
570580
to_concat.extend(self._extract_comparand(s) for s in name_columns.values())
571-
namespace = self.__narwhals_namespace__()
572-
df = namespace._horizontal_concat(to_concat)
581+
df = horizontal_concat(
582+
to_concat,
583+
implementation=self._implementation,
584+
backend_version=self._backend_version,
585+
)
573586
return self._with_native(df, validate_column_names=False)
574587

575588
def rename(self: Self, mapping: Mapping[str, str]) -> Self:

narwhals/_pandas_like/group_by.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from narwhals._compliant import EagerGroupBy
1313
from narwhals._expression_parsing import evaluate_output_names_and_aliases
14+
from narwhals._pandas_like.utils import horizontal_concat
1415
from narwhals._pandas_like.utils import select_columns_by_name
1516
from narwhals._pandas_like.utils import set_columns
1617
from narwhals.utils import find_stacklevel
@@ -232,8 +233,11 @@ def agg(self: Self, *exprs: PandasLikeExpr) -> PandasLikeDataFrame: # noqa: PLR
232233
pass
233234
msg = f"Expected unique output names, got:{msg}"
234235
raise ValueError(msg)
235-
namespace = self.compliant.__narwhals_namespace__()
236-
result = namespace._horizontal_concat(result_aggs)
236+
result = horizontal_concat(
237+
dfs=result_aggs,
238+
implementation=implementation,
239+
backend_version=backend_version,
240+
)
237241
else:
238242
# No aggregation provided
239243
result = self.compliant.__native_namespace__().DataFrame(

narwhals/_pandas_like/namespace.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
from __future__ import annotations
22

33
import operator
4-
import warnings
54
from functools import reduce
65
from typing import TYPE_CHECKING
76
from typing import Any
87
from typing import Iterable
9-
from typing import Sequence
10-
from typing import TypeVar
118

129
from narwhals._compliant import CompliantThen
1310
from narwhals._compliant import EagerNamespace
@@ -20,6 +17,7 @@
2017
from narwhals._pandas_like.series import PandasLikeSeries
2118
from narwhals._pandas_like.utils import align_series_full_broadcast
2219
from narwhals._pandas_like.utils import diagonal_concat
20+
from narwhals._pandas_like.utils import horizontal_concat
2321
from narwhals._pandas_like.utils import vertical_concat
2422
from narwhals.utils import import_dtypes_module
2523

@@ -32,8 +30,6 @@
3230
from narwhals.utils import Implementation
3331
from narwhals.utils import Version
3432

35-
NDFrameT = TypeVar("NDFrameT", "pd.DataFrame", "pd.Series[Any]")
36-
3733

3834
class PandasLikeNamespace(
3935
EagerNamespace[
@@ -227,27 +223,16 @@ def func(df: PandasLikeDataFrame) -> list[PandasLikeSeries]:
227223
context=self,
228224
)
229225

230-
def _horizontal_concat(self, dfs: Sequence[NDFrameT], /) -> NDFrameT:
231-
"""Concatenate (native) DataFrames horizontally."""
232-
concat = self._implementation.to_native_namespace().concat
233-
if self._implementation.is_cudf():
234-
with warnings.catch_warnings():
235-
warnings.filterwarnings(
236-
"ignore",
237-
message="The behavior of array concatenation with empty entries is deprecated",
238-
category=FutureWarning,
239-
)
240-
return concat(dfs, axis=1)
241-
elif self._implementation.is_pandas() and self._backend_version < (3,):
242-
return concat(dfs, axis=1, copy=False)
243-
return concat(dfs, axis=1)
244-
245226
def concat(
246227
self, items: Iterable[PandasLikeDataFrame], *, how: ConcatMethod
247228
) -> PandasLikeDataFrame:
248229
dfs: list[Any] = [item._native_frame for item in items]
249230
if how == "horizontal":
250-
native_dataframe = self._horizontal_concat(dfs)
231+
native_dataframe = horizontal_concat(
232+
dfs,
233+
implementation=self._implementation,
234+
backend_version=self._backend_version,
235+
)
251236
elif how == "vertical":
252237
native_dataframe = vertical_concat(
253238
dfs,

narwhals/_pandas_like/utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import functools
44
import re
5+
import warnings
56
from contextlib import suppress
67
from typing import TYPE_CHECKING
78
from typing import Any
@@ -129,6 +130,35 @@ def align_and_extract_native(
129130
return lhs.native, rhs
130131

131132

133+
def horizontal_concat(
134+
dfs: list[Any], *, implementation: Implementation, backend_version: tuple[int, ...]
135+
) -> Any:
136+
"""Concatenate (native) DataFrames horizontally.
137+
138+
Should be in namespace.
139+
"""
140+
if implementation is Implementation.CUDF:
141+
with warnings.catch_warnings():
142+
warnings.filterwarnings(
143+
"ignore",
144+
message="The behavior of array concatenation with empty entries is deprecated",
145+
category=FutureWarning,
146+
)
147+
return implementation.to_native_namespace().concat(dfs, axis=1)
148+
149+
if implementation.is_pandas_like():
150+
extra_kwargs = (
151+
{"copy": False}
152+
if implementation is Implementation.PANDAS and backend_version < (3,)
153+
else {}
154+
)
155+
return implementation.to_native_namespace().concat(dfs, axis=1, **extra_kwargs)
156+
157+
else: # pragma: no cover
158+
msg = f"Expected pandas-like implementation ({PANDAS_LIKE_IMPLEMENTATION}), found {implementation}"
159+
raise TypeError(msg)
160+
161+
132162
def vertical_concat(
133163
dfs: list[Any], *, implementation: Implementation, backend_version: tuple[int, ...]
134164
) -> Any:

0 commit comments

Comments
 (0)