diff --git a/RELEASE.rst b/RELEASE.rst index 94926034c48c0..31ded56bb9263 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -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 ============ diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 60dd9b15ce46c..b72f9ce4c6566 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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'): diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index 4724ad9b76c14..558c6e33e2202 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -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)