Skip to content

BUG: Fix metadata propagation in squeeze and describe #53538

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 2 commits into from
Jun 7, 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
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 @@ -482,6 +482,7 @@ Styler

Metadata
^^^^^^^^
- Fixed metadata propagation in :meth:`DataFrame.squeeze`, and :meth:`DataFrame.describe` (:issue:`28283`)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Planning on finishing the rest of the missing methods (only a couple left), so I'll merge all the entries in the future.

- Fixed metadata propagation in :meth:`DataFrame.std` (:issue:`28283`)

Other
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,12 +957,15 @@ def squeeze(self, axis: Axis | None = None):
1
"""
axes = range(self._AXIS_LEN) if axis is None else (self._get_axis_number(axis),)
return self.iloc[
result = self.iloc[
tuple(
0 if i in axes and len(a) == 1 else slice(None)
for i, a in enumerate(self.axes)
)
]
if isinstance(result, NDFrame):
result = result.__finalize__(self, method="squeeze")
return result

# ----------------------------------------------------------------------
# Rename
Expand Down Expand Up @@ -11137,7 +11140,7 @@ def describe(
include=include,
exclude=exclude,
percentiles=percentiles,
)
).__finalize__(self, method="describe")

@final
def pct_change(
Expand Down
17 changes: 4 additions & 13 deletions pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,8 @@
),
(pd.DataFrame, frame_mi_data, operator.methodcaller("droplevel", "A")),
(pd.DataFrame, frame_data, operator.methodcaller("pop", "A")),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("squeeze")),
marks=not_implemented_mark,
),
# Squeeze on columns, otherwise we'll end up with a scalar
(pd.DataFrame, frame_data, operator.methodcaller("squeeze", axis="columns")),
(pd.Series, ([1, 2],), operator.methodcaller("squeeze")),
(pd.Series, ([1, 2],), operator.methodcaller("rename_axis", index="a")),
(pd.DataFrame, frame_data, operator.methodcaller("rename_axis", columns="a")),
Expand Down Expand Up @@ -372,14 +370,8 @@
({"A": [1, 1, 1, 1]}, pd.date_range("2000", periods=4)),
operator.methodcaller("tz_localize", "CET"),
),
pytest.param(
(pd.Series, ([1, 2],), operator.methodcaller("describe")),
marks=not_implemented_mark,
),
pytest.param(
(pd.DataFrame, frame_data, operator.methodcaller("describe")),
marks=not_implemented_mark,
),
(pd.Series, ([1, 2],), operator.methodcaller("describe")),
(pd.DataFrame, frame_data, operator.methodcaller("describe")),
(pd.Series, ([1, 2],), operator.methodcaller("pct_change")),
(pd.DataFrame, frame_data, operator.methodcaller("pct_change")),
(pd.Series, ([1],), operator.methodcaller("transform", lambda x: x - x.min())),
Expand Down Expand Up @@ -767,7 +759,6 @@ def test_groupby_finalize(obj, method):
lambda x: x.agg("sem"),
lambda x: x.agg("size"),
lambda x: x.agg("ohlc"),
lambda x: x.agg("describe"),
],
)
@not_implemented_mark
Expand Down