3

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?

1 Answer 1

3

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

2 Comments

Good shout - still haven't internalized this dt notation. Quick followup that I don't want to ask a new question for: why does unique change the type of my dates? For some reason, df.dates.unique() returns np.datetime64, rather than the original datetime objects?
Because that is the underlying numpy dtype representation that is used

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.