开发者社区> 问答> 正文

在创建CSV之前,panda会转换Epoch时间

我这里有一个简单的脚本,它获取一个响应(原始JSON),然后用panda组织它,并从中创建一个CSV。然而,我注意到其中一列是大纪元时间出于某种原因。在转换到CSV之前,将NPSDate列转换为整个dataframe的正常datetime的正确方法是什么?如果我这样尝试,它会返回这些错误:

 File "NPStoGainsight.py", line 42, in <module>
    NPS['NPSDate'] = pandas.to_datetime(['NPSDate'], unit='ms', utc=True)
  File "/home/ec2-user/.local/lib/python2.7/site-packages/pandas/core/tools/datetimes.py", line 609, in to_datetime
    result = convert_listlike(arg, box, format)
  File "/home/ec2-user/.local/lib/python2.7/site-packages/pandas/core/tools/datetimes.py", line 203, in _convert_listlike_datetimes
    errors=errors)
  File "pandas/_libs/tslib.pyx", line 398, in pandas._libs.tslib.array_with_unit_to_datetime
NPS = pandas.DataFrame(reportData)

renamedColumns = {
        'account_salesforce_total_arr__c' : 'totalARR',
        'account_salesforce_pricing_package__c' : 'pricingPackage',
        'account_salesforce_customer_classification__c' : 'customerClassification',
        'account_salesforce_account_manager_full_name__c' : 'CSM',
        'account_agent_plancommit' : 'planCommit',
        'poll_543uce0tt59_eqOSduL4o25wbYsmfGbmj_pgBXY_pollResponse' : 'NPSComment',
        'account_auto_id' : 'accountId',
        'visitor_agent_email' : 'email',
        'poll_tudk5p2oot9_eqOSduL4o25wbYsmfGbmj_pgBXY_pollResponse' : 'NPSResponse',
        'poll_tudk5p2oot9_eqOSduL4o25wbYsmfGbmj_pgBXY_pollTime' : 'NPSDate'
}

NPS = NPS.rename(columns = renamedColumns)

NPS = pandas.to_datetime(NPS['NPSDate'], unit='ms')

NPS.to_csv("NPStoGainsight.csv", encoding='utf-8', index=False)

使用大纪元时间的MS日期格式(例如:1.5667E+12) 问题来源StackOverflow 地址:/questions/59381038/pandas-convert-epoch-time-before-creating-csv

展开
收起
kun坤 2019-12-30 09:59:23 496 0
0 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Data Wrangling with PySpark for Data Scientists Who Know Pandas 立即下载
PySpark for Time Series Analysis 立即下载
数据采集:日志数...[jing.luojing].1477375627.pdf 立即下载