pandas.to_json 以特定形式输出日期格式 [英] pandas.to_json output date format in specific form

查看:184
本文介绍了pandas.to_json 以特定形式输出日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dataframe中日期的原始形式是:

The original form of date in dataframe is:

Date                                                                   
2018-09-17          12.83  12.92  12.38  12.65         12.65  1937329.0
2018-09-10          12.92  13.12  12.81  12.83         12.83  1150470.0

转成json后,df.to_json(orient='index',date_format='iso')它看起来像这样:

After converted to json, df.to_json(orient='index',date_format='iso') it looks like this:

"2018-09-17T00:00:00Z":{"

有什么办法可以解决这个问题吗?

any way to fix this?

推荐答案

最简单的解决方法是首先将 datetime 系列转换为 object dtype 系列字符串:

The easiest fix is to first convert your datetime series to an object dtype series of strings:

df['Date'] = df['Date'].dt.strftime('%Y-%m-%d')

我建议您只在 json 转换之前的最后一步执行此操作,因为您会失去矢量化计算的优势,并且可能会看到内存使用效率较低.

I advise you only do this as a final step prior to json conversion, as you lose benefits of vectorised computations and likely will see less efficient memory usage.

这篇关于pandas.to_json 以特定形式输出日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆