Skip to content

Commit

Permalink
BUG: tweak for DataFrame.to_records datetimeindex fix #1908
Browse files Browse the repository at this point in the history
  • Loading branch information
changhiskhan committed Dec 18, 2012
1 parent f971840 commit 38333f4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
1 change: 0 additions & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 38333f4

Please sign in to comment.