-1

I have a date column in my pandas dataframe which I convert to datetime first and then extract the year:

data[date]      = pd.to_datetime(data[date])
data['year'] = pd.PeriodIndex(data[date], freq='A')

I would like to subset this based on the year, something like:

 if data['year'] ==2015

But somehow that does not work. Anyone have an idea why?

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
Niccola Tartaglia
  • 1,537
  • 2
  • 26
  • 40

2 Answers2

2

There are two basic methods to solve this.

If you want to make a new column named year to solve this, then you can take the year from the date column and create year column, on which you can do loc and get the needed result.

It can be done like this.

data['date'] = pd.to_datetime(data['date'])
data['year'] = data['date'].dt.year

data.loc[data.year == 2015, :]

If you don't want to make a seperate column for solving this you can directly access the year from the date feature on loc. It can be done like this.

data['date'] = pd.to_datetime(data['date'])
data.loc[data.date.dt.year == 2015, :]
Sreeram TP
  • 11,346
  • 7
  • 54
  • 108
1

If you want all the rows where year equals 2015 from your dataframe, the right pandas syntax would be:

data[data['year'] == 2015]

OR

data[data.year == 2015]

Note:

Please ensure that the year column is of type int. It may be the case that you forgot to convert the year to int in which case it is most likely going to be a string. In such a case, use:
data[data['year'] == '2015']

OR

data[data.year == 2015]

Alernatively, if you just need the subset where the year is 2015 without needing a column for the year, you can do.

data[pd.DatetimeIndex(data['Date']).year == 2015]
Samuel Nde
  • 2,565
  • 2
  • 23
  • 23
  • So both cases 2015 and '2015' come back empty – Niccola Tartaglia Oct 20 '18 at 05:06
  • I think you did not extract the year from the date correctly. Please look at [this post](https://stackoverflow.com/questions/25146121/extracting-just-month-and-year-from-pandas-datetime-column-python). I would go for @KieranPC 's answer. Ensure that the year was extracted correctly and then the code snippets that I shared above would do the rest. – Samuel Nde Oct 20 '18 at 05:11