Skip to content

BUG: GH10160 DataFrame construction from nested dict #10267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
5 changes: 3 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down