Skip to content

Commit e87b8bc

Browse files
authored
BUG: to_dict_of_blocks failing to invalidate item_cache (pandas-dev#35874)
1 parent d3d74c5 commit e87b8bc

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

pandas/core/internals/managers.py

-5
Original file line numberDiff line numberDiff line change
@@ -896,12 +896,7 @@ def to_dict(self, copy: bool = True):
896896
Returns
897897
-------
898898
values : a dict of dtype -> BlockManager
899-
900-
Notes
901-
-----
902-
This consolidates based on str(dtype)
903899
"""
904-
self._consolidate_inplace()
905900

906901
bd: Dict[str, List[Block]] = {}
907902
for b in self.blocks:

pandas/tests/frame/test_block_internals.py

+18
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,21 @@ def test_add_column_with_pandas_array(self):
626626
assert type(df["c"]._mgr.blocks[0]) == ObjectBlock
627627
assert type(df2["c"]._mgr.blocks[0]) == ObjectBlock
628628
tm.assert_frame_equal(df, df2)
629+
630+
631+
def test_to_dict_of_blocks_item_cache():
632+
# Calling to_dict_of_blocks should not poison item_cache
633+
df = pd.DataFrame({"a": [1, 2, 3, 4], "b": ["a", "b", "c", "d"]})
634+
df["c"] = pd.arrays.PandasArray(np.array([1, 2, None, 3], dtype=object))
635+
mgr = df._mgr
636+
assert len(mgr.blocks) == 3 # i.e. not consolidated
637+
638+
ser = df["b"] # populations item_cache["b"]
639+
640+
df._to_dict_of_blocks()
641+
642+
# Check that the to_dict_of_blocks didnt break link between ser and df
643+
ser.values[0] = "foo"
644+
assert df.loc[0, "b"] == "foo"
645+
646+
assert df["b"] is ser

0 commit comments

Comments
 (0)