Skip to content
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

DEPR: _metadata propagation #52168

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ Deprecations
- Deprecated :func:`is_interval_dtype`, check ``isinstance(dtype, pd.IntervalDtype)`` instead (:issue:`52607`)
- Deprecated :meth:`DataFrame.applymap`. Use the new :meth:`DataFrame.map` method instead (:issue:`52353`)
- Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`)
- Deprecated ``_metadata`` propagation (:issue:`51280`)
- Deprecated ``freq`` parameter in :class:`PeriodArray` constructor, pass ``dtype`` instead (:issue:`52462`)
- Deprecated behavior of :func:`concat` when :class:`DataFrame` has columns that are all-NA, in a future version these will not be discarded when determining the resulting dtype (:issue:`40893`)
- Deprecated behavior of :meth:`Series.dt.to_pydatetime`, in a future version this will return a :class:`Series` containing python ``datetime`` objects instead of an ``ndarray`` of datetimes; this matches the behavior of other :meth:`Series.dt` properties (:issue:`20306`)
Expand Down
14 changes: 14 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6057,6 +6057,14 @@ def __finalize__(self, other, method: str | None = None, **kwargs) -> Self:
# For subclasses using _metadata.
for name in set(self._metadata) & set(other._metadata):
assert isinstance(name, str)
if name != "_name":
warnings.warn(
"_metadata propagation is deprecated and will be removed "
"in a future version. To retain the old behavior, "
"override __finalize__ in a subclass.",
FutureWarning,
stacklevel=find_stack_level(),
)
object.__setattr__(self, name, getattr(other, name, None))

if method == "concat":
Expand Down Expand Up @@ -6111,6 +6119,12 @@ def __setattr__(self, name: str, value) -> None:
if name in self._internal_names_set:
object.__setattr__(self, name, value)
elif name in self._metadata:
warnings.warn(
"_metadata handling is deprecated and will be removed in "
"a future version.",
FutureWarning,
stacklevel=find_stack_level(),
)
object.__setattr__(self, name, value)
else:
try:
Expand Down
14 changes: 10 additions & 4 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2069,12 +2069,18 @@ def _constructor(self):
def _constructor_sliced(self):
return SubclassedSeries

sdf = SubclassedDataFrame("some_data", {"A": [1, 2, 3], "B": [4, 5, 6]})
result = sdf * 2
expected = SubclassedDataFrame("some_data", {"A": [2, 4, 6], "B": [8, 10, 12]})
msg = "_metadata handling is deprecated"

with tm.assert_produces_warning(FutureWarning, match=msg):
sdf = SubclassedDataFrame("some_data", {"A": [1, 2, 3], "B": [4, 5, 6]})
with tm.assert_produces_warning(FutureWarning, match=msg):
result = sdf * 2
with tm.assert_produces_warning(FutureWarning, match=msg):
expected = SubclassedDataFrame("some_data", {"A": [2, 4, 6], "B": [8, 10, 12]})
tm.assert_frame_equal(result, expected)

result = sdf + sdf
with tm.assert_produces_warning(FutureWarning, match=msg):
result = sdf + sdf
tm.assert_frame_equal(result, expected)


Expand Down
Loading