brodoll brodoll - 1 month ago 9
Python Question

Access atribute of every object in pandas dataframe column

Consider the following mwe:

import pandas as pd
from decimal import *
from datetime import date

d1={'Date':date(2016,10,24),'Value':Decimal(20)}
d2={'Date':date(2016,10,25),'Value':Decimal(10)}
d3={'Date':date(2016,9,25),'Value':Decimal(50)}
d4={'Date':date(2016,9,24),'Value':Decimal(5)}

df=pd.DataFrame([d1,d2,d3,d4])


I'm able to access the
month
attribute of a single date the following way:

df.Date[0].month
Out[22]: 10


However
df.Date.month
does not return a vector containing all the months as I would expect. Instead it throws me an error:


AttributeError: 'Series' object has no attribute 'month'


Is there a nice way to accomplish this without having to iterate over the dataframe?

Answer

You need first convert to_datetime and then use dt.month:

print (pd.to_datetime(df.Date).dt.month)
0    10
1    10
2     9
3     9
Name: Date, dtype: int64

Another slowier solution with apply:

print (df.Date.apply(lambda x: x.month))
0    10
1    10
2     9
3     9
Name: Date, dtype: int64

Timings:

#[40000 rows x 2 columns]
df = pd.concat([df]*10000).reset_index(drop=True)

In [292]: %timeit (df.Date.apply(lambda x: x.month))
100 loops, best of 3: 15.8 ms per loop

In [293]: %timeit (pd.to_datetime(df.Date).dt.month)
100 loops, best of 3: 5.44 ms per loop