Skip to content

Commit fadb72c

Browse files
REGR: fix consolidation/cache issue with take operation (#36114)
1 parent 7d73641 commit fadb72c

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

doc/source/whatsnew/v1.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fixed regressions
1717
- Regression in :meth:`DatetimeIndex.intersection` incorrectly raising ``AssertionError`` when intersecting against a list (:issue:`35876`)
1818
- Fix regression in updating a column inplace (e.g. using ``df['col'].fillna(.., inplace=True)``) (:issue:`35731`)
1919
- Performance regression for :meth:`RangeIndex.format` (:issue:`35712`)
20+
- Fix regression in invalid cache after an indexing operation; this can manifest when setting which does not update the data (:issue:`35521`)
2021
- Regression in :meth:`DataFrame.replace` where a ``TypeError`` would be raised when attempting to replace elements of type :class:`Interval` (:issue:`35931`)
2122
- Fix regression in pickle roundtrip of the ``closed`` attribute of :class:`IntervalIndex` (:issue:`35658`)
2223
- Fixed regression in :meth:`DataFrameGroupBy.agg` where a ``ValueError: buffer source array is read-only`` would be raised when the underlying array is read-only (:issue:`36014`)

pandas/core/generic.py

+2
Original file line numberDiff line numberDiff line change
@@ -3534,6 +3534,8 @@ class max_speed
35343534

35353535
nv.validate_take(tuple(), kwargs)
35363536

3537+
self._consolidate_inplace()
3538+
35373539
new_data = self._mgr.take(
35383540
indices, axis=self._get_block_manager_axis(axis), verify=True
35393541
)

pandas/tests/frame/test_block_internals.py

+23
Original file line numberDiff line numberDiff line change
@@ -658,3 +658,26 @@ def test_update_inplace_sets_valid_block_values():
658658

659659
# smoketest for OP bug from GH#35731
660660
assert df.isnull().sum().sum() == 0
661+
662+
663+
def test_nonconsolidated_item_cache_take():
664+
# https://github.com/pandas-dev/pandas/issues/35521
665+
666+
# create non-consolidated dataframe with object dtype columns
667+
df = pd.DataFrame()
668+
df["col1"] = pd.Series(["a"], dtype=object)
669+
df["col2"] = pd.Series([0], dtype=object)
670+
671+
# access column (item cache)
672+
df["col1"] == "A"
673+
# take operation
674+
# (regression was that this consolidated but didn't reset item cache,
675+
# resulting in an invalid cache and the .at operation not working properly)
676+
df[df["col2"] == 0]
677+
678+
# now setting value should update actual dataframe
679+
df.at[0, "col1"] = "A"
680+
681+
expected = pd.DataFrame({"col1": ["A"], "col2": [0]}, dtype=object)
682+
tm.assert_frame_equal(df, expected)
683+
assert df.at[0, "col1"] == "A"

0 commit comments

Comments
 (0)