4

Pandas DataFrame に毎日のデータがあり、適切なインデックスがあります。このようなもの:

import pandas as pd
import numpy as np

rng = pd.date_range('1/1/2010', periods=1000, freq='D')
ts = pd.DataFrame(randn(len(rng)), index=rng, columns=['vals'])
print ts.head()

                vals
2010-01-01  1.098302
2010-01-02 -1.384821
2010-01-03 -0.426329
2010-01-04 -0.587967
2010-01-05 -0.853374

すべての年の 2 月 2 日から 3 月 3 日の間にあるレコードのみに DataFrame をサブセット化したいと思います。

これを行うには非常にパンダ風の方法があるはずですが、私はそれを見つけるのに苦労しています。何か助けはありますか?

4

2 に答える 2

6

これを行うためのネイティブな方法はないと思います(時間の間にあります)。

しかし、あなたは単純にそれを行うことができます (これは効率的ですが、書くのは面倒です!):

In [11]: ts[((ts.index.month == 2) & (2 <= ts.index.day)  # in Feb after the 2nd inclusive
              | (ts.index.month == 3) & (ts.index.day <= 3))]  # in March before the 3rd inclusive
Out[11]: 
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 94 entries, 2010-02-01 00:00:00 to 2012-03-03 00:00:00
Data columns (total 1 columns):
vals    94  non-null values
dtypes: float64(1)
于 2013-09-11T17:19:59.937 に答える