Skip to content

Commit

Permalink
BUG: np.rec.fromarrays doesn't know what to do with M8[ns] close #1720
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Aug 12, 2012
1 parent 4f2e9c9 commit 69d1f75
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pandas 0.8.2
- Handle PeriodIndex in to_datetime instance method (#1703)
- Support StaticTzInfo in DatetimeIndex infrastructure (#1692)
- Allow MultiIndex setops with length-0 other type indexes (#1727)
- Fix handling of DatetimeIndex in DataFrame.to_records (#1720)

pandas 0.8.1
============
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,13 +882,15 @@ def to_records(self, index=True):
y : recarray
"""
if index:
arrays = [self.index] + [self[c] for c in self.columns]
arrays = [self.index.values] + [self[c].values
for c in self.columns]
names = ['index'] + list(map(str, self.columns))
else:
arrays = [self[c] for c in self.columns]
arrays = [self[c].values for c in self.columns]
names = list(map(str, self.columns))

return np.rec.fromarrays(arrays, names=names)
dtype = np.dtype([(x, v.dtype) for x, v in zip(names, arrays)])
return np.rec.fromarrays(arrays, dtype=dtype, names=names)

@classmethod
def from_items(cls, items, columns=None, orient='columns'):
Expand Down
10 changes: 10 additions & 0 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,16 @@ def test_frame_datetime64_mixed_index_ctor_1681(self):
d = DataFrame({'A': 'foo', 'B': ts}, index=dr)
self.assert_(d['B'].isnull().all())

def test_frame_timeseries_to_records(self):
index = date_range('1/1/2000', periods=10)
df = DataFrame(np.random.randn(10, 3), index=index,
columns=['a', 'b', 'c'])

result = df.to_records()
result['index'].dtype == 'M8[ns]'

result = df.to_records(index=False)

def _simple_ts(start, end, freq='D'):
rng = date_range(start, end, freq=freq)
return Series(np.random.randn(len(rng)), index=rng)
Expand Down

0 comments on commit 69d1f75

Please sign in to comment.