Skip to content

Commit c52f831

Browse files
neilkgjbrockmendel
authored andcommitted
BUG: DataFrame._item_cache not cleared on on .copy() (pandas-dev#33299)
1 parent dfa6f44 commit c52f831

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ Indexing
364364
- Bug in :class:`Index` constructor where an unhelpful error message was raised for ``numpy`` scalars (:issue:`33017`)
365365
- Bug in :meth:`DataFrame.lookup` incorrectly raising an ``AttributeError`` when ``frame.index`` or ``frame.columns`` is not unique; this will now raise a ``ValueError`` with a helpful error message (:issue:`33041`)
366366
- Bug in :meth:`DataFrame.iloc.__setitem__` creating a new array instead of overwriting ``Categorical`` values in-place (:issue:`32831`)
367+
- Bug in :meth:`DataFrame.copy` _item_cache not invalidated after copy causes post-copy value updates to not be reflected (:issue:`31784`)
367368

368369
Missing
369370
^^^^^^^

pandas/core/generic.py

+1
Original file line numberDiff line numberDiff line change
@@ -5665,6 +5665,7 @@ def copy(self: FrameOrSeries, deep: bool_t = True) -> FrameOrSeries:
56655665
dtype: object
56665666
"""
56675667
data = self._data.copy(deep=deep)
5668+
self._clear_item_cache()
56685669
return self._constructor(data).__finalize__(self)
56695670

56705671
def __copy__(self: FrameOrSeries, deep: bool_t = True) -> FrameOrSeries:

pandas/tests/frame/test_api.py

+18
Original file line numberDiff line numberDiff line change
@@ -540,3 +540,21 @@ def test_attrs(self):
540540

541541
result = df.rename(columns=str)
542542
assert result.attrs == {"version": 1}
543+
544+
def test_cache_on_copy(self):
545+
# GH 31784 _item_cache not cleared on copy causes incorrect reads after updates
546+
df = DataFrame({"a": [1]})
547+
548+
df["x"] = [0]
549+
df["a"]
550+
551+
df.copy()
552+
553+
df["a"].values[0] = -1
554+
555+
tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0]}))
556+
557+
df["y"] = [0]
558+
559+
assert df["a"].values[0] == -1
560+
tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0], "y": [0]}))

0 commit comments

Comments
 (0)