在 python 中添加数据框的假日屋檐
Adding holiday eves on a dateframe in python
我在 Python 中有以下数据框:
日期列采用时间戳格式。
date
holiday_type
name
other
2022-01-01 00:00:00
Holiday
Holiday 1
UK
2022-01-02 00:00:00
Holiday
Holiday 2
UK
2022-03-08 00:00:00
Holiday
Holiday 3
UK
2022-04-12 00:00:00
Holiday
Holiday 4
UK
我想为指定日期前一天的记录添加新行。生成的数据框将如下所示:
date
holiday_type
name
other
2021-12-31 00:00:00
Pre Holiday
(Pre) Holiday 1
UK
2022-01-01 00:00:00
Holiday
Holiday 1
UK
2022-01-02 00:00:00
Holiday
Holiday 2
UK
2022-03-07 00:00:00
Pre Holiday
(Pre) Holiday 3
UK
2022-03-08 00:00:00
Holiday
Holiday 3
UK
2022-04-11 00:00:00
Pre Holiday
(Pre) Holiday 4
UK
2022-04-12 00:00:00
Holiday
Holiday 4
UK
例外情况是,如果前一天已经是节假日,则不添加节前。
希望大家能帮帮我,谢谢
这可能是一种更有效的方法,但我是这样做的。
我创建了一个将你的日期偏移一天的数据框。然后添加后缀'(Pre) '
并将holiday_type
更改为'Pre Holiday'
。然后我将它附加到原始数据框,排序并删除重复日期,保留最后一个条目。
import pandas as pd
cols = ['date','holiday_type','name','other']
data = [['2022-01-01 00:00:00', 'Holiday', 'Holiday 1', 'UK'],
['2022-01-02 00:00:00', 'Holiday', 'Holiday 2', 'UK'],
['2022-03-08 00:00:00', 'Holiday', 'Holiday 3', 'UK'],
['2022-04-12 00:00:00', 'Holiday', 'Holiday 4', 'UK']]
df = pd.DataFrame(data, columns=cols)
df['date'] = pd.to_datetime(df['date'])
df_yesterday = df[df['holiday_type'] == 'Holiday']
df_yesterday['date'] = df_yesterday['date'] + pd.offsets.Day(-1)
df_yesterday['holiday_type'] = 'Pre Holiday'
df_yesterday['name'] = '(Pre) ' + df_yesterday['name']
df = pd.concat([df, df_yesterday]).sort_values(['date', 'holiday_type'], ascending=[True, False]).reset_index(drop=True)
df = df.drop_duplicates(['date'], keep='last').reset_index(drop=True)
输出:
print(df)
date holiday_type name other
0 2021-12-31 Pre Holiday (Pre) Holiday 1 UK
1 2022-01-01 Holiday Holiday 1 UK
2 2022-01-02 Holiday Holiday 2 UK
3 2022-03-07 Pre Holiday (Pre) Holiday 3 UK
4 2022-03-08 Holiday Holiday 3 UK
5 2022-04-11 Pre Holiday (Pre) Holiday 4 UK
6 2022-04-12 Holiday Holiday 4 UK
我在 Python 中有以下数据框: 日期列采用时间戳格式。
date | holiday_type | name | other |
---|---|---|---|
2022-01-01 00:00:00 | Holiday | Holiday 1 | UK |
2022-01-02 00:00:00 | Holiday | Holiday 2 | UK |
2022-03-08 00:00:00 | Holiday | Holiday 3 | UK |
2022-04-12 00:00:00 | Holiday | Holiday 4 | UK |
我想为指定日期前一天的记录添加新行。生成的数据框将如下所示:
date | holiday_type | name | other |
---|---|---|---|
2021-12-31 00:00:00 | Pre Holiday | (Pre) Holiday 1 | UK |
2022-01-01 00:00:00 | Holiday | Holiday 1 | UK |
2022-01-02 00:00:00 | Holiday | Holiday 2 | UK |
2022-03-07 00:00:00 | Pre Holiday | (Pre) Holiday 3 | UK |
2022-03-08 00:00:00 | Holiday | Holiday 3 | UK |
2022-04-11 00:00:00 | Pre Holiday | (Pre) Holiday 4 | UK |
2022-04-12 00:00:00 | Holiday | Holiday 4 | UK |
例外情况是,如果前一天已经是节假日,则不添加节前。
希望大家能帮帮我,谢谢
这可能是一种更有效的方法,但我是这样做的。
我创建了一个将你的日期偏移一天的数据框。然后添加后缀'(Pre) '
并将holiday_type
更改为'Pre Holiday'
。然后我将它附加到原始数据框,排序并删除重复日期,保留最后一个条目。
import pandas as pd
cols = ['date','holiday_type','name','other']
data = [['2022-01-01 00:00:00', 'Holiday', 'Holiday 1', 'UK'],
['2022-01-02 00:00:00', 'Holiday', 'Holiday 2', 'UK'],
['2022-03-08 00:00:00', 'Holiday', 'Holiday 3', 'UK'],
['2022-04-12 00:00:00', 'Holiday', 'Holiday 4', 'UK']]
df = pd.DataFrame(data, columns=cols)
df['date'] = pd.to_datetime(df['date'])
df_yesterday = df[df['holiday_type'] == 'Holiday']
df_yesterday['date'] = df_yesterday['date'] + pd.offsets.Day(-1)
df_yesterday['holiday_type'] = 'Pre Holiday'
df_yesterday['name'] = '(Pre) ' + df_yesterday['name']
df = pd.concat([df, df_yesterday]).sort_values(['date', 'holiday_type'], ascending=[True, False]).reset_index(drop=True)
df = df.drop_duplicates(['date'], keep='last').reset_index(drop=True)
输出:
print(df)
date holiday_type name other
0 2021-12-31 Pre Holiday (Pre) Holiday 1 UK
1 2022-01-01 Holiday Holiday 1 UK
2 2022-01-02 Holiday Holiday 2 UK
3 2022-03-07 Pre Holiday (Pre) Holiday 3 UK
4 2022-03-08 Holiday Holiday 3 UK
5 2022-04-11 Pre Holiday (Pre) Holiday 4 UK
6 2022-04-12 Holiday Holiday 4 UK