简体   繁体   中英

Always got NaN when converting dictionary to multiindex pandas DataFrame if the index including datetime.date object

In the following example, when a date object is in the index, pandas can not generate DataFrame correctly. Instead of 6, it shows NaN.

import pandas as pd
import datetime

X = {(datetime.date(2020, 6, 3), 'A'): {'B': 6}}

df = pd.DataFrame.from_dict(X, orient='index')

print(df)
               B
2020-06-03 A NaN

It's working fine without datetime.date as index.

X = {('OK', 'A'): {'B': 6}}

df = pd.DataFrame.from_dict(X, orient='index')

print(df)
      B
OK A  6

What's wrong here? I'm using python 3.8.3 and pandas 1.0.4

Interestingly, a single datetime index works fine, a compound datetime index with any other type (including another datetime ) do give NaN . The only workaround I have found is to use pandas native datetime :

import pandas as pd

X = {(pd.datetime(2020, 6, 3), 'A'): {'B': 6}}

df = pd.DataFrame.from_dict(X, orient='index')

print(df)

which yields

FutureWarning: The pandas.datetime class is deprecated and will be removed from pandas in a future version. Import from datetime module instead.
  X = {(pd.datetime(2020, 6, 3), 'A'): {'B': 6}}
              B
2020-06-03 A  6

However the warning indicates that this is not a future proof solution and your original form will be the only option (which is currently buggy). I have Python 3.7.6 and pandas 1.0.3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM