我可以在pandas read_json中更改默认时间戳解释

时间:2017-06-30 17:10:23

标签: python json pandas timestamp

我有一个JSON数据文件,遗憾的是名为“timestamp”字段。时间戳的格式为 currentArray ( [0] => /home/MY_DOMAIN/hardeen/index.php [1] => /home/MY_DOMAIN/hardeen/core/construct.php [2] => /home/MY_DOMAIN/hardeen/core/template.php [3] => /home/MY_DOMAIN/hardeen/bin/tags.php [4] => /home/MY_DOMAIN/hardeen/bin/systemFunction.php ) ,但pandas.json_read()将此解释为UNIX纪元时间戳,因此该字段将被解释为1975年的某个时间。

有没有办法覆盖read_json行为以提供格式说明符 - 类似于pandas.to_json(...,date_format =“...”))吗?

"%y%m%d%H%M%S"

输出是:

import StringIO
import pandas as pd
my_json_data='''[
  {
    "itemId": "alpha:136:1",
    "testTime": 12.449,
    "workTime": 152.5,
    "project": "alpha",
    "user": "user100021-su7d",
    "timestamp": "170520161430",
    "accuracy": 1
  },
  {
    "itemId": "alpha:136:10",
    "testTime": 4.114,
    "workTime": 152.5,
    "project": "alpha",
    "user": "user100021-su7d",
    "timestamp": "170520161430",
    "accuracy": 0.8890000000000001
  },
  {
    "itemId": "alpha:136:100",
    "testTime": 5.114,
    "workTime": 43.4,
    "project": "alpha",
    "user": "user100021-su7d",
    "timestamp": "170522150338",
    "accuracy": 0.875
  }
]
'''

my_df = pd.read_json(StringIO.StringIO(my_json_data))
my_df["timestamp"]

1 个答案:

答案 0 :(得分:0)

不幸的是,我在Pandas文档中找不到类似的选项,但是这个解决方法应该这样做:

my_df = pd.read_json(StringIO.StringIO(my_json_data),convert_dates = False)
my_df['timestamp'] = pd.to_datetime(my_df['timestamp'],format='%y%m%d%H%M%S')
print my_df["timestamp"]

我只是要求pandas在读取JSON时不处理字符串到日期时间,然后通过提供格式手动执行它。这应该正确显示日期。

希望这有帮助!