2016-07-25 48 views
1

我有一個稱爲'分數'的日期時間索引系列。如何在季度和年份使用日期時間索引過濾熊貓系列

我希望通過quarteryear
僞代碼子集是:​​

嘗試至今:
s.dt.quarter

AttributeError: Can only use .dt accessor with datetimelike values

s.index.dt.quarter

AttributeError: 'DatetimeIndex' object has no attribute 'dt'

此作品(由this answer啓發),但我不能相信這是在熊貓這樣做的正確方法:

d = pd.DataFrame(s)
d['date'] = pd.to_datetime(d.index)
d.loc[(d['date'].dt.quarter == 2) & (d['date'].dt.year == 2013)]['scores']

我希望有一種方法可以做到這一點沒有轉化成數據集,迫使指數到日期時間,然後從中獲取一個系列。

我錯過了什麼,在熊貓系列上做這件事的優雅方式是什麼?

+1

如果索引日期時間's.index.quarter'這會工作。 – shivsn

+0

你想在特定的年份和季度獲得一個函數嗎? – piRSquared

+0

IIUC你需要'scores.ix [scores.index.quarter == 2]'。 – shivsn

回答

1
import numpy as np 
import pandas as pd 

index = pd.date_range('2013-01-01', freq='M', periods=12) 
s = pd.Series(np.random.rand(12), index=index) 
print(s) 

# 2013-01-31 0.820672 
# 2013-02-28 0.994890 
# 2013-03-31 0.928376 
# 2013-04-30 0.848532 
# 2013-05-31 0.122263 
# 2013-06-30 0.305741 
# 2013-07-31 0.088432 
# 2013-08-31 0.647288 
# 2013-09-30 0.640308 
# 2013-10-31 0.737139 
# 2013-11-30 0.233656 
# 2013-12-31 0.245214 
# Freq: M, dtype: float64 

d = pd.Series(s.index, index=s.index) 
quarter = d.dt.quarter.astype(str) + 'Q' + d.dt.year.astype(str) 
print(quarter) 

# 2013-01-31 1Q2013 
# 2013-02-28 1Q2013 
# 2013-03-31 1Q2013 
# 2013-04-30 2Q2013 
# 2013-05-31 2Q2013 
# 2013-06-30 2Q2013 
# 2013-07-31 3Q2013 
# 2013-08-31 3Q2013 
# 2013-09-30 3Q2013 
# 2013-10-31 4Q2013 
# 2013-11-30 4Q2013 
# 2013-12-31 4Q2013 
# Freq: M, dtype: object 

print(s[quarter == '1Q2013']) 

# 2013-01-31 0.124398 
# 2013-02-28 0.052828 
# 2013-03-31 0.126374 
# Freq: M, dtype: float64 

如果你不想創建包含一個標籤每個季度的一個新的系列(例如,如果您進行子集只有一次),你可以連做

print(s[(s.index.quarter == 1) & (s.index.year == 2013)]) 

# 2013-01-31 0.124398 
# 2013-02-28 0.052828 
# 2013-03-31 0.126374 
# Freq: M, dtype: float64 
0

如果你知道的年份和季度,稱2013年第二季度,那麼你可以這樣做:

s['2013-04':'2013-06'] 

把它包起來成一個函數:

qmap = pd.DataFrame([ 
     ('01', '03'), ('04', '06'), ('07', '09'), ('10', '12') 
    ], list('1234'), list('se')).T 

def get_quarter(df, year, quarter): 
    s, e = qmap[str(quarter)] 
    y = str(year) 
    s = y + '-' + s 
    e = y + '-' + e 
    return df[s:e] 

,並稱之爲:

get_quarter(s, 2013, 2) 

假設s是:

s = pd.Series(range(32), pd.date_range('2011-01-01', periods=32, freq='Q')) 

然後我得到:

2013-03-31 8 
Freq: Q-DEC, dtype: int64 
1

假設你有一個這樣的數據幀:

sa 
Out[28]: 
      0 
1970-01-31 1 
1970-02-28 2 
1970-03-31 3 
1970-04-30 4 
1970-05-31 5 
1970-06-30 6 
1970-07-31 7 
1970-08-31 8 
1970-09-30 9 
1970-10-31 10 
1970-11-30 11 
1970-12-31 12 

如果該指數的日期時間,那麼你可以得到季度sa.index.quarter

sa.index.quarter 
Out[30]: array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]) 
相關問題