35

So my code is as follows:

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

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'.

If I do

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

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)

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?

Thanks.

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
...
2
  • Please show an example of what df looks like. You probably want to df.set_index('Dates'), but it's hard to say without seeing. Commented Sep 16, 2014 at 16:28
  • Edit your question with df so that it's formatted correctly. Post the head of the entire dataframe if you can. Commented Sep 16, 2014 at 16:46

3 Answers 3

70

Using pd.to_datetime & dt accessor

The accepted answer is not the "pandas" way to approach this problem. To select only rows with month 11, use the dt accessor:

# df['Date'] = pd.to_datetime(df['Date']) -- if column is not datetime yet
df = df[df['Date'].dt.month == 11]

Same works for days or years, where you can substitute dt.month with dt.day or dt.year

Besides that, there are many more, here are a few:

  • dt.quarter
  • dt.week
  • dt.weekday
  • dt.day_name
  • dt.is_month_end
  • dt.is_month_start
  • dt.is_year_end
  • dt.is_year_start

For a complete list see the documentation

1
  • 1
    This approach wouldnt work for old dates, e.g. OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1031-11-29 00:00:00. For such data you have to use some magic
    – VMAtm
    Commented Feb 14, 2022 at 17:37
28

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]
1
  • The int64 was actually because accessed the index first with df['Dates'].index.month. In this case the index was an Int64Index which doesn't have a month attribute. Commented Sep 16, 2014 at 16:59
4

In your code there are two issues. First, need to bring column reference after the filtering condition. Second, can either use ".month" with a column or index, but not both. One of the following should work:

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.