Reputation: 13800
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?
Upvotes: 3
Views: 2330
Reputation: 394159
use vectorised .dt.weekofyear
So df['x'].dt.weekofyear
will return for whole column:
In [119]:
df = pd.DataFrame({'dates': pd.date_range(dt.datetime(2016,1,1), dt.datetime(2016,1,20))})
df
Out[119]:
dates
0 2016-01-01
1 2016-01-02
2 2016-01-03
3 2016-01-04
4 2016-01-05
5 2016-01-06
6 2016-01-07
7 2016-01-08
8 2016-01-09
9 2016-01-10
10 2016-01-11
11 2016-01-12
12 2016-01-13
13 2016-01-14
14 2016-01-15
15 2016-01-16
16 2016-01-17
17 2016-01-18
18 2016-01-19
19 2016-01-20
In [120]:
df['dates'].dt.weekofyear
Out[120]:
0 53
1 53
2 53
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 2
11 2
12 2
13 2
14 2
15 2
16 2
17 3
18 3
19 3
Name: dates, dtype: int64
Upvotes: 3