Skip to content

Commit 69d1f75

Browse files
committed
BUG: np.rec.fromarrays doesn't know what to do with M8[ns] close #1720
1 parent 4f2e9c9 commit 69d1f75

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

RELEASE.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pandas 0.8.2
8484
- Handle PeriodIndex in to_datetime instance method (#1703)
8585
- Support StaticTzInfo in DatetimeIndex infrastructure (#1692)
8686
- Allow MultiIndex setops with length-0 other type indexes (#1727)
87+
- Fix handling of DatetimeIndex in DataFrame.to_records (#1720)
8788

8889
pandas 0.8.1
8990
============

pandas/core/frame.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -882,13 +882,15 @@ def to_records(self, index=True):
882882
y : recarray
883883
"""
884884
if index:
885-
arrays = [self.index] + [self[c] for c in self.columns]
885+
arrays = [self.index.values] + [self[c].values
886+
for c in self.columns]
886887
names = ['index'] + list(map(str, self.columns))
887888
else:
888-
arrays = [self[c] for c in self.columns]
889+
arrays = [self[c].values for c in self.columns]
889890
names = list(map(str, self.columns))
890891

891-
return np.rec.fromarrays(arrays, names=names)
892+
dtype = np.dtype([(x, v.dtype) for x, v in zip(names, arrays)])
893+
return np.rec.fromarrays(arrays, dtype=dtype, names=names)
892894

893895
@classmethod
894896
def from_items(cls, items, columns=None, orient='columns'):

pandas/tseries/tests/test_timeseries.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,16 @@ def test_frame_datetime64_mixed_index_ctor_1681(self):
10751075
d = DataFrame({'A': 'foo', 'B': ts}, index=dr)
10761076
self.assert_(d['B'].isnull().all())
10771077

1078+
def test_frame_timeseries_to_records(self):
1079+
index = date_range('1/1/2000', periods=10)
1080+
df = DataFrame(np.random.randn(10, 3), index=index,
1081+
columns=['a', 'b', 'c'])
1082+
1083+
result = df.to_records()
1084+
result['index'].dtype == 'M8[ns]'
1085+
1086+
result = df.to_records(index=False)
1087+
10781088
def _simple_ts(start, end, freq='D'):
10791089
rng = date_range(start, end, freq=freq)
10801090
return Series(np.random.randn(len(rng)), index=rng)

0 commit comments

Comments
 (0)