如何在 Python 中高效地将日期范围分配给优先项目?

问题描述 投票:0回答:1

我需要计算一些以时间序列形式存储在 InfluxDB 中的预测的误差。每个预测都有一个与 InfluxDB 中的 guid 关联的标签。但是,对于某些日期范围,您有多个预测,因为用户可以运行多个预测。所以,我遇到了问题:我必须说出使用哪些预测来计算误差。对于它们,我有一些参数可以让我对它们进行排名。用户“正式化”的所有预测将首先优先,然后是最近的预测。 预测信息也存储在我的关系数据库中,因此我决定将那些具有我感兴趣的日期数据的数据(从计算错误的最后日期到我拥有真实数据的日期),并按官方、创建顺序对它们进行排序日期。

因此,在我的服务中,字典列表按优先级排序,如下所示:

executions_forecast = [
            {
                "guid": "foo123",
                "init_date": "2024-20-02T00:00:00Z",
                "final_date": "2024-24-02T00:00:00Z",
                "is_officialized": True,
                "created_at": "2024-02-16T00:00:00Z",
            },
            {
                "guid": "foo456",
                "init_date": "2024-02-18T00:00:00Z",
                "final_date": "2024-02-22T00:00:00Z",
                "is_officialized": True,
                "created_at": "2024-02-14T00:00:00Z",
            },
            {
                "guid": "foo789",
                "init_date": "2024-02-16T00:00:00Z",
                "final_date": "2024-02-21T00:00:00Z",
                "is_officialized": False,
                "created_at": "2024-02-15T00:00:00Z",
            }
            # ...
        ]

(您应该优先使用 foo 123,然后是 foo 456,然后是 foo 789) 我还有可以计算错误的日期范围:next_datetime_error、last_datetime_real 最后,我需要的是让每次执行都具有相应的日期范围来查询 InfluxDB,例如:

{"foo123": ("2024-20-02T00:00:00Z", "2024-24-02T12:00:00Z"), "foo456": ("2024-18-02T00:00:00Z", "2024-22-02T12:00:00Z"), "foo789": ("2024-16-02T00:00:00Z", "2024-21-02T12:00:00Z")} 

我有点不确定实现这一目标的最佳方法是什么。我在微服务中也有 pandas,所以我可以使用它,尽管我不知道仅使用循环是否更好。我赞成executions_forecast按优先级排序,我正在考虑创建一个包含所有日期的df并遍历executions_forecast并填充它,但我不确定如何有效地处理它。或者也许我应该以相反的优先顺序浏览它并覆盖。我不确定最有效的方法是什么。 这是我的代码:

executions_ranges = {}
        range_datetimes = date_range(start=next_datetime_error, end=last_datetime_real, freq="H")
        df = DataFrame({'fecha': range_datetimes, 'bool': False})
        upper_datetime = last_datetime_real
        for execution in executions_forecast:
            init_date = datetime.strptime(execution.get("init_date"), "%Y-%m-%dT%H:%M:%S%z")
            final_date = datetime.strptime(execution.get("final_date"), "%Y-%m-%dT%H:%M:%S%z")
            init_date, final_date = init_date.replace(tzinfo=None), final_date.replace(tzinfo=None)
            # Here I must check that the execution has data on dates less than upper_datetime
            # and fill the corresponding data in range_datetimes
            upper_datetime = init_date
            df.loc[(df['fecha'] >= init_date) & (df['fecha'] <= final_date), 'bool'] = True
            executions_ranges[execution.get("guid")] = (init_date, final_date)
python pandas algorithm influxdb
1个回答
0
投票

事实上也许这个解决方案如下

首先我将此字典转换为数据框并定义时间列列表

df = pd.DataFrame(executions_forecast)
time_cols = ["init_date","final_date","created_at"]

然后我使用映射将时间字符串转换为_datetime

df[time_cols] = df[time_cols].map(lambda x: pd.to_datetime(x, format="%Y-%m-%dT%H:%M:%S%z").tz_convert(None))

首先使用 is_officialized 然后使用created_time

对值进行排序
df.sort_values(["is_officialized","created_at"], ascending=False, inplace=True)

终于从dataframe中得到了需要的键值

rdf = df.astype(str)[["guid","init_date","final_date"]]
result = { k:(v1,v2) for k,v1,v2 in rdf.values }
result
© www.soinside.com 2019 - 2024. All rights reserved.