根据期间

时间:2017-01-05 06:40:14

标签: python pandas

我有一个包含时间序列的DataFrame,例如:

enter image description here

我想创建该DataFrame的多个子集,每个子​​集包含一周的数据,从星期日凌晨0点到星期六凌晨0点。

我可以想办法使用来自timeutil的RRule来做到这一点,但似乎可能有更直观/直接的方法使用Pandas Periods。

但是我很新,所以不知道从哪里开始寻找。理想情况下它会是这样的:

Period= Sun 0am to Sat 0am
Subsets=[]
for Period in DataFrame:
    Subsets.append(DataFrame[Period])

像这样......

数据:

Pd.DataFrame(dict, columns=['timestamp','open','high','low','close','volume'])

dict={'volume': {Timestamp('2005-03-06 19:00:00'): 521.0, Timestamp('2005-03-06 20:00:00'): 234.0, Timestamp('2005-03-06 20:30:00'): 164.0, Timestamp('2005-03-06 21:00:00'): 99.0, Timestamp('2005-03-06 17:30:00'): 1603.0, Timestamp('2005-03-06 21:30:00'): 389.0, Timestamp('2005-03-06 18:00:00'): 590.0, Timestamp('2005-03-06 17:00:00'): 1668.0, Timestamp('2005-03-06 19:30:00'): 79.0, Timestamp('2005-03-06 18:30:00'): 213.0}, 'low': {Timestamp('2005-03-06 19:00:00'): 1226.25, Timestamp('2005-03-06 20:00:00'): 1226.0, Timestamp('2005-03-06 20:30:00'): 1226.0, Timestamp('2005-03-06 21:00:00'): 1226.0, Timestamp('2005-03-06 17:30:00'): 1225.75, Timestamp('2005-03-06 21:30:00'): 1225.5, Timestamp('2005-03-06 18:00:00'): 1226.75, Timestamp('2005-03-06 17:00:00'): 1225.0, Timestamp('2005-03-06 19:30:00'): 1226.25, Timestamp('2005-03-06 18:30:00'): 1226.75}, 'timestamp': {Timestamp('2005-03-06 19:00:00'): 732011.79166666663, Timestamp('2005-03-06 20:00:00'): 732011.83333333337, Timestamp('2005-03-06 20:30:00'): 732011.85416666663, Timestamp('2005-03-06 21:00:00'): 732011.875, Timestamp('2005-03-06 17:30:00'): 732011.72916666663, Timestamp('2005-03-06 21:30:00'): 732011.89583333337, Timestamp('2005-03-06 18:00:00'): 732011.75, Timestamp('2005-03-06 17:00:00'): 732011.70833333337, Timestamp('2005-03-06 19:30:00'): 732011.8125, Timestamp('2005-03-06 18:30:00'): 732011.77083333337}, 'open': {Timestamp('2005-03-06 19:00:00'): 1227.0, Timestamp('2005-03-06 20:00:00'): 1226.25, Timestamp('2005-03-06 20:30:00'): 1226.5, Timestamp('2005-03-06 21:00:00'): 1226.0, Timestamp('2005-03-06 17:30:00'): 1225.75, Timestamp('2005-03-06 21:30:00'): 1225.75, Timestamp('2005-03-06 18:00:00'): 1227.0, Timestamp('2005-03-06 17:00:00'): 1225.75, Timestamp('2005-03-06 19:30:00'): 1226.25, Timestamp('2005-03-06 18:30:00'): 1227.25}, 'high': {Timestamp('2005-03-06 19:00:00'): 1227.0, Timestamp('2005-03-06 20:00:00'): 1226.5, Timestamp('2005-03-06 20:30:00'): 1226.5, Timestamp('2005-03-06 21:00:00'): 1226.25, Timestamp('2005-03-06 17:30:00'): 1227.5, Timestamp('2005-03-06 21:30:00'): 1226.0, Timestamp('2005-03-06 18:00:00'): 1227.5, Timestamp('2005-03-06 17:00:00'): 1226.25, Timestamp('2005-03-06 19:30:00'): 1226.75, Timestamp('2005-03-06 18:30:00'): 1227.5}, 'close': {Timestamp('2005-03-06 19:00:00'): 1226.25, Timestamp('2005-03-06 20:00:00'): 1226.25, Timestamp('2005-03-06 20:30:00'): 1226.0, Timestamp('2005-03-06 21:00:00'): 1226.0, Timestamp('2005-03-06 17:30:00'): 1227.0, Timestamp('2005-03-06 21:30:00'): 1225.5, Timestamp('2005-03-06 18:00:00'): 1227.25, Timestamp('2005-03-06 17:00:00'): 1225.5, Timestamp('2005-03-06 19:30:00'): 1226.5, Timestamp('2005-03-06 18:30:00'): 1226.75}}

1 个答案:

答案 0 :(得分:1)

您可以使用:

#sample dataframe
start = pd.to_datetime('2016-12-28')
rng = pd.date_range(start, periods=100, freq='100min')
df = pd.DataFrame({'timestamp': rng, 'X': range(100), 
                   'id': ['a'] * 30 + ['b'] * 30 + ['c'] * 40 })  
df = df.set_index(['timestamp'])
#print (df)

首先根据需要dayofweekboolean indexing过滤掉周末:

#df = df[df.index.dayofweek < 5]
#print (df)

然后将period_range与周频率一起使用:

#first date in index
first_date = df.index[0]
#last date in index
last_date = df.index[-1]
per = pd.period_range(first_date,last_date, freq='W')
print (per)
PeriodIndex(['2016-12-26/2017-01-01', 
             '2017-01-02/2017-01-08'], dtype='period[W-SUN]', freq='W-SUN')

最后按Subsets创建list comprehension并转换每个句点to_timestamp并按loc选择值:

Subsets = [ df.loc[x.to_timestamp('D', how='s'): x.to_timestamp('D', how='e')] for x in per]
#print (Subsets)

如果无法使用loc,则Dataetimeindex使用boolean indexing中不包含终点:

Subsets = [ df[(df.index > x.to_timestamp('D', how='s')) & 
               (df.index < x.to_timestamp('D', how='e'))] for x in per]
#print (Subsets)