简体   繁体   English

带有python本机日期时间类型而不是时间戳的pandas to_dict

[英]pandas to_dict with python native datetime type and not timestamp

I have a pandas DataFrame df that contains Timesatamp columns.我有一个pandas DataFrame df包含Timesatamp列。

I wish to create an iterator of rows (either via the iter.. methods or via to_dict ) from df where the Timesatamp values are python datetime .我希望从df创建一个行迭代器(通过iter..方法或通过to_dict ),其中Timesatamp值是 python datetime

I have tried doing this我试过这样做

for col in df.select_dtypes(['datetime']):
        df[col] = df[col].dt.to_pydatetime()

however it seems like the columns is still Timesatamp when using the above mentioned iterator methods.但是,在使用上述迭代器方法时,列似乎仍然是Timesatamp Is there a 'batch'y way to achieve this apart from manualy converting each values when its iterated upon?除了在迭代时手动转换每个值之外,是否有一种“批量”方式来实现这一点?


example例子

df = pd.DataFrame({'d': pd.date_range('2018-01-01', freq='12h', periods=2), 'a':[1,2]})
for col in df.select_dtypes(['datetime']):
    df[col] = df[col].dt.to_pydatetime()
print(df.to_dict('records'))

the output:输出:

[{'d': Timestamp('2018-01-01 00:00:00'), 'a': 1}, {'d': Timestamp('2018-01-01 12:00:00'), 'a': 2}]

the desired output:所需的输出:

[{'d': datetime.datetime(2018, 1, 1, 0, 0), 'a': 1}, {'d': datetime.datetime(2018, 1, 1, 12, 0), 'a': 2}]

You can try你可以试试

df[col] = pd.Series(df[col].dt.to_pydatetime(), dtype = object)

instead of代替

df[col] = df[col].dt.to_pydatetime()

One workaround is the following: 一种解决方法如下:

#Initialize empty records list
records=[]

#Iterate over datetime columns
for col in df.select_dtypes(['datetime']):

    #Create a temp list consisting of dictionaries
    temp_df=[{col: r.to_pydatetime()} for r in df[col]]

    #Add it to records
    records+=temp_df

which result is: 结果是:

records

[{'d': datetime.datetime(2018, 1, 1, 0, 0)},
 {'d': datetime.datetime(2018, 1, 2, 0, 0)}]

Try it:尝试一下:

df["d"]=df.d.apply(lambda t: t.date())                                                                              
df.d.to_dict()                                                                                                      

{0: datetime.date(2018, 1, 1), 1: datetime.date(2018, 1, 2)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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