参考:Python pandas convert unix timestamp with timezone into datetime
在此主题上进行了搜索,但仍然找不到答案。
我有一个数据帧,其格式如下:
df timestamp
1 1549914000
2 1549913400
3 1549935000
3 1549936800
5 1549936200
我使用以下方法将纪元转换为日期:
df['date'] = pd.to_datetime(df['timestamp'], unit='s')
此行将产生一个始终比我当地时间晚8小时的日期。
因此,我按照链接中的示例对亚洲/新加坡使用了apply + tz.localize,我在上述代码之后的下一行尝试了以下代码。
df['date'] = df.apply(lambda x: x['date'].tz_localize(x['Asia/Singapore']), axis=1)
但是python返回如下错误:
Traceback (most recent call last):
File "/home/test/script.py", line 479, in <module>
schedule.every(10).minutes.do(main).run()
File "/opt/cloudera/parcels/Anaconda-4.0.0/lib/python2.7/site-packages/schedule/__init__.py", line 411, in run
ret = self.job_func()
File "/home/test/script.py", line 361, in main
df['date'] = df.apply(localize_ts, axis = 1)
File "/opt/cloudera/parcels/Anaconda-4.0.0/lib/python2.7/site-packages/pandas/core/frame.py", line 4877, in apply
ignore_failures=ignore_failures)
File "/opt/cloudera/parcels/Anaconda-4.0.0/lib/python2.7/site-packages/pandas/core/frame.py", line 4973, in _apply_standard
results[i] = func(v)
File "/home/test/script.py", line 359, in localize_ts
return pd.to_datetime(row['date']).tz_localize(row['Asia/Singapore'])
File "/opt/cloudera/parcels/Anaconda-4.0.0/lib/python2.7/site-packages/pandas/core/series.py", line 623, in __getitem__
result = self.index.get_value(self, key)
File "/opt/cloudera/parcels/Anaconda-4.0.0/lib/python2.7/site-packages/pandas/core/indexes/base.py", line 2574, in get_value
raise e1
KeyError: ('Asia/Singapore', u'occurred at index 0')
我是否正确替换了.tz_localize(x ['tz'])?
答案 0 :(得分:1)
按照编写的方式,您的代码正在寻找名为Asia/Singapore
的列。尝试以下方法:
df['date'] = df['date'].dt.tz_localize('Asia/Singapore')
答案 1 :(得分:1)
您可以尝试
import numpy as np
import pandas as pd
df = pd.DataFrame({'timestamp': [1549952400, 1549953600]},index=['1', '2'])
df['timestamp2'] = df['timestamp'] + 28800
df['date'] = pd.to_datetime(df['timestamp2'], unit='s')
df = df.drop('timestamp2', 1)