python - pandas.to_json 以特定形式输出日期格式

标签 python json pandas datetime

dataframe中日期的原始形式是:

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') 它看起来像这样:

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

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

最佳答案

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

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

我建议您只将此作为 json 转换之前的最后一步,因为您会失去矢量化计算的优势,并且可能会发现内存使用效率较低。

关于python - pandas.to_json 以特定形式输出日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52730953/

相关文章:

python - 我将如何清除/删除跨列具有重复值的行?

python - 循环 Pandas 对象列表表现出奇怪的行为

python - 如何在 python ssl.wrap_socket() 中设置/查找 ca_certs 参数

python - Python 中的并行程序不产生输出

python - 将列转置为行,将前列的 value_counts 显示为 Pandas 中的列值

javascript - 有序列表;使用 javascript 和 json

asp.net - 为什么我应该在 ASP.NET 中使用 JSON?

C# : extract/retrieve child node from JSON structure

python - 无法访问docker容器内的jupyter笔记本

python - 将 pandas 操作表达为管道