From 831b9ce2f7659c40b5175f3a1bafafe0a60ebbe1 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 24 Jun 2020 20:27:06 -0700 Subject: [PATCH 1/2] REF: move consolidate_inplace call to DataFrame level --- pandas/core/frame.py | 1 + pandas/core/generic.py | 3 ++- pandas/core/internals/managers.py | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 521d16ac0b905..199fd4e858b5e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1341,6 +1341,7 @@ def to_numpy( array([[1, 3.0, Timestamp('2000-01-01 00:00:00')], [2, 4.5, Timestamp('2000-01-02 00:00:00')]], dtype=object) """ + self._consolidate_inplace() result = self._mgr.as_array( transpose=self._AXIS_REVERSED, dtype=dtype, copy=copy, na_value=na_value ) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 488dd00686a17..e9f808cc4e225 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5321,6 +5321,7 @@ def values(self) -> np.ndarray: ['lion', 80.5, 1], ['monkey', nan, None]], dtype=object) """ + self._consolidate_inplace() return self._mgr.as_array(transpose=self._AXIS_REVERSED) @property @@ -6517,7 +6518,7 @@ def replace( f"Replacement lists must match in length. " f"Expecting {len(to_replace)} got {len(value)} " ) - + self._consolidate_inplace() new_data = self._mgr.replace_list( src_list=to_replace, dest_list=value, diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 6055a6205d286..843e7ce40fef8 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -812,7 +812,7 @@ def as_array( .values.to_numpy(dtype=dtype, na_value=na_value) .reshape(self.blocks[0].shape) ) - elif self._is_single_block or not self.is_mixed_type: + elif self._is_single_block: arr = np.asarray(self.blocks[0].get_values()) if dtype: arr = arr.astype(dtype, copy=False) From bdacd78ec88e4cc8965770cd997fb7ef5393fa8b Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 25 Jun 2020 15:35:40 -0700 Subject: [PATCH 2/2] TST: test for cleared item_cache --- pandas/tests/frame/test_block_internals.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/tests/frame/test_block_internals.py b/pandas/tests/frame/test_block_internals.py index e2910a2eb6100..d5554860c034d 100644 --- a/pandas/tests/frame/test_block_internals.py +++ b/pandas/tests/frame/test_block_internals.py @@ -86,9 +86,14 @@ def test_modify_values(self, float_frame): # unconsolidated float_frame["E"] = 7.0 + col = float_frame["E"] float_frame.values[6] = 6 assert (float_frame.values[6] == 6).all() + # check that item_cache was cleared + assert float_frame["E"] is not col + assert (col == 7).all() + def test_boolean_set_uncons(self, float_frame): float_frame["E"] = 7.0