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

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

问题描述

所以我的代码如下:

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

我正在做一个测试,看看我是否可以过滤月份,使其只显示 11 月的日期,但这不起作用.它给了我以下错误:AttributeError: 'Int64Index' object has no attribute '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",这让我相信存储在数据帧中的对象类型是时间戳对象.(我不确定 '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)

我想要做的是:数据框列包含从 2000 年初到以下列格式呈现的日期:dd/mm/yyyy.我只想过滤 11 月 15 日和 3 月 15 日之间的日期,与年份无关.什么是最简单的方法来做到这一点?

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 月份的 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天全站免登陆