Skip to content

DEPR: remove use of nan_as_null from callers of __dataframe__ #54846

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

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 3 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,8 @@ def __dataframe__(
Parameters
----------
nan_as_null : bool, default False
Whether to tell the DataFrame to overwrite null values in the data
with ``NaN`` (or ``NaT``).
`nan_as_null` is DEPRECATED and has no effect. Please avoid using
it; it will be removed in a future release.
allow_copy : bool, default True
Whether to allow memory copying when exporting. If set to False
it would cause non-zero-copy exports to fail.
Expand All @@ -909,9 +909,6 @@ def __dataframe__(
Details on the interchange protocol:
https://data-apis.org/dataframe-protocol/latest/index.html

`nan_as_null` currently has no effect; once support for nullable extension
dtypes is added, this value should be propagated to columns.

Examples
--------
>>> df_not_necessarily_pandas = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
Expand All @@ -931,7 +928,7 @@ def __dataframe__(

from pandas.core.interchange.dataframe import PandasDataFrameXchg

return PandasDataFrameXchg(self, nan_as_null, allow_copy)
return PandasDataFrameXchg(self, allow_copy=allow_copy)

def __dataframe_consortium_standard__(
self, *, api_version: str | None = None
Expand Down
22 changes: 7 additions & 15 deletions pandas/core/interchange/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,20 @@ class PandasDataFrameXchg(DataFrameXchg):
attributes defined on this class.
"""

def __init__(
self, df: DataFrame, nan_as_null: bool = False, allow_copy: bool = True
) -> None:
def __init__(self, df: DataFrame, allow_copy: bool = True) -> None:
"""
Constructor - an instance of this (private) class is returned from
`pd.DataFrame.__dataframe__`.
"""
self._df = df
# ``nan_as_null`` is a keyword intended for the consumer to tell the
# producer to overwrite null values in the data with ``NaN`` (or ``NaT``).
# This currently has no effect; once support for nullable extension
# dtypes is added, this value should be propagated to columns.
self._nan_as_null = nan_as_null
self._allow_copy = allow_copy

def __dataframe__(
self, nan_as_null: bool = False, allow_copy: bool = True
) -> PandasDataFrameXchg:
return PandasDataFrameXchg(self._df, nan_as_null, allow_copy)
# `nan_as_null` can be removed here once it's removed from
# Dataframe.__dataframe__
return PandasDataFrameXchg(self._df, allow_copy)

@property
def metadata(self) -> dict[str, Index]:
Expand Down Expand Up @@ -84,7 +79,7 @@ def select_columns(self, indices: Sequence[int]) -> PandasDataFrameXchg:
indices = list(indices)

return PandasDataFrameXchg(
self._df.iloc[:, indices], self._nan_as_null, self._allow_copy
self._df.iloc[:, indices], allow_copy=self._allow_copy
)

def select_columns_by_name(self, names: list[str]) -> PandasDataFrameXchg: # type: ignore[override] # noqa: E501
Expand All @@ -93,9 +88,7 @@ def select_columns_by_name(self, names: list[str]) -> PandasDataFrameXchg: # ty
if not isinstance(names, list):
names = list(names)

return PandasDataFrameXchg(
self._df.loc[:, names], self._nan_as_null, self._allow_copy
)
return PandasDataFrameXchg(self._df.loc[:, names], allow_copy=self._allow_copy)

def get_chunks(self, n_chunks: int | None = None) -> Iterable[PandasDataFrameXchg]:
"""
Expand All @@ -109,8 +102,7 @@ def get_chunks(self, n_chunks: int | None = None) -> Iterable[PandasDataFrameXch
for start in range(0, step * n_chunks, step):
yield PandasDataFrameXchg(
self._df.iloc[start : start + step, :],
self._nan_as_null,
self._allow_copy,
allow_copy=self._allow_copy,
)
else:
yield self