From 825cf5565d1055cdfd268ce428f4ff9db41eae72 Mon Sep 17 00:00:00 2001 From: jreback Date: Fri, 28 Mar 2014 16:24:19 -0400 Subject: [PATCH] BUG: bug in BlockManager._get_numeric_data, with invalid combine --- pandas/core/internals.py | 11 +++++------ pandas/tests/test_frame.py | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/pandas/core/internals.py b/pandas/core/internals.py index fe5ae48fea281..d32664559f7fc 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -1907,7 +1907,7 @@ def fillna(self, value, limit=None, inplace=False, downcast=None): values = self.values if inplace else self.values.copy() return [self.make_block(values.get_values(value), fill_value=value)] - + def shift(self, periods, axis=0): """ shift the block by periods """ N = len(self.values.T) @@ -2674,18 +2674,17 @@ def get_data(self, copy=False, columns=None, **kwargs): if len(blocks) == 0: return self.make_empty() - return self.combine(blocks) + return self.combine(blocks, copy=copy) - def combine(self, blocks): + def combine(self, blocks, copy=True): """ return a new manager with the blocks """ indexer = np.sort(np.concatenate([b.ref_locs for b in blocks])) new_items = self.items.take(indexer) new_blocks = [] for b in blocks: - b = b.copy(deep=False) - b.ref_items = new_items - new_blocks.append(b) + b = b.reindex_items_from(new_items, copy=copy) + new_blocks.extend(_valid_blocks(b)) new_axes = list(self.axes) new_axes[0] = new_items return self.__class__(new_blocks, new_axes, do_integrity_check=False) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index a7270dc4517b7..1bbcba0e4caad 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -10424,6 +10424,16 @@ def test_get_numeric_data(self): expected = df.ix[:, []] assert_frame_equal(result, expected) + df = DataFrame.from_dict({'a':[1,2], 'b':['foo','bar'],'c':[np.pi,np.e]}) + result = df._get_numeric_data() + expected = DataFrame.from_dict({'a':[1,2], 'c':[np.pi,np.e]}) + assert_frame_equal(result, expected) + + df = result.copy() + result = df._get_numeric_data() + expected = df + assert_frame_equal(result, expected) + def test_bool_describe_in_mixed_frame(self): df = DataFrame({ 'string_data': ['a', 'b', 'c', 'd', 'e'], @@ -10937,7 +10947,7 @@ def test_rank2(self): expected = DataFrame([[1.0, 3.0, 2.0], [1, 2, 3]]) / 3.0 result = df.rank(1, pct=True) assert_frame_equal(result, expected) - + df = DataFrame([[1, 3, 2], [1, 2, 3]]) expected = df.rank(0) / 2.0 result = df.rank(0, pct=True) @@ -10950,7 +10960,7 @@ def test_rank2(self): result = df.rank(1, numeric_only=False) assert_frame_equal(result, expected) - + expected = DataFrame([[2.0, 1.5, 1.0], [1, 1.5, 2]]) result = df.rank(0, numeric_only=False) assert_frame_equal(result, expected)