converting a pandas date to week number
Python 3.xPandasDatetimePython 3.x Problem Overview
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
Python 3.x Solutions
Solution 1 - Python 3.x
Just 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
Solution 2 - Python 3.x
Here is another possibility using strftime
. strftime.org
is a good resource.
df['Week_Number'] = df['Date'].dt.strftime('%U')
'%U'
represents the week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.
If you have dates from multiple years, I recommend creating a Year-Week combination
df['Year-Week'] = df['Date'].dt.strftime('%Y-%U')
Solution 3 - Python 3.x
Pandas has its .dayofyear
and .weekofyear
functionality, which can be applied straight away to the output of pandas.to_datetime(df['column_name'])
, giving type "Timestamp" as the output.
import pandas as pd
df['formatted_date'] = pd.to_datetime(df['datetime'])
df['day_of_year'] = df.formatted_date.apply(lambda x: x.dayofyear)
df['week_of_year'] = df.formatted_date.apply(lambda x: x.weekofyear)
Solution 4 - Python 3.x
from datetime import date
df_date = pd.DataFrame([date.today()],columns = ['today'])
print(df_date)
#### Print Output ####
# today
#0 2019-09-07
df_date['weeknum'] = df_date.today.apply(lambda x:x.isocalendar()[1])
print(df_date)
#### Print Output ####
# today weeknum
#0 2019-09-07 36
Solution 5 - Python 3.x
Update to this answer
In my current python version (3.7, May 2021). The syntax df['Date'].dt.week
is printing the following warning: FutureWarning: weekofyear and week have been deprecated, please use DatetimeIndex.isocalendar().week instead
The way to use DatetimeIndex would be: df['week_number'] = pd.DatetimeIndex(df.index).isocalendar().week
Here a small demonstration of its use to return a Series
# Input
time_idx = pd.date_range('2022-01-01', periods=4, freq='H').tz_localize('UTC')
values = [9 , 8, 7, 6]
df1 = pd.DataFrame(data = values, index=time_idx, columns=['vals'])
# FutureWarning: weekofyear and week have been deprecated
df1['week_number'] = df1.index.week
# Using DatetimeIndex.isocalendar().week instead
df2 = pd.DataFrame(data = values, index=time_idx, columns=['vals'])
# Does not throws a warning
df2['week_number'] = pd.DatetimeIndex(df2.index).isocalendar().week
print(df2)
Solution 6 - Python 3.x
In case of pandas:
import random
import pandas as pd
desired_length = 100
desired_frequency="20D" # XXXM: XXX months, "XXXD":XXX days, XXXMin: XXX minutes etc.
index = pd.date_range('2020-01-01', periods=desired_length, freq=desired_frequency)
data = [random.random() for _ in range(len(index))]
df = pd.DataFrame(data=data, index=index, columns=['DATA'])
df[df.index.isocalendar().keys()] = df.index.isocalendar()