我正在閱讀帶日期時間但沒有時區數據的csv,但是一旦我使用datetime列作爲索引,就會添加(n不正確)時區偏移量。我怎樣才能防止這種情況發生?將datetime64系列設置爲熊貓數據框索引自動添加時區偏移
數據:
Time (UTC),Open,High,Low,Close,Volume
2005.01.03 00:00:00,1.8275,1.858,1.7971,1.819,41998.5
2005.01.10 00:00:00,1.8095,1.8376,1.771,1.766,46353.9
是每週OHLC數據。
import pandas as pd
df = pd.read_csv("test.csv", parse_dates=["Time (UTC)"])
在數據讀取後,沒有時區偏移:
in:
df["Time (UTC)"].head(2)
out:
0 1973-02-26
1 1973-03-05
Name: Time (UTC), dtype: datetime64[ns]
但是,當我設置此數據爲指標,一個時區偏移量添加:
in:
df.index = df["Time (UTC)"]
df.index.values[:1]
out:
array(['1973-02-26T01:00:00.000000000+0100'], dtype='datetime64[ns]')
使用df.index
,我回到dtype='datetime64[ns]'
,所以即使添加了一個時區偏移量,也沒有添加時區(順便說一句,這似乎也有夏令時)。如果我將時區設置爲UTC,df = df.tz_localize("UTC")
,df.index
顯示我dtype = 'datetime64[ns, UTC]'
。但是,它對偏移沒有影響。
因爲我知道數據在什麼時區,所以我不需要時區偏移量,更不用說可能基於我的機器時區的錯誤偏移量。 出於性能原因,我寧願將[Time(UTC)]]列設置爲使用pd.read_csv時的索引,但在執行此操作時我會得到相同的行爲。
如何防止添加時區偏移或設置正確偏移?
我的python版本是2.7.11(Anaconda 2.5.0 64位),pandas版本是0.17.1,numpy 1.10.4。