在以下系列中:
0 1411161507178
1 1411138436009
2 1411123732180
3 1411167606146
4 1411124780140
5 1411159331327
6 1411131745474
7 1411151831454
8 1411152487758
9 1411137160544
Name: my_series, dtype: int64
Run Code Online (Sandbox Code Playgroud)
此命令(转换为时间戳,本地化并转换为EST)有效:
pd.to_datetime(my_series, unit='ms').apply(lambda x: x.tz_localize('UTC').tz_convert('US/Eastern'))
Run Code Online (Sandbox Code Playgroud)
但是这个失败了:
pd.to_datetime(my_series, unit='ms').tz_localize('UTC').tz_convert('US/Eastern')
Run Code Online (Sandbox Code Playgroud)
有:
TypeError Traceback (most recent call last)
<ipython-input-3-58187a4b60f8> in <module>()
----> 1 lua = pd.to_datetime(df[column], unit='ms').tz_localize('UTC').tz_convert('US/Eastern')
/Users/josh/anaconda/envs/py34/lib/python3.4/site-packages/pandas/core/generic.py in tz_localize(self, tz, axis, copy, infer_dst)
3492 ax_name = self._get_axis_name(axis)
3493 raise TypeError('%s is not a valid DatetimeIndex or PeriodIndex' %
-> 3494 ax_name)
3495 else:
3496 ax …Run Code Online (Sandbox Code Playgroud) 我正在尝试转换 csv 文件中时间/日期列(“Created_At”)中“GMT”时间的所有实例,以便将其全部格式化为“EST”。请参阅以下内容:
import pandas as pd
from pandas.tseries.resample import TimeGrouper
from pandas.tseries.offsets import DateOffset
from pandas.tseries.index import DatetimeIndex
cambridge = pd.read_csv('\Users\cgp\Desktop\Tweets.csv')
cambridge['Created_At'] = pd.to_datetime(pd.Series(cambridge['Created_At']))
cambridge.set_index('Created_At', drop=False, inplace=True)
cambridge.index = cambridge.index.tz_localize('GMT').tz_convert('EST')
cambridge.index = cambridge.index - DateOffset(hours = 12)
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
cambridge.index = cambridge.index.tz_localize('GMT').tz_convert('EST')
Run Code Online (Sandbox Code Playgroud)
AttributeError: 'Index' 对象没有属性 'tz_localize'
我尝试了各种不同的东西,但我不明白为什么 Index 对象无法识别 tz_attribute。非常感谢你的帮助!