Skip to content

Commit 6b959f7

Browse files
committed
BUG: in as_matrix with subset of columns with single-block
1 parent 870eca6 commit 6b959f7

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

pandas/core/frame.py

+3
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,9 @@ def _insert_item(self, key, value):
822822
Series/TimeSeries will be conformed to the DataFrame's index to
823823
ensure homogeneity.
824824
"""
825+
# Need to make sure new columns (which go into the BlockManager as new
826+
# blocks) are always copied
827+
825828
if hasattr(value, '__iter__'):
826829
if isinstance(value, Series):
827830
if value.index.equals(self.index):

pandas/core/internals.py

+2
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ def as_matrix(self, columns=None):
421421
if columns is None or blk.columns.equals(columns):
422422
# if not, then just call interleave per below
423423
mat = blk.values
424+
else:
425+
mat = self.reindex_columns(columns).as_matrix()
424426
else:
425427
if columns is None:
426428
mat = _interleave(self.blocks, self.columns)

pandas/tests/test_frame.py

+5
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,11 @@ def test_as_matrix(self):
12841284
mat = self.mixed_frame.as_matrix(['foo', 'A'])
12851285
self.assertEqual(mat[0, 0], 'bar')
12861286

1287+
# single block corner case
1288+
mat = self.frame.as_matrix(['A', 'B'])
1289+
expected = self.frame.reindex(columns=['A', 'B']).values
1290+
assert_almost_equal(mat, expected)
1291+
12871292
def test_values(self):
12881293
pass
12891294

0 commit comments

Comments
 (0)