Skip to content

Commit 50b185f

Browse files
author
y-p
committed
Merge branch 'GH3189'
* GH3189: BUG: DataFrame.to_records(index=True) doesn't work with MultiIndex GH3189 TST: DataFrame.to_records(index=True) doesn't work with MultiIndex GH3189
2 parents 4c1ca12 + 72eb647 commit 50b185f

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

pandas/core/frame.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1100,8 +1100,13 @@ def to_records(self, index=True, convert_datetime64=True):
11001100
if com.is_datetime64_dtype(self.index) and convert_datetime64:
11011101
ix_vals = [self.index.to_pydatetime()]
11021102
else:
1103-
ix_vals = [self.index.values]
1104-
arrays = ix_vals + [self[c].values for c in self.columns]
1103+
if isinstance(self.index, MultiIndex):
1104+
# array of tuples to numpy cols. copy copy copy
1105+
ix_vals = map(np.array,zip(*self.index.values))
1106+
else:
1107+
ix_vals = [self.index.values]
1108+
1109+
arrays = ix_vals+ [self[c].values for c in self.columns]
11051110

11061111
count = 0
11071112
index_names = self.index.names

pandas/tests/test_frame.py

+10
Original file line numberDiff line numberDiff line change
@@ -3303,6 +3303,16 @@ def test_to_records_dt64(self):
33033303
rs = df.to_records(convert_datetime64=False)
33043304
self.assert_(rs['index'][0] == df.index.values[0])
33053305

3306+
def test_to_records_with_multindex(self):
3307+
# GH3189
3308+
index = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
3309+
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
3310+
data = np.zeros((8, 4))
3311+
df = DataFrame(data, index=index)
3312+
r = df.to_records(index=True)['level_0']
3313+
self.assertTrue('bar' in r)
3314+
self.assertTrue('one' not in r)
3315+
33063316
def test_to_records_with_Mapping_type(self):
33073317
import email
33083318
from email.parser import Parser

0 commit comments

Comments
 (0)