Skip to content

Commit a715964

Browse files
Backport PR #35882: BUG: item_cache invalidation in get_numeric_data (#36188)
Co-authored-by: jbrockmendel <[email protected]>
1 parent d0a39d1 commit a715964

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v1.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Bug fixes
4040
- Bug in :class:`Series` constructor incorrectly raising a ``TypeError`` when passed an ordered set (:issue:`36044`)
4141
- Bug in :meth:`Series.dt.isocalendar` and :meth:`DatetimeIndex.isocalendar` that returned incorrect year for certain dates (:issue:`36032`)
4242
- Bug in :class:`DataFrame` indexing returning an incorrect :class:`Series` in some cases when the series has been altered and a cache not invalidated (:issue:`33675`)
43+
- Bug in :meth:`DataFrame.corr` causing subsequent indexing lookups to be incorrect (:issue:`35882`)
4344

4445
.. ---------------------------------------------------------------------------
4546

pandas/core/internals/managers.py

-1
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,6 @@ def get_numeric_data(self, copy: bool = False) -> "BlockManager":
732732
copy : bool, default False
733733
Whether to copy the blocks
734734
"""
735-
self._consolidate_inplace()
736735
return self._combine([b for b in self.blocks if b.is_numeric], copy)
737736

738737
def _combine(self, blocks: List[Block], copy: bool = True) -> "BlockManager":

pandas/tests/frame/methods/test_cov_corr.py

+17
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,23 @@ def test_corr_nullable_integer(self, nullable_column, other_column, method):
191191
expected = pd.DataFrame(np.ones((2, 2)), columns=["a", "b"], index=["a", "b"])
192192
tm.assert_frame_equal(result, expected)
193193

194+
def test_corr_item_cache(self):
195+
# Check that corr does not lead to incorrect entries in item_cache
196+
197+
df = pd.DataFrame({"A": range(10)})
198+
df["B"] = range(10)[::-1]
199+
200+
ser = df["A"] # populate item_cache
201+
assert len(df._mgr.blocks) == 2
202+
203+
_ = df.corr()
204+
205+
# Check that the corr didnt break link between ser and df
206+
ser.values[0] = 99
207+
assert df.loc[0, "A"] == 99
208+
assert df["A"] is ser
209+
assert df.values[0, 0] == 99
210+
194211

195212
class TestDataFrameCorrWith:
196213
def test_corrwith(self, datetime_frame):

0 commit comments

Comments
 (0)