Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (50.2k points)

I would like to extract a week number from data in a pandas dataframe.

The date format is datetime64[ns]

I have normalized the date to remove the time from it

df['Date'] = df['Date'].apply(pd.datetools.normalize_date)

so the date now looks like - 2015-06-17 in the data frame column

and now I like to convert that to a week number.

Thanks in advance

1 Answer

0 votes
by (107k points)

You just have to access the dt week attribute:

In [286]:

df['Date'].dt.week

Out[286]:

0    25

dtype: int64

In [287]:

df['Week_Number'] = df['Date'].dt.week

df

Out[287]:

        Date  Week_Number

0 2015-06-17           25

If you are interested in learning Pandas and want to become an expert in Python Programming, then check out this Python Course and upskill yourself.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...