brodoll
brodoll

Reputation: 1881

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 monthattribute 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?

Upvotes: 1

Views: 290

Answers (1)

jezrael
jezrael

Reputation: 863166

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

Upvotes: 4

Related Questions