Skip to content

BUG: to_dict_of_blocks failing to invalidate item_cache #35874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 25, 2020
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