根据月份和月份选择 Pandas DataFrame 记录多年日范围 [英] Selecting Pandas DataFrame records for many years based on month & day range

查看:30
本文介绍了根据月份和月份选择 Pandas DataFrame 记录多年日范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Pandas DataFrame 中有一些日常数据,它有一个很好的索引.像这样:

I've got some daily data in a Pandas DataFrame and it has a nice index. Something like this:

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

我想将我的 DataFrame 子集到仅属于 2 月 2 日和 2 月 2 日之间的记录.3 月 3 日所有年份.

I'd like to subset my DataFrame to only the records that fall between February 2 & March 3 for all years.

似乎应该有一种非常熊猫式的方式来做到这一点,但我很难找到它.有什么帮助吗?

It seems there should be a very Pandas-esque way of doing this but I'm struggling to find it. Any help?

推荐答案

我认为没有本地方法可以做到这一点 (两次之间有 ).

I don't think there is a native way to do this (there is with between times).

但是你可以天真地做(这会很有效率,但写起来很痛苦!):

But you can do it naively (this will be efficient, but is a pain to write!):

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)

这篇关于根据月份和月份选择 Pandas DataFrame 记录多年日范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆