Skip to content

Commit f7cd658

Browse files
authored
Fixed Metadata Propogation in DataFrame (#37381)
1 parent a057135 commit f7cd658

File tree

4 files changed

+15
-20
lines changed

4 files changed

+15
-20
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ Other
569569
- Bug in :meth:`DataFrame.replace` and :meth:`Series.replace` incorrectly raising ``AssertionError`` instead of ``ValueError`` when invalid parameter combinations are passed (:issue:`36045`)
570570
- Bug in :meth:`DataFrame.replace` and :meth:`Series.replace` with numeric values and string ``to_replace`` (:issue:`34789`)
571571
- Fixed bug in metadata propagation incorrectly copying DataFrame columns as metadata when the column name overlaps with the metadata name (:issue:`37037`)
572-
- Fixed metadata propagation in the :class:`Series.dt` and :class:`Series.str` accessors and :class:`DataFrame.duplicated` and :class:`DataFrame.stack` and :class:`DataFrame.unstack` and :class:`DataFrame.pivot` methods (:issue:`28283`)
572+
- Fixed metadata propagation in the :class:`Series.dt`, :class:`Series.str` accessors, :class:`DataFrame.duplicated`, :class:`DataFrame.stack`, :class:`DataFrame.unstack`, :class:`DataFrame.pivot`, :class:`DataFrame.append`, :class:`DataFrame.diff`, :class:`DataFrame.applymap` and :class:`DataFrame.update` methods (:issue:`28283`) (:issue:`37381`)
573573
- Bug in :meth:`Index.union` behaving differently depending on whether operand is a :class:`Index` or other list-like (:issue:`36384`)
574574
- Passing an array with 2 or more dimensions to the :class:`Series` constructor now raises the more specific ``ValueError``, from a bare ``Exception`` previously (:issue:`35744`)
575575
- Bug in ``accessor.DirNamesMixin``, where ``dir(obj)`` wouldn't show attributes defined on the instance (:issue:`37173`).

pandas/core/frame.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -7401,7 +7401,7 @@ def diff(self, periods: int = 1, axis: Axis = 0) -> DataFrame:
74017401
return self - self.shift(periods, axis=axis)
74027402

74037403
new_data = self._mgr.diff(n=periods, axis=bm_axis)
7404-
return self._constructor(new_data)
7404+
return self._constructor(new_data).__finalize__(self, "diff")
74057405

74067406
# ----------------------------------------------------------------------
74077407
# Function application
@@ -7780,7 +7780,7 @@ def infer(x):
77807780
return lib.map_infer(x, func, ignore_na=ignore_na)
77817781
return lib.map_infer(x.astype(object)._values, func, ignore_na=ignore_na)
77827782

7783-
return self.apply(infer)
7783+
return self.apply(infer).__finalize__(self, "applymap")
77847784

77857785
# ----------------------------------------------------------------------
77867786
# Merging / joining methods
@@ -7917,12 +7917,14 @@ def append(
79177917
to_concat = [self, *other]
79187918
else:
79197919
to_concat = [self, other]
7920-
return concat(
7921-
to_concat,
7922-
ignore_index=ignore_index,
7923-
verify_integrity=verify_integrity,
7924-
sort=sort,
7925-
)
7920+
return (
7921+
concat(
7922+
to_concat,
7923+
ignore_index=ignore_index,
7924+
verify_integrity=verify_integrity,
7925+
sort=sort,
7926+
)
7927+
).__finalize__(self, method="append")
79267928

79277929
def join(
79287930
self, other, on=None, how="left", lsuffix="", rsuffix="", sort=False

pandas/core/reshape/merge.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ def get_result(self):
689689

690690
self._maybe_restore_index_levels(result)
691691

692-
return result
692+
return result.__finalize__(self, method="merge")
693693

694694
def _indicator_pre_merge(
695695
self, left: "DataFrame", right: "DataFrame"
@@ -1505,7 +1505,7 @@ def get_result(self):
15051505
)
15061506

15071507
typ = self.left._constructor
1508-
result = typ(result_data).__finalize__(self, method=self._merge_type)
1508+
result = typ(result_data)
15091509

15101510
self._maybe_add_join_keys(result, left_indexer, right_indexer)
15111511

pandas/tests/generic/test_finalize.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -178,28 +178,21 @@
178178
marks=not_implemented_mark,
179179
),
180180
pytest.param(
181-
(pd.DataFrame, frame_data, operator.methodcaller("diff")),
182-
marks=not_implemented_mark,
183-
),
184-
pytest.param(
185-
(pd.DataFrame, frame_data, operator.methodcaller("applymap", lambda x: x)),
186-
marks=not_implemented_mark,
181+
(pd.DataFrame, frame_data, operator.methodcaller("applymap", lambda x: x))
187182
),
188183
pytest.param(
189184
(
190185
pd.DataFrame,
191186
frame_data,
192187
operator.methodcaller("append", pd.DataFrame({"A": [1]})),
193-
),
194-
marks=not_implemented_mark,
188+
)
195189
),
196190
pytest.param(
197191
(
198192
pd.DataFrame,
199193
frame_data,
200194
operator.methodcaller("append", pd.DataFrame({"B": [1]})),
201195
),
202-
marks=not_implemented_mark,
203196
),
204197
pytest.param(
205198
(

0 commit comments

Comments
 (0)