From 9ef3b902806f20140f5133c5043b773f6c0c79bf Mon Sep 17 00:00:00 2001 From: Ka Wo Chen Date: Wed, 3 Jun 2015 21:35:32 -0400 Subject: [PATCH] BUG: GH10160 DataFrame constructor from nested dict where index is datetime64 --- doc/source/whatsnew/v0.17.0.txt | 2 ++ pandas/core/frame.py | 5 +++-- pandas/tests/test_frame.py | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index f96a2954f98a2..4b5cfabfdedd4 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -95,3 +95,5 @@ Bug Fixes - Bug where infer_freq infers timerule (WOM-5XXX) unsupported by to_offset (:issue:`9425`) + +- Bug in DataFrame constructor from nested ``dict`` type where the index is ``datatime64`` (:issue:`10160`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f36108262432d..623a36503ac38 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5095,8 +5095,9 @@ def _homogenize(data, index, dtype=None): v = v.reindex(index, copy=False) else: if isinstance(v, dict): - if oindex is None: - oindex = index.astype('O') + if lib.infer_dtype(v) in ['datetime64']: + v = dict((lib.Timestamp(key), v[key]) for key in v) + oindex = index.astype('O') if type(v) == dict: # fast cython method v = lib.fast_multiget(v, oindex.values, default=NA) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 4964d13f7ac28..8bde06dc45704 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -2941,6 +2941,21 @@ def test_constructor_dict_multiindex(self): df = df.reindex(columns=expected.columns, index=expected.index) check(df, expected) + def test_constructor_dict_datetime64_index(self): + # GH 10160 + data = {1: {np.datetime64('1990-03-15'): 1}, + 2: {np.datetime64('1989-12-03'): 3}, + 3: {np.datetime64('1988-11-06'): 5}, + 4: {np.datetime64('1984-02-19'): 7}} + data_expected = {1: {Timestamp('1990-03-15'): 1}, + 2: {Timestamp('1989-12-03'): 3}, + 3: {Timestamp('1988-11-06'): 5}, + 4: {Timestamp('1984-02-19'): 7}} + result = DataFrame(data) + expected = DataFrame(data_expected) + assert_frame_equal(result, expected) + + def _check_basic_constructor(self, empty): "mat: 2d matrix with shpae (3, 2) to input. empty - makes sized objects" mat = empty((2, 3), dtype=float)