Skip to content

Commit ca3f889

Browse files
jbrockmendelfangchenli
authored andcommitted
BUG: item_cache not cleared on DataFrame.values (pandas-dev#34999)
1 parent f189cfb commit ca3f889

File tree

4 files changed

+9
-2
lines changed

4 files changed

+9
-2
lines changed

pandas/core/frame.py

+1
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,7 @@ def to_numpy(
13411341
array([[1, 3.0, Timestamp('2000-01-01 00:00:00')],
13421342
[2, 4.5, Timestamp('2000-01-02 00:00:00')]], dtype=object)
13431343
"""
1344+
self._consolidate_inplace()
13441345
result = self._mgr.as_array(
13451346
transpose=self._AXIS_REVERSED, dtype=dtype, copy=copy, na_value=na_value
13461347
)

pandas/core/generic.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5330,6 +5330,7 @@ def values(self) -> np.ndarray:
53305330
['lion', 80.5, 1],
53315331
['monkey', nan, None]], dtype=object)
53325332
"""
5333+
self._consolidate_inplace()
53335334
return self._mgr.as_array(transpose=self._AXIS_REVERSED)
53345335

53355336
@property
@@ -6530,7 +6531,7 @@ def replace(
65306531
f"Replacement lists must match in length. "
65316532
f"Expecting {len(to_replace)} got {len(value)} "
65326533
)
6533-
6534+
self._consolidate_inplace()
65346535
new_data = self._mgr.replace_list(
65356536
src_list=to_replace,
65366537
dest_list=value,

pandas/core/internals/managers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ def as_array(
812812
.values.to_numpy(dtype=dtype, na_value=na_value)
813813
.reshape(self.blocks[0].shape)
814814
)
815-
elif self._is_single_block or not self.is_mixed_type:
815+
elif self._is_single_block:
816816
arr = np.asarray(self.blocks[0].get_values())
817817
if dtype:
818818
arr = arr.astype(dtype, copy=False)

pandas/tests/frame/test_block_internals.py

+5
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,14 @@ def test_modify_values(self, float_frame):
8686

8787
# unconsolidated
8888
float_frame["E"] = 7.0
89+
col = float_frame["E"]
8990
float_frame.values[6] = 6
9091
assert (float_frame.values[6] == 6).all()
9192

93+
# check that item_cache was cleared
94+
assert float_frame["E"] is not col
95+
assert (col == 7).all()
96+
9297
def test_boolean_set_uncons(self, float_frame):
9398
float_frame["E"] = 7.0
9499

0 commit comments

Comments
 (0)