python - 如何查看日期在哪个日期范围内并相应地分配值?

标签 python python-3.x pandas dataframe

我有 2 个 dfs:

阈值

    id  meter_point_id  valid_from  valid_until     max_power_contractual
0   3   61  1969-12-31 23:00:00 2019-11-06 23:00:00 0
1   79  61  2019-11-07 00:00:00 2020-07-13 00:00:00 10

数据

    id  ds  time_series_id  y
0   12858   2019-03-21 14:30:00 12858   49.25
1   12858   2019-03-21 14:15:00 12858   52.5
2   12858   2019-03-21 17:15:00 12858   49.25
3   12858   2019-03-21 13:45:00 12858   49.0
4   12858   2019-03-21 13:30:00 12858   51.75
5   22231   2019-11-11 12:00:00 22231   10.5
6   22231   2019-11-11 11:45:00 22231   10.0
7   22231   2019-11-12 09:45:00 22231   10.0
8   22231   2019-11-14 21:45:00 22231   9.5

我想根据 valid_fromvalid_until 日期检查 data df 中 ds 列中的每个日期在 threshold 列中。如果 ds 中的日期位于日期范围 1969-12-31 23:00:00-2019-11-06 23:00:00,我想从 threshold df 中获取 max_power_contractual 值,并将其创建为 data df 中的新列。

预期输出

    id  ds  time_series_id  y max_power_contractual
0   12858   2019-03-21 14:30:00 12858   49.25 0
1   12858   2019-03-21 14:15:00 12858   52.5  0
2   12858   2019-03-21 17:15:00 12858   49.25 0
3   12858   2019-03-21 13:45:00 12858   49.0  0
4   12858   2019-03-21 13:30:00 12858   51.75 0
5   22231   2019-11-11 12:00:00 22231   10.5  10
6   22231   2019-11-11 11:45:00 22231   10.0  10
7   22231   2019-11-12 09:45:00 22231   10.0  10
8   22231   2019-11-14 21:45:00 22231   9.5   10

data df的前5行,ds值与threshold df第一行的日期范围匹配,因此,由于该日期范围的 max_power_contractual 为 0,因此我希望 data df 中有该值。类似地,data df 的最后第 4 行,ds 值与 threshold df 第二行中的日期范围匹配,所以由于该日期范围的 max_power_contractual 是 10,我希望 data df 中有该值。

如何做到这一点?

谢谢

最佳答案

首先使用 pd.to_datetime 将两个数据框中的日期类列转换为 pandas datetime 系列:

threshold['valid_from']  = pd.to_datetime(threshold['valid_from'])
threshold['valid_until'] = pd.to_datetime(threshold['valid_until'])
data['ds'] = pd.to_datetime(data['ds'])

然后使用:

idx = pd.IntervalIndex.from_arrays(threshold['valid_from'], threshold['valid_until'])
mapping = threshold.set_index(idx)['max_power_contractual']
data['max_power_contractual'] = data['ds'].map(mapping)

详细信息:

步骤 A:创建 pd.IntervalIndex来自 valid_fromvalid_until 列,此 IntervalIndex 将在 STEP C 中用于映射列 ds:

# print(idx)
IntervalIndex([(1969-12-31 23:00:00, 2019-11-06 23:00:00], (2019-11-07, 2020-07-13]],
              closed='right',
              dtype='interval[datetime64[ns]]')

步骤 B:使用 .set_indexmax_power_contractual 系列的索引设置为此区间索引 idx:

# print(mapping)
(1969-12-31 23:00:00, 2019-11-06 23:00:00]     0
(2019-11-07, 2020-07-13]                      10
Name: max_power_contractual, dtype: int64

步骤 C:最后使用 Series.mapds 列映射到此 mapping 系列,并将其分配给 data 中的新列。

# print(data)

      id                  ds  time_series_id      y  max_power_contractual
0  12858 2019-03-21 14:30:00           12858  49.25                      0
1  12858 2019-03-21 14:15:00           12858  52.50                      0
2  12858 2019-03-21 17:15:00           12858  49.25                      0
3  12858 2019-03-21 13:45:00           12858  49.00                      0
4  12858 2019-03-21 13:30:00           12858  51.75                      0
5  22231 2019-11-11 12:00:00           22231  10.50                     10
6  22231 2019-11-11 11:45:00           22231  10.00                     10
7  22231 2019-11-12 09:45:00           22231  10.00                     10
8  22231 2019-11-14 21:45:00           22231   9.50                     10

关于python - 如何查看日期在哪个日期范围内并相应地分配值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62874981/

相关文章:

python - Python中子数据帧中数据帧拆分的优化运行时间

python - 基本的Python查询

python - 简化pan​​das表达式

python - Windows 任务调度程序和 python 日志记录模块

python - 处理 urllib2 的超时? - Python

Python3 Flask 在服务器内存中上传文件

python - PyQt5鼠标悬停功能

python - Anaconda 已安装但无法启动 Navigator

python 非常缓慢地释放输出到 tee

python - 遍历 numpy.ma 数组,忽略屏蔽值