Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas apply to attribute instead of function

Tags:

Is it possible to use df.apply to get an attribute as opposed to running a function? I want to retrieve the year from a date to perform a groupby. For example ..

import pandas as pd
import datetime
import numpy as np

df = pd.DataFrame({'date': [datetime.datetime(2010,1,1)+datetime.timedelta(days=i*15) 
for i in range(0,100)]})

This works ..

df['year'] = [d.year for d in df['date']]

This also works ..

df['year'] = df['date'].apply(lambda x: x.year)

But this does not ..

df['year'] = df['date'].apply(year)

Nor does this ..

df['year'] = df['date'].year

What is the most efficient way of doing this?

like image 598
ashishsingal Avatar asked Jan 19 '17 14:01

ashishsingal


1 Answers

You will be happy to know there is an entire set of functionality built to provide an abundance of date attributes. You can use the dt accessor to get many datetime attributes. It can only be used on pandas series or indexes that are date types.

You can see all the available attributes with dir(df['date'].dt). Here they are printed out below. In your case simply use df['date'].dt.year

'ceil',
 'date',
 'day',
 'dayofweek',
 'dayofyear',
 'days_in_month',
 'daysinmonth',
 'floor',
 'freq',
 'hour',
 'is_leap_year',
 'is_month_end',
 'is_month_start',
 'is_quarter_end',
 'is_quarter_start',
 'is_year_end',
 'is_year_start',
 'microsecond',
 'minute',
 'month',
 'nanosecond',
 'normalize',
 'quarter',
 'round',
 'second',
 'strftime',
 'time',
 'to_period',
 'to_pydatetime',
 'tz',
 'tz_convert',
 'tz_localize',
 'week',
 'weekday',
 'weekday_name',
 'weekofyear',
 'year'
like image 84
Ted Petrou Avatar answered Sep 23 '22 10:09

Ted Petrou