Python Forum

Full Version: Manipulating Series in Dataframe (Pandas)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have a date column in a data frame that looks like this:

(Year-Month-Day)
2017-09-21
2018-11-25

I am trying to create a function that considers only the year, I have been trying the following.

df[df['DateColumn'].str[:4]=='2017']

But I am receiving this error:
AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

Any chance to point what I am missing here? Should I change the column to strings, or is there a way to do something similar with the series?

Thank you for your help,
Wendy
Pandas is very flexible, so you access year component of a date-column easily : df.loc[:, 'DateColumn'].dt.year

Also, you can select desired rows of the dataframe by year:

df.loc[df.loc[:, 'DateColumn'].dt.year == 2017, :]
This worked:

df.loc[df.loc[:, 'DateColumn'].dt.year == 2017, :]

Thank you!