diff --git a/pandas/core/common.py b/pandas/core/common.py index 2c2237eaf4ec6..15f6cb6412c78 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -649,7 +649,6 @@ def _possibly_cast_item(obj, item, dtype): elif not issubclass(dtype, (np.integer, np.bool_)): # pragma: no cover raise ValueError("Unexpected dtype encountered: %s" % dtype) - def _is_bool_indexer(key): if isinstance(key, np.ndarray) and key.dtype == np.object_: key = np.asarray(key) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a633bcf96c975..30bb4eb9ac096 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1051,7 +1051,7 @@ def from_records(cls, data, index=None, exclude=None, columns=None, return DataFrame(mgr) - def to_records(self, index=True): + def to_records(self, index=True, convert_datetime64=True): """ Convert DataFrame to record array. Index will be put in the 'index' field of the record array if requested @@ -1060,16 +1060,20 @@ def to_records(self, index=True): ---------- index : boolean, default True Include index in resulting record array, stored in 'index' field + convert_datetime64 : boolean, default True + Whether to convert the index to datetime.datetime if it is a + DatetimeIndex Returns ------- y : recarray """ if index: - if (com.is_datetime64_dtype(self.index)): - arrays = [self.index.asobject.values] + [self[c].values for c in self.columns] + if com.is_datetime64_dtype(self.index) and convert_datetime64: + ix_vals = [self.index.to_pydatetime()] else: - arrays = [self.index.values] + [self[c].values for c in self.columns] + ix_vals = [self.index.values] + arrays = ix_vals + [self[c].values for c in self.columns] count = 0 index_names = self.index.names diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 3bbdc1b4e2ec6..cf485f70ffbc8 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -2668,10 +2668,14 @@ def test_to_json_except(self): df = DataFrame([1, 2, 3]) self.assertRaises(ValueError, df.to_json, orient="garbage") - def test_array_index_asobject(self): - df = DataFrame([["one", "two", "three"], ["four", "five", "six"]], index=pan.date_range("2012-01-01", "2012-01-02")) + def test_to_records_dt64(self): + df = DataFrame([["one", "two", "three"], + ["four", "five", "six"]], + index=pan.date_range("2012-01-01", "2012-01-02")) self.assert_(df.to_records()['index'][0] == df.index[0]) + rs = df.to_records(convert_datetime64=False) + self.assert_(rs['index'][0] == df.index.values[0]) def test_from_records_to_records(self): # from numpy documentation