Skip to content

fix: Preserve pandas column name attribute #2363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion narwhals/_dask/dataframe.py
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dask select and with_columns have no issues since we use .assign method for both!

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from narwhals._dask.utils import add_row_index
from narwhals._dask.utils import evaluate_exprs
from narwhals._pandas_like.utils import native_to_narwhals_dtype
from narwhals._pandas_like.utils import rename_axis
from narwhals._pandas_like.utils import select_columns_by_name
from narwhals.typing import CompliantDataFrame
from narwhals.typing import CompliantLazyFrame
Expand Down Expand Up @@ -112,7 +113,12 @@ def collect(
from narwhals._pandas_like.dataframe import PandasLikeDataFrame

return PandasLikeDataFrame(
result,
rename_axis(
result,
implementation=Implementation.PANDAS,
backend_version=parse_version(pd),
columns=self.native.columns.name,
),
implementation=Implementation.PANDAS,
backend_version=parse_version(pd),
version=self._version,
Expand Down
39 changes: 19 additions & 20 deletions narwhals/_pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
from narwhals._pandas_like.utils import check_column_names_are_unique
from narwhals._pandas_like.utils import convert_str_slice_to_int_slice
from narwhals._pandas_like.utils import get_dtype_backend
from narwhals._pandas_like.utils import horizontal_concat
from narwhals._pandas_like.utils import native_to_narwhals_dtype
from narwhals._pandas_like.utils import object_native_to_narwhals_dtype
from narwhals._pandas_like.utils import pivot_table
from narwhals._pandas_like.utils import rename
from narwhals._pandas_like.utils import rename_axis
from narwhals._pandas_like.utils import select_columns_by_name
from narwhals._pandas_like.utils import set_index
from narwhals.dependencies import is_numpy_array_1d
Expand Down Expand Up @@ -120,6 +120,7 @@ def __init__(
validate_backend_version(self._implementation, self._backend_version)
if validate_column_names:
check_column_names_are_unique(native_dataframe.columns)
self._native_columns_name = native_dataframe.columns.name

@classmethod
def from_arrow(cls, data: IntoArrowTable, /, *, context: _FullContext) -> Self:
Expand Down Expand Up @@ -251,7 +252,12 @@ def _with_version(self: Self, version: Version) -> Self:

def _with_native(self: Self, df: Any, *, validate_column_names: bool = True) -> Self:
return self.__class__(
df,
rename_axis(
df,
implementation=self._implementation,
backend_version=self._backend_version,
columns=self._native_columns_name,
),
implementation=self._implementation,
backend_version=self._backend_version,
version=self._version,
Expand Down Expand Up @@ -504,11 +510,8 @@ def select(self: PandasLikeDataFrame, *exprs: PandasLikeExpr) -> PandasLikeDataF
# return empty dataframe, like Polars does
return self._with_native(self.native.__class__(), validate_column_names=False)
new_series = align_series_full_broadcast(*new_series)
df = horizontal_concat(
[s.native for s in new_series],
implementation=self._implementation,
backend_version=self._backend_version,
)
namespace = self.__narwhals_namespace__()
df = namespace._horizontal_concat([s.native for s in new_series])
return self._with_native(df, validate_column_names=True)

def drop_nulls(
Expand All @@ -531,13 +534,7 @@ def with_row_index(self: Self, name: str) -> Self:
row_index = namespace._series.from_iterable(
range(len(frame)), context=self, index=frame.index
).alias(name)
return self._with_native(
horizontal_concat(
[row_index.native, frame],
implementation=self._implementation,
backend_version=self._backend_version,
)
)
return self._with_native(namespace._horizontal_concat([row_index.native, frame]))

def row(self: Self, index: int) -> tuple[Any, ...]:
return tuple(x for x in self.native.iloc[index])
Expand Down Expand Up @@ -571,11 +568,8 @@ def with_columns(
series = self.native[name]
to_concat.append(series)
to_concat.extend(self._extract_comparand(s) for s in name_columns.values())
df = horizontal_concat(
to_concat,
implementation=self._implementation,
backend_version=self._backend_version,
)
namespace = self.__narwhals_namespace__()
df = namespace._horizontal_concat(to_concat)
return self._with_native(df, validate_column_names=False)

def rename(self: Self, mapping: Mapping[str, str]) -> Self:
Expand Down Expand Up @@ -633,7 +627,12 @@ def collect(
import pandas as pd # ignore-banned-import

return PandasLikeDataFrame(
self.to_pandas(),
rename_axis(
self.to_pandas(),
implementation=self._implementation,
backend_version=self._backend_version,
columns=self._native_columns_name,
),
implementation=Implementation.PANDAS,
backend_version=parse_version(pd),
version=self._version,
Expand Down
8 changes: 2 additions & 6 deletions narwhals/_pandas_like/group_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from narwhals._compliant import EagerGroupBy
from narwhals._expression_parsing import evaluate_output_names_and_aliases
from narwhals._pandas_like.utils import horizontal_concat
from narwhals._pandas_like.utils import select_columns_by_name
from narwhals._pandas_like.utils import set_columns
from narwhals.utils import find_stacklevel
Expand Down Expand Up @@ -233,11 +232,8 @@ def agg(self: Self, *exprs: PandasLikeExpr) -> PandasLikeDataFrame: # noqa: PLR
pass
msg = f"Expected unique output names, got:{msg}"
raise ValueError(msg)
result = horizontal_concat(
dfs=result_aggs,
implementation=implementation,
backend_version=backend_version,
)
namespace = self.compliant.__narwhals_namespace__()
result = namespace._horizontal_concat(result_aggs)
else:
# No aggregation provided
result = self.compliant.__native_namespace__().DataFrame(
Expand Down
67 changes: 36 additions & 31 deletions narwhals/_pandas_like/namespace.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from __future__ import annotations

import operator
import warnings
from functools import reduce
from typing import TYPE_CHECKING
from typing import Any
from typing import Iterable
from typing import Sequence
from typing import TypeVar

from narwhals._compliant import CompliantThen
from narwhals._compliant import EagerNamespace
Expand All @@ -17,7 +20,6 @@
from narwhals._pandas_like.series import PandasLikeSeries
from narwhals._pandas_like.utils import align_series_full_broadcast
from narwhals._pandas_like.utils import diagonal_concat
from narwhals._pandas_like.utils import horizontal_concat
from narwhals._pandas_like.utils import vertical_concat
from narwhals.utils import import_dtypes_module

Expand All @@ -30,6 +32,8 @@
from narwhals.utils import Implementation
from narwhals.utils import Version

NDFrameT = TypeVar("NDFrameT", "pd.DataFrame", "pd.Series[Any]")


class PandasLikeNamespace(
EagerNamespace[
Expand Down Expand Up @@ -223,48 +227,49 @@ def func(df: PandasLikeDataFrame) -> list[PandasLikeSeries]:
context=self,
)

def _horizontal_concat(self, dfs: Sequence[NDFrameT], /) -> NDFrameT:
"""Concatenate (native) DataFrames horizontally."""
concat = self._implementation.to_native_namespace().concat
if self._implementation.is_cudf():
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="The behavior of array concatenation with empty entries is deprecated",
category=FutureWarning,
)
return concat(dfs, axis=1)
elif self._implementation.is_pandas() and self._backend_version < (3,):
return concat(dfs, axis=1, copy=False)
return concat(dfs, axis=1)

def concat(
self, items: Iterable[PandasLikeDataFrame], *, how: ConcatMethod
) -> PandasLikeDataFrame:
dfs: list[Any] = [item._native_frame for item in items]
if how == "horizontal":
return PandasLikeDataFrame(
horizontal_concat(
dfs,
implementation=self._implementation,
backend_version=self._backend_version,
),
native_dataframe = self._horizontal_concat(dfs)
elif how == "vertical":
native_dataframe = vertical_concat(
dfs,
implementation=self._implementation,
backend_version=self._backend_version,
version=self._version,
validate_column_names=True,
)
if how == "vertical":
return PandasLikeDataFrame(
vertical_concat(
dfs,
implementation=self._implementation,
backend_version=self._backend_version,
),
elif how == "diagonal":
native_dataframe = diagonal_concat(
dfs,
implementation=self._implementation,
backend_version=self._backend_version,
version=self._version,
validate_column_names=True,
)
else:
raise NotImplementedError

if how == "diagonal":
return PandasLikeDataFrame(
diagonal_concat(
dfs,
implementation=self._implementation,
backend_version=self._backend_version,
),
implementation=self._implementation,
backend_version=self._backend_version,
version=self._version,
validate_column_names=True,
)
raise NotImplementedError
return PandasLikeDataFrame(
native_dataframe,
implementation=self._implementation,
backend_version=self._backend_version,
version=self._version,
validate_column_names=True,
)

def when(self: Self, predicate: PandasLikeExpr) -> PandasWhen:
return PandasWhen.from_expr(predicate, context=self)
Expand Down
45 changes: 15 additions & 30 deletions narwhals/_pandas_like/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import functools
import re
import warnings
from contextlib import suppress
from typing import TYPE_CHECKING
from typing import Any
Expand Down Expand Up @@ -130,35 +129,6 @@ def align_and_extract_native(
return lhs.native, rhs


def horizontal_concat(
dfs: list[Any], *, implementation: Implementation, backend_version: tuple[int, ...]
) -> Any:
"""Concatenate (native) DataFrames horizontally.

Should be in namespace.
"""
if implementation is Implementation.CUDF:
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="The behavior of array concatenation with empty entries is deprecated",
category=FutureWarning,
)
return implementation.to_native_namespace().concat(dfs, axis=1)

if implementation.is_pandas_like():
extra_kwargs = (
{"copy": False}
if implementation is Implementation.PANDAS and backend_version < (3,)
else {}
)
return implementation.to_native_namespace().concat(dfs, axis=1, **extra_kwargs)

else: # pragma: no cover
msg = f"Expected pandas-like implementation ({PANDAS_LIKE_IMPLEMENTATION}), found {implementation}"
raise TypeError(msg)


def vertical_concat(
dfs: list[Any], *, implementation: Implementation, backend_version: tuple[int, ...]
) -> Any:
Expand Down Expand Up @@ -794,6 +764,21 @@ def check_column_names_are_unique(columns: pd.Index[str]) -> None:
raise DuplicateError(msg)


def rename_axis(
obj: T,
*args: Any,
implementation: Implementation,
backend_version: tuple[int, ...],
**kwargs: Any,
) -> T:
"""Wrapper around pandas' rename_axis so that we can set `copy` based on implementation/version."""
if implementation is Implementation.PANDAS and (
backend_version >= (3,)
): # pragma: no cover
return obj.rename_axis(*args, **kwargs) # type: ignore[attr-defined]
return obj.rename_axis(*args, **kwargs, copy=False) # type: ignore[attr-defined]


class PandasLikeSeriesNamespace(EagerSeriesNamespace["PandasLikeSeries", Any]):
@property
def implementation(self) -> Implementation:
Expand Down
2 changes: 1 addition & 1 deletion narwhals/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,8 @@ def _from_native_impl( # noqa: PLR0915
return DataFrame(
PandasLikeDataFrame(
native_object,
backend_version=parse_version(pd),
implementation=Implementation.PANDAS,
backend_version=parse_version(pd),
Comment on lines -478 to +479
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mental sanity for symmetry with other pandas-like and order of the spec πŸ˜…

version=version,
validate_column_names=True,
),
Expand Down
12 changes: 7 additions & 5 deletions tests/frame/join_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import narwhals as nw_main # use nw_main in some tests for coverage
import narwhals.stable.v1 as nw
from narwhals.utils import Implementation
from tests.utils import DUCKDB_VERSION
from tests.utils import PANDAS_VERSION
from tests.utils import POLARS_VERSION
Expand Down Expand Up @@ -238,11 +237,14 @@ def test_cross_join_suffix(


def test_cross_join_non_pandas() -> None:
_ = pytest.importorskip("modin")

import modin.pandas as mpd

data = {"antananarivo": [1, 3, 2]}
df = nw.from_native(pd.DataFrame(data))
# HACK to force testing for a non-pandas codepath
df._compliant_frame._implementation = Implementation.MODIN
result = df.join(df, how="cross") # type: ignore[arg-type]
df1 = nw.from_native(mpd.DataFrame(pd.DataFrame(data)), eager_only=True)
df2 = nw.from_native(mpd.DataFrame(pd.DataFrame(data)), eager_only=True)
result = df1.join(df2, how="cross")
expected = {
"antananarivo": [1, 1, 1, 3, 3, 3, 2, 2, 2],
"antananarivo_right": [1, 3, 2, 1, 3, 2, 1, 3, 2],
Expand Down
28 changes: 28 additions & 0 deletions tests/preserve_pandas_like_columns_name_attr_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

import narwhals.stable.v1 as nw

if TYPE_CHECKING:
from tests.utils import Constructor


def test_ops_preserve_column_index_name(constructor: Constructor) -> None:
if not any(x in str(constructor) for x in ("pandas", "modin", "cudf", "dask")):
pytest.skip(
reason="Dataframe columns is a list and do not have a `name` like a pandas Index does"
)

data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8.0, 9.0]}
df_native = constructor(data)
df_native.columns.name = "foo" # type: ignore[union-attr]

df = nw.from_native(df_native)

result = df.with_columns(b=nw.col("a") + 1, c=nw.col("a") * 2).select("c", "b")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to concatenate two methods here, mostly for the sake of it.
Might be worth reparametrizing it


assert result.to_native().columns.name == "foo" # type: ignore[union-attr]
assert result.lazy().collect(backend="pandas").to_native().columns.name == "foo"
Loading