我有一個數據幀以下幾點:從熊貓轉換時區的時間戳
> df['timestamps'].loc[0]
Timestamp('2014-09-02 20:24:00')
我知道時區(我認爲這是GMT)它使用,並想整列轉換爲EST。我如何在熊貓中做到這一點?
僅供參考,我發現這些其他線程:
- Changing a unix timestamp to a different timezone
- Pandas: Read Timestamp from CSV in GMT then resample
,但他們與datetime
時間戳工作。例如: -
> datetime.datetime.fromtimestamp(df['timestamps'].loc[0], tz=None)
returns:
TypeError Traceback (most recent call last)
----> 2 datetime.datetime.fromtimestamp(ts, tz=None)
TypeError: an integer is required (got type Timestamp)
謝謝,但是當我嘗試: 'DF [ '時間戳'] [0] .tz_convert( '美國/東部')'我得到: '異常:無法轉換TZ-天真的時間戳,使用tz_localize或本地化' – 2014-09-03 22:02:42
同樣,如果我嘗試直接使用列: 'df ['timestamps']。tz_convert('US/Eastern')' 'TypeError:索引不是有效的DatetimeIndex或PeriodIndex' – 2014-09-03 22:03:29
@ user815423426您需要先tz_localize(如我的答案結尾處所述),如果您不知道您開始使用什麼時區,則無法轉換爲時區。 – 2014-09-03 22:05:31