Skip to content

pd.DataFrame.from_dict fails when index is numpy.datetime #10160

Closed
@JohnNapier

Description

@JohnNapier

Suppose that we want to create a dataframe from a dictionary.

import pandas as pd,numpy as np
#1) Form dataframe
ix=pd.date_range('1/1/2015',periods=5,freq='D')
df0=pd.DataFrame(np.random.normal(size=(len(ix),5)),index=ix)
#2) Split dataframe into 2 dictionaries, based on sign
d={'+':{},'-':{}}
for loc in df0.index:
    d['+'][loc]=df0.loc[loc][df0.loc[loc]>=0]
    d['-'][loc]=df0.loc[loc][df0.loc[loc]<0]
#3) Convert dictionary to dataframe
print pd.DataFrame.from_dict(d['+'],orient='index')

This returns a dataframe of positive values, with NaN in place of the negative values. Now, it appears the exact same operation fails if we use "df.index.values" instead of "df.index".

d={'+':{},'-':{}}
for loc in df0.index.values: # NOTE .values!
    d['+'][loc]=df0.loc[loc][df0.loc[loc]>=0]
    d['-'][loc]=df0.loc[loc][df0.loc[loc]<0]
print pd.DataFrame.from_dict(d['+'],orient='index')

Now the outcome is a dataframe with all None. I think the problem is some inconsistency between pandas' datetime and numpy's datetime formats.

Pandas version 0.16.1, Numpy version 1.9.2. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDtype ConversionsUnexpected or buggy dtype conversionsReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions