如何按特定月份/日期过滤日期数据框? [英] How to filter a dataframe of dates by a particular month/day?

查看:98
本文介绍了如何按特定月份/日期过滤日期数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的代码如下:

df['Dates'][df['Dates'].index.month == 11]

我正在做一个测试,看是否可以过滤月份,所以它只显示11月的日期,但这没有用.它给了我以下错误:AttributeError:'Int64Index'对象没有属性'month'.

I was doing a test to see if I could filter the months so it only shows November dates, but this did not work. It gives me the following error: AttributeError: 'Int64Index' object has no attribute 'month'.

如果我这样做

print type(df['Dates'][0])

然后,我得到类'pandas.tslib.Timestamp',这使我相信存储在数据框中的对象类型是Timestamp对象. (我不确定'Int64Index'的来源是...之前的错误)

then I get class 'pandas.tslib.Timestamp', which leads me to believe that the types of objects stored in the dataframe are Timestamp objects. (I'm not sure where the 'Int64Index' is coming from... for the error before)

我想做的是:dataframe列包含从2000年代开始到现在的日期,格式如下:dd/mm/yyyy.我只想过滤11月15日到3月15日之间的日期,与YEAR无关.最简单的方法是什么?

What I want to do is this: The dataframe column contains dates from the early 2000's to present in the following format: dd/mm/yyyy. I want to filter for dates only between November 15 and March 15, independent of the YEAR. What is the easiest way to do this?

谢谢.

这是df ['Dates'](带有索引):

Here is df['Dates'] (with indices):

0    2006-01-01
1    2006-01-02
2    2006-01-03
3    2006-01-04
4    2006-01-05
5    2006-01-06
6    2006-01-07
7    2006-01-08
8    2006-01-09
9    2006-01-10
10   2006-01-11
11   2006-01-12
12   2006-01-13
13   2006-01-14
14   2006-01-15
...

推荐答案

映射一个匿名函数以计算该系列的月份,并将其与11月份进行比较. 那会给你一个布尔面罩.然后,您可以使用该掩码过滤数据框.

Map an anonymous function to calculate the month on to the series and compare it to 11 for nov. That will give you a boolean mask. You can then use that mask to filter your dataframe.

nov_mask = df['Dates'].map(lambda x: x.month) == 11
df[nov_mask]

我认为没有直接的方法可以过滤掉您想忽略年份的方式,因此请尝试

I don't think there is straight forward way to filter the way you want ignoring the year so try this.

nov_mar_series = pd.Series(pd.date_range("2013-11-15", "2014-03-15"))
#create timestamp without year
nov_mar_no_year = nov_mar_series.map(lambda x: x.strftime("%m-%d"))
#add a yearless timestamp to the dataframe
df["no_year"] = df['Date'].map(lambda x: x.strftime("%m-%d"))
no_year_mask = df['no_year'].isin(nov_mar_no_year)
df[no_year_mask]

这篇关于如何按特定月份/日期过滤日期数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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