把熊猫的日期转换成星期数

我想从熊猫数据框中提取一个星期的数字。

日期格式为 datetime64[ ns ]

我已经将日期标准化,从中删除了时间

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

所以数据框中的日期现在看起来像-2015-06-17

现在我要把它转换成一个星期数。

先谢谢你

177957 次浏览

Just access the week attribute of Series.dt.isocalendar():

Example:

In [286]:
df['Date'].dt.isocalendar().week


Out[286]:
0    25
dtype: int64


In [287]:
df['Week_Number'] = df['Date'].dt.isocalendar().week
df


Out[287]:
Date  Week_Number
0 2015-06-17           25

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')

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)
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

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)

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()