2014-09-19 76 views
3

在熊貓中,您可以通過經典的整數位置/基於行的索引或基於日期時間的索引訪問時間序列的特定位置。基於整數的索引可以使用基本的算術運算來處理,例如,如果我有一個integer_index時間序列的頻率爲12小時,並且我想在此之前一天訪問該條目,我可以簡單地執行integer_index - 2。但是,真實世界的數據並不總是完美的,有時候行也會丟失。在這種情況下,此方法失敗,並且能夠使用基於日期時間的索引並從該索引中減去例如one day會很有幫助。我怎樣才能做到這一點?關於熊貓日期時間索引的算術運算

示例腳本:

# generate a sample time series 
import pandas as pd 
s = pd.Series(["A", "B", "C", "D", "E"], index=pd.date_range("2000-01-01", periods=5, freq="12h")) 
print s 

2000-01-01 00:00:00 A 
2000-01-01 12:00:00 B 
2000-01-02 00:00:00 C 
2000-01-02 12:00:00 D 
2000-01-03 00:00:00 E 
Freq: 12H, dtype: object 

# these to indices should access the same value ("C") 
integer_index = 2 
date_index = "2000-01-02 00:00" 

print s[integer_index] # prints "C" 
print s[date_index] # prints "C" 

# I can access the value one day earlier by subtracting 2 from the integer index 
print s[integer_index - 2] # prints A 

# how can I subtract one day from the date index? 
print s[date_index - 1] # raises an error 

的背景,這個問題可以在這裏較早提交我的發現:

Fill data gaps with average of data from adjacent days

其中用戶約內找到一個解決辦法,以我的問題是使用基於整數位置的索引。他通過重新採樣時間序列來確保我具有等間隔的數據。

回答

3

您的日期時間指數不是基於字符串的,這是一個DatetimeIndex這意味着你可以適當地使用datetime對象的索引,而不是一個字符串,它看起來的日期。

以下代碼將date_index轉換爲datetime對象,然後使用timedelta(days=1)從中減去「一天」。

# generate a sample time series 
import pandas as pd 
from datetime import datetime, timedelta 

s = pd.Series(["A", "B", "C", "D", "E"], index=pd.date_range("2000-01-01", periods=5, freq="12h")) 
print(s) 

# these two indices should access the same value ("C") 
integer_index = 2 
# Converts the string into a datetime object 
date_index = datetime.strptime("2000-01-02 00:00", "%Y-%m-%d %H:%M") 
print(date_index) # 2000-01-02 00:00:00 

print(s[integer_index]) # prints "C" 
print(s[date_index]) # prints "C" 


print(s[integer_index - 2]) # prints "A" 

one_day = timedelta(days=1) 
print(s[date_index - one_day]) # prints "A" 
print(date_index - one_day) # 2000-01-01 00:00:00