本文介绍了 pandas 时间序列多片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从熊猫文档中可以看到您可以去

I can see from the pandas documentation that you can go:

df.loc[['a','b','c'],:]

对于时间序列,为什么不能去:

For time series, why can you not go:

x = df.loc[['2005-10-27 14:30':'2005-10-27 15:15', '2006-04-14 14:40':'2006-04-14 15:20', '2008-01-25 14:30':'2008-01-25 15:30'],:]

我收到语法错误.您不能在一个时间序列上进行多个切分范围吗?有解决方法吗?

I get a syntax error. Can you not do multiple sliced ranges on a time series? Is there a workaround?

推荐答案

虽然DataFrame索引将接受列索引列表,但将不接受行切片对象列表.

While a DataFrame index will accept a list of column indexes, it will not accept a list of row slice objects.

这应该做您想要的,它遍历您想要的范围,编译一个新的DataFrame.

This should do what you want, it loops through your desired ranges compiling a new DataFrame.

import numpy as np
import pandas as pd

# let's create some fake data
date_range = pd.date_range('2005-01-01', '2008-12-31', freq='9min')
l = len(date_range)
df = pd.DataFrame({'normal': np.random.randn(l), 'uniform':np.random.rand(l), 
    'datetime':date_range, 'integer':range(l)}, index=date_range)

# let's identify the periods we want
desired = [('2005-10-27 14:30','2005-10-27 15:15'), 
           ('2006-04-14 14:40','2006-04-14 15:20'), 
           ('2008-01-25 14:30','2008-01-25 15:30')]

# let's loop through the desired ranges and compile our selection           
x = pd.DataFrame()
for (start, stop) in desired:
    selection = df[(df.index >= pd.Timestamp(start)) & 
        (df.index <= pd.Timestamp(stop))]
    x = x.append(selection)

# and let's have a look at what we found ...
print(x)

这篇关于 pandas 时间序列多片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 20:59