Skip to content

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

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
JohnNapier opened this issue May 17, 2015 · 2 comments
Closed

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

JohnNapier opened this issue May 17, 2015 · 2 comments
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@JohnNapier
Copy link

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.

@jreback
Copy link
Contributor

jreback commented May 18, 2015

This is a bug here. Pull-requests are welcome. In any event, you should prob just do this.

In [7]: pd.concat({ '+' : df0[df0>=0], '-' : df0[df0<0] })
Out[7]: 
                     0         1         2         3         4
+ 2015-01-01       NaN       NaN  0.592870  0.007121       NaN
  2015-01-02  0.104371  0.618107  1.347137       NaN       NaN
  2015-01-03       NaN  1.477950       NaN       NaN       NaN
  2015-01-04       NaN  1.588150  0.172968       NaN       NaN
  2015-01-05  0.631164  1.921521  0.952758       NaN       NaN
- 2015-01-01 -1.248147 -1.011006       NaN       NaN -0.230722
  2015-01-02       NaN       NaN       NaN -1.097534 -0.768210
  2015-01-03 -1.541076       NaN -0.160582 -0.277739 -1.420439
  2015-01-04 -1.584375       NaN       NaN -0.304879 -0.263297
  2015-01-05       NaN       NaN       NaN -0.450119 -0.230233

@jreback jreback added Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode Dtype Conversions Unexpected or buggy dtype conversions Difficulty Intermediate labels May 18, 2015
@jreback jreback added this to the Next Major Release milestone May 18, 2015
@jreback jreback modified the milestones: 0.16.2, Next Major Release Jun 4, 2015
jreback pushed a commit that referenced this issue Jun 10, 2015
BUG: #10160 DataFrame construction from nested dict with datetime64 index
@jreback
Copy link
Contributor

jreback commented Jun 10, 2015

closed by #10269

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
2 participants