pandas.Series.dt.day_name
や pandas.Series.dt.dayofweek
を使います。import pandas as pd
start_date = '2022-05-01'
end_date = '2022-05-31'
start_date = pd.to_datetime(start_date)
end_date = pd.to_datetime(end_date)
df = pd.DataFrame(pd.date_range(start_date, end_date), columns=['date'])
df['day_name'] = df['date'].dt.day_name()
df['dayofweek'] = df['date'].dt.dayofweek
df
date | day_name | weekday | |
---|---|---|---|
0 | 2022-05-01 | Sunday | 6 |
1 | 2022-05-02 | Monday | 0 |
2 | 2022-05-03 | Tuesday | 1 |
3 | 2022-05-04 | Wednesday | 2 |
4 | 2022-05-05 | Thursday | 3 |
5 | 2022-05-06 | Friday | 4 |
6 | 2022-05-07 | Saturday | 5 |
7 | 2022-05-08 | Sunday | 6 |
8 | 2022-05-09 | Monday | 0 |
9 | 2022-05-10 | Tuesday | 1 |
10 | 2022-05-11 | Wednesday | 2 |
11 | 2022-05-12 | Thursday | 3 |
12 | 2022-05-13 | Friday | 4 |
13 | 2022-05-14 | Saturday | 5 |
14 | 2022-05-15 | Sunday | 6 |
15 | 2022-05-16 | Monday | 0 |
16 | 2022-05-17 | Tuesday | 1 |
17 | 2022-05-18 | Wednesday | 2 |
18 | 2022-05-19 | Thursday | 3 |
19 | 2022-05-20 | Friday | 4 |
20 | 2022-05-21 | Saturday | 5 |
21 | 2022-05-22 | Sunday | 6 |
22 | 2022-05-23 | Monday | 0 |
23 | 2022-05-24 | Tuesday | 1 |
24 | 2022-05-25 | Wednesday | 2 |
25 | 2022-05-26 | Thursday | 3 |
26 | 2022-05-27 | Friday | 4 |
27 | 2022-05-28 | Saturday | 5 |
28 | 2022-05-29 | Sunday | 6 |
29 | 2022-05-30 | Monday | 0 |
30 | 2022-05-31 | Tuesday | 1 |
strftime()
を使う場合。import pandas as pd
date = '2022-05-28'
date = pd.to_datetime(date)
date.strftime('%A')
type(date)
%a
を使います。date.strftime('%a')
date.weekday()