Skip to content

Commit

Permalink
BUG: index name in to_records #2161
Browse files Browse the repository at this point in the history
  • Loading branch information
changhiskhan committed Nov 2, 2012
1 parent 2086ae2 commit 6247c8c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,16 @@ def to_records(self, index=True):
if index:
arrays = [self.index.values] + [self[c].values
for c in self.columns]
names = ['index'] + list(map(str, self.columns))
count = 0
index_names = self.index.names
if isinstance(self.index, MultiIndex):
for i, n in enumerate(index_names):
if n is None:
index_names[i] = 'level_%d' % count
count += 1
elif index_names[0] is None:
index_names = ['index']
names = index_names + list(map(str, self.columns))
else:
arrays = [self[c].values for c in self.columns]
names = list(map(str, self.columns))
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2572,6 +2572,21 @@ def test_to_records_floats(self):
df = DataFrame(np.random.rand(10,10))
df.to_records()

def test_to_recods_index_name(self):
df = DataFrame(np.random.randn(3, 3))
df.index.name = 'X'
rs = df.to_records()
self.assert_('X' in rs.dtype.fields)

df = DataFrame(np.random.randn(3, 3))
rs = df.to_records()
self.assert_('index' in rs.dtype.fields)

df.index = MultiIndex.from_tuples([('a', 'x'), ('a', 'y'), ('b', 'z')])
df.index.names = ['A', None]
rs = df.to_records()
self.assert_('level_0' in rs.dtype.fields)

def test_join_str_datetime(self):
str_dates = ['20120209' , '20120222']
dt_dates = [datetime(2012,2,9), datetime(2012,2,22)]
Expand Down

0 comments on commit 6247c8c

Please sign in to comment.