Say I have the following DataFrame:
df = pd.DataFrame("x":[pd.Timestamp("2016-03-18")])
How can I access the properties of the datetime object stored in column x? That is, I want e.g.
df.x[0].weekofyear # returns 9
But for the whole column. Clearly, df.x.weekofyear wouldn't work, as df.x is a series and not a datetime object, and df.x.apply(pd.Timestamp.weekofyear) won't work either, as weekofyear is an attribute and not a function.
The solution I could think of was defining a function to access the attribute and then apply this, as:
def get_week(x):
return x.weekofyear
df.x.apply(get_week) # returns series of weeks
It seems that this is a bit roundabout and verbose, and my experience is that whenever I do something like this, pandas already has a super efficient built-in way to do it - is there one for this case?