1

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?

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that was spot on! I was not aware of the difference between type(df.Date[0]) Out[39]: datetime.date and type(pd.to_datetime(df.Date)[0]) Out[41]: pandas.tslib.Timestamp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.