首页 > 解决方案 > 带有日期时间索引的熊猫 to_json 和 read_json

问题描述

我有一个pd.DataFrame带有索引类型的Timestamp

当我to_json使用数据框时,索引将转换为类似'1629849600000'.

我不能pd.to_datetime这样或那样datetime.datetime.fromtimestamp

如果我 pd.DataFrame json,它是格式良好的:

在此处输入图像描述

如果我pd.to_datetime这样做,它会失败

OverflowError: Python int too large to convert to C long

标签: pythonpandas

解决方案


pandas.to_datetime与参数一起使用unit='ms'

import pandas as pd

print(pd.to_datetime(1629417600000, unit='ms'))  # 2021-08-20 00:00:00

推荐阅读