For example, a date range between 1Apr2019 to 31Mar2020. Week 1: 1-7apr2019 Week 2: 8-15apr2019 and so on.
Is there a way to populate the week number in this manner?
Thanks
use strftime
to set the DateTime format, and transform the data you wanted.
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
begin_date, end_date = '1Apr2019', '31Mar2020'
df = pd.DataFrame([
pd.date_range(begin_date, end_date).strftime('%G-%V'),
pd.date_range(begin_date, end_date).strftime('%d%b%Y')]).T
group_map = pd.DataFrame(enumerate(df[0].unique())).set_index(1)[0].to_dict()
df['group'] = df[0].map(group_map)
df['week_group'] = (df['group'] + 1).map(lambda x: f'Week {x}')
print(df.head(14))
0 1 group week_group
0 2019-13 01Apr2019 0 Week 1
1 2019-13 02Apr2019 0 Week 1
2 2019-13 03Apr2019 0 Week 1
3 2019-13 04Apr2019 0 Week 1
4 2019-13 05Apr2019 0 Week 1
5 2019-13 06Apr2019 0 Week 1
6 2019-13 07Apr2019 0 Week 1
7 2019-14 08Apr2019 1 Week 2
8 2019-14 09Apr2019 1 Week 2
9 2019-14 10Apr2019 1 Week 2
10 2019-14 11Apr2019 1 Week 2
11 2019-14 12Apr2019 1 Week 2
12 2019-14 13Apr2019 1 Week 2
13 2019-14 14Apr2019 1 Week 2
where:
%G
ISO 8601 year with century representing the year that contains the greater part of the ISO week (%V).
%V
ISO 8601 week as a decimal number with Monday as the first day of the week. Week 01 is the week containing Jan 4.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加