Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,12 +909,7 @@ def to_dict(self, copy: bool = True):
Returns
-------
values : a dict of dtype -> BlockManager

Notes
-----
This consolidates based on str(dtype)
"""
self._consolidate_inplace()

bd: Dict[str, List[Block]] = {}
for b in self.blocks:
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/frame/test_block_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,21 @@ def test_add_column_with_pandas_array(self):
assert type(df["c"]._mgr.blocks[0]) == ObjectBlock
assert type(df2["c"]._mgr.blocks[0]) == ObjectBlock
tm.assert_frame_equal(df, df2)


def test_to_dict_of_blocks_item_cache():
# Calling to_dict_of_blocks should not poison item_cache
df = pd.DataFrame({"a": [1, 2, 3, 4], "b": ["a", "b", "c", "d"]})
df["c"] = pd.arrays.PandasArray(np.array([1, 2, None, 3], dtype=object))
mgr = df._mgr
assert len(mgr.blocks) == 3 # i.e. not consolidated

ser = df["b"] # populations item_cache["b"]

df._to_dict_of_blocks()

# Check that the to_dict_of_blocks didnt break link between ser and df
ser.values[0] = "foo"
assert df.loc[0, "b"] == "foo"

assert df["b"] is ser