Skip to content

Commit f971840

Browse files
svakshachanghiskhan
authored andcommitted
BUG: fixed DataFrame.to_records() error with datetime64, #1908
1 parent 1751bae commit f971840

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

pandas/core/frame.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1066,8 +1066,11 @@ def to_records(self, index=True):
10661066
y : recarray
10671067
"""
10681068
if index:
1069-
arrays = [self.index.values] + [self[c].values
1070-
for c in self.columns]
1069+
if (com.is_datetime64_dtype(self.index)):
1070+
arrays = [self.index.asobject.values] + [self[c].values for c in self.columns]
1071+
else:
1072+
arrays = [self.index.values] + [self[c].values for c in self.columns]
1073+
10711074
count = 0
10721075
index_names = self.index.names
10731076
if isinstance(self.index, MultiIndex):

pandas/tests/test_frame.py

+5
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,11 @@ def test_to_json_except(self):
26682668
df = DataFrame([1, 2, 3])
26692669
self.assertRaises(ValueError, df.to_json, orient="garbage")
26702670

2671+
def test_array_index_asobject(self):
2672+
df = DataFrame([["one", "two", "three"], ["four", "five", "six"]], index=pan.date_range("2012-01-01", "2012-01-02"))
2673+
self.assert_(df.to_records()['index'][0] == df.index[0])
2674+
2675+
26712676
def test_from_records_to_records(self):
26722677
# from numpy documentation
26732678
arr = np.zeros((2,),dtype=('i4,f4,a10'))

0 commit comments

Comments
 (0)