在特定时间间隔内将小时添加到行的日期时间
Add hours to datetime of rows in a specific time interval
让它成为下面的Python Panda DataFrame:
| country_ID | date | counter | value |
| -------- | ------------------------- | -------- | ------ |
| USA | 2022-03-01 09:22:29+00:00 | 1 | red |
| UK | 2022-03-01 11:21:20+00:00 | 1 | blue |
| USA | 2022-04-02 12:15:23+00:00 | 1 | red |
| ITL | 2022-04-03 11:13:31+00:00 | 1 | red |
| USA | 2022-05-05 21:04:42+00:00 | 1 | green |
| USA | 2022-05-05 22:01:51+00:00 | 1 | green |
| ITL | 2022-06-06 13:00:41+00:00 | 1 | red |
给定一个日期和时间范围(开始和结束)和一个 country_ID
,我想向该范围内的行添加 2 小时:
示例:
add_hours('USA', '2022-03-01 09:00:00', '2022-05-05 21:30:00', 2)
| country_ID | date | counter | value |
| -------- | ------------------------- | -------- | ------ |
| USA | 2022-03-01 11:22:29+00:00 | 1 | red |
| UK | 2022-03-01 11:21:20+00:00 | 1 | blue |
| USA | 2022-04-02 14:15:23+00:00 | 1 | red |
| ITL | 2022-04-03 11:13:31+00:00 | 1 | red |
| USA | 2022-05-05 23:04:42+00:00 | 1 | green |
| USA | 2022-05-05 22:01:51+00:00 | 1 | green |
| ITL | 2022-06-06 13:00:41+00:00 | 1 | red |
使用布尔索引尝试您的逻辑(date
也必须是日期时间对象,而不是字符串):
def add_hours(df, country, start, end):
is_country = df['country_ID'].eq(country)
valid_date = df['date'].between(start, end)
df.loc[is_country & valid_date, 'date'] += pd.Timedelta('2H')
让它成为下面的Python Panda DataFrame:
| country_ID | date | counter | value |
| -------- | ------------------------- | -------- | ------ |
| USA | 2022-03-01 09:22:29+00:00 | 1 | red |
| UK | 2022-03-01 11:21:20+00:00 | 1 | blue |
| USA | 2022-04-02 12:15:23+00:00 | 1 | red |
| ITL | 2022-04-03 11:13:31+00:00 | 1 | red |
| USA | 2022-05-05 21:04:42+00:00 | 1 | green |
| USA | 2022-05-05 22:01:51+00:00 | 1 | green |
| ITL | 2022-06-06 13:00:41+00:00 | 1 | red |
给定一个日期和时间范围(开始和结束)和一个 country_ID
,我想向该范围内的行添加 2 小时:
示例:
add_hours('USA', '2022-03-01 09:00:00', '2022-05-05 21:30:00', 2)
| country_ID | date | counter | value |
| -------- | ------------------------- | -------- | ------ |
| USA | 2022-03-01 11:22:29+00:00 | 1 | red |
| UK | 2022-03-01 11:21:20+00:00 | 1 | blue |
| USA | 2022-04-02 14:15:23+00:00 | 1 | red |
| ITL | 2022-04-03 11:13:31+00:00 | 1 | red |
| USA | 2022-05-05 23:04:42+00:00 | 1 | green |
| USA | 2022-05-05 22:01:51+00:00 | 1 | green |
| ITL | 2022-06-06 13:00:41+00:00 | 1 | red |
使用布尔索引尝试您的逻辑(date
也必须是日期时间对象,而不是字符串):
def add_hours(df, country, start, end):
is_country = df['country_ID'].eq(country)
valid_date = df['date'].between(start, end)
df.loc[is_country & valid_date, 'date'] += pd.Timedelta('2H')