Skip to content

Commit c45dd37

Browse files
authored
BUG: Fix metadata propagation in squeeze and describe (#53538)
* BUG: Fix metadata propagation in squeeze and describe * remove unnecessary param
1 parent 6a5ae29 commit c45dd37

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ Styler
484484

485485
Metadata
486486
^^^^^^^^
487+
- Fixed metadata propagation in :meth:`DataFrame.squeeze`, and :meth:`DataFrame.describe` (:issue:`28283`)
487488
- Fixed metadata propagation in :meth:`DataFrame.std` (:issue:`28283`)
488489

489490
Other

pandas/core/generic.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -964,12 +964,15 @@ def squeeze(self, axis: Axis | None = None):
964964
1
965965
"""
966966
axes = range(self._AXIS_LEN) if axis is None else (self._get_axis_number(axis),)
967-
return self.iloc[
967+
result = self.iloc[
968968
tuple(
969969
0 if i in axes and len(a) == 1 else slice(None)
970970
for i, a in enumerate(self.axes)
971971
)
972972
]
973+
if isinstance(result, NDFrame):
974+
result = result.__finalize__(self, method="squeeze")
975+
return result
973976

974977
# ----------------------------------------------------------------------
975978
# Rename
@@ -11144,7 +11147,7 @@ def describe(
1114411147
include=include,
1114511148
exclude=exclude,
1114611149
percentiles=percentiles,
11147-
)
11150+
).__finalize__(self, method="describe")
1114811151

1114911152
@final
1115011153
def pct_change(

pandas/tests/generic/test_finalize.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,8 @@
223223
),
224224
(pd.DataFrame, frame_mi_data, operator.methodcaller("droplevel", "A")),
225225
(pd.DataFrame, frame_data, operator.methodcaller("pop", "A")),
226-
pytest.param(
227-
(pd.DataFrame, frame_data, operator.methodcaller("squeeze")),
228-
marks=not_implemented_mark,
229-
),
226+
# Squeeze on columns, otherwise we'll end up with a scalar
227+
(pd.DataFrame, frame_data, operator.methodcaller("squeeze", axis="columns")),
230228
(pd.Series, ([1, 2],), operator.methodcaller("squeeze")),
231229
(pd.Series, ([1, 2],), operator.methodcaller("rename_axis", index="a")),
232230
(pd.DataFrame, frame_data, operator.methodcaller("rename_axis", columns="a")),
@@ -350,14 +348,8 @@
350348
({"A": [1, 1, 1, 1]}, pd.date_range("2000", periods=4)),
351349
operator.methodcaller("tz_localize", "CET"),
352350
),
353-
pytest.param(
354-
(pd.Series, ([1, 2],), operator.methodcaller("describe")),
355-
marks=not_implemented_mark,
356-
),
357-
pytest.param(
358-
(pd.DataFrame, frame_data, operator.methodcaller("describe")),
359-
marks=not_implemented_mark,
360-
),
351+
(pd.Series, ([1, 2],), operator.methodcaller("describe")),
352+
(pd.DataFrame, frame_data, operator.methodcaller("describe")),
361353
(pd.Series, ([1, 2],), operator.methodcaller("pct_change")),
362354
(pd.DataFrame, frame_data, operator.methodcaller("pct_change")),
363355
(pd.Series, ([1],), operator.methodcaller("transform", lambda x: x - x.min())),
@@ -737,7 +729,6 @@ def test_groupby_finalize(obj, method):
737729
lambda x: x.agg("sem"),
738730
lambda x: x.agg("size"),
739731
lambda x: x.agg("ohlc"),
740-
lambda x: x.agg("describe"),
741732
],
742733
)
743734
@not_implemented_mark

0 commit comments

Comments
 (0)