gpt4 book ai didi

python - 处理闰日,无论何时存在

转载 作者:行者123 更新时间:2023-12-01 15:11:57 25 4
gpt4 key购买 nike

我有一个通过 CSV 提供的数据集。每当该 csv 在闰年提供时,它包含 2 月 29 日,而在非闰年提供时则不包含。我只得到一年的数据,我必须将该数据向前复制 X 年。

如果 29 存在于它不应该存在的年份,我需要删除它;如果它不存在于它应该存在的年份,我需要创建它。

编辑:

我要回到开头,以便呈现整个画面。

我得到了 3 个 csv 文件,我用它们来制作一个数据框:

  1. df0 包含我需要过滤和创建最终数据框的 4 列来自 df1
  2. df2 包含我需要过滤和创建最终结果的 3 列来自 df1 的数据框
  3. df1 是包含单个年份的数据框数据(但它最初缺少年份,我必须根据开始日期从 df0 添加它)

df0 为正确的列分区看起来像这样:

+------------+------------+--------------------+--------------------+
| project_id | start_date | degredation_factor | snapshot |
+------------+------------+--------------------+--------------------+
| pid1 | 1/1/2021 | 0.60% | 2/28/2020 18:35:46 |
| pid2 | 1/1/2024 | 0.40% | 2/28/2020 18:35:46 |
+------------+------------+--------------------+--------------------+

*请注意,其中可能包含 20 多个独特的项目

df2 看起来像这样:

+------------+---------------+--------------------+
| project_id | duration_year | snapshot |
+------------+---------------+--------------------+
| pid1 | 10 | 2/28/2020 18:35:46 |
| pid1 | 15 | 2/28/2020 18:35:46 |
| pid2 | 20 | 2/28/2020 18:35:46 |
| pid2 | 25 | 2/28/2020 18:35:46 |
+------------+---------------+--------------------+

*每个项目可以包含多行,只需要持续时间最长的行

df1 看起来像这样:

+-----+-------+------+-------------+--------------------+------------+------+
| day | month | hour | hourly_rate | snapshot | project_id | year |
+-----+-------+------+-------------+--------------------+------------+------+
| 1 | 1 | 1 | 123.43 | 2/28/2020 18:35:46 | pid1 | 2021 |
| 1 | 1 | 2 | 120.11 | 2/28/2020 18:35:46 | pid1 | 2021 |
| ... | ... | ... | ... | ... | ... | ... |
| 31 | 12 | 24 | 123.43 | 2/28/2020 18:35:46 | pid1 | 2021 |
+-----+-------+------+-------------+--------------------+------------+------+

*每一天,每一小时,一年

我必须采用那个 1 年的数据框并将每个项目的所有 future 年份附加到它。所以在 pid1 的情况下,我需要附加 2022 - 2036 并且我需要对 2022 年及以后的小时费率进行降级(公式:hourly_rate * (1 - (float(row.loc['degradation_factor_solar'] .strip('%')) * (year# - 1)/100))

我之前的代码(它正在做所有的追加和计算,除了它在所有年份都是 2 月 29 日,因此出现了问题):

        df0_partition_1 = df0[['project_id', 'start_date', 'degradation_factor', 'snapshot_datetime']]
df0_partition_2 = df0_partition_1.groupby(
['project_id', 'start_date', 'degradation_factor', 'snapshot_datetime']).size().reset_index()
df2_partition_1 = df2.groupby(['project_id', 'snapshot_datetime'])['duration_year'].max().reset_index()
df_merge = pd.merge(df0_partition_2, df2_partition_1, on=['project_id', 'snapshot_datetime'], how='left')
df_parts = df_merge[
['project_id', 'start_date', 'duration_year', 'degradation_factor', 'snapshot_datetime']].dropna()

for index, row in df_parts.iterrows():
df1_filtered = df1[(df1['project_id'] == row['project_id']) &
(df1['snapshot_datetime'] == row['snapshot_datetime'])]
df1_filtered['year'] = pd.to_datetime(row['start_date']).year
df1_filtered.reset_index(inplace=True, drop=True)
df1_filtered.drop(columns='project_name', inplace=True)
df_stg_1 = df1_filtered.copy() # deep=True)
df_stg_2 = pd.DataFrame()
df_final = pd.DataFrame()

for y in range(1, int(row['duration_year']) + 1):
year = df1_filtered['year'] + (y - 1)
hourly_production = df1_filtered['hourly_production']
df_stg_1['year'] = year
df_stg_1['hourly_production'] = hourly_production * (
1 - (float(row.loc['degradation_factor_solar'].strip('%')) * (y - 1)/ 100))
df_stg_2 = df_stg_2.append(df_stg_1)
df_final = df1_filtered.append(df_stg_2)

这给了我所有的年份,但是当在闰年输入数据时它会出现闰年问题。第 29 个存在,并且每年都会填充它。在提供数据的非闰年,将缺少闰日,因此必须从 28 日开始创建闰日。

这是之前帖子的部分重复,我很抱歉。我需要扩展以显示整个问题以及我为什么要尝试解决我要解决的问题。

如果能在闰日解决这个问题,我很乐意以不同的方式创建最终数据框。

编辑 2:

我试过 df_stg_3.drop(df_stg_3[(df_stg_3['year'] % 4 != 0) & (df_stg_3['month'] == 2) & (df_stg_3['day'] == 29 )].index)

因为 drop 实际上没有工作,我有错误的文件。

最佳答案

这是关于daymonth 合并的想法:

def move_data(df, future):
new_df = pd.DataFrame({'date':pd.date_range(f'{future}/01/01',
f'{future}/12/31',
freq='D')
})

new_df['day'] = new_df['date'].dt.day
new_df['month'] = new_df['date'].dt.month

new_df = (new_df.merge(df, on=['day','month'],how='left')
.assign(year=future)
.drop('date', axis=1)
)
return new_df

数据

   month  day  hour snapshot_datetime  year
0 2 28 1 2/28/2020 16:51 2020
1 2 28 2 2/28/2020 16:51 2020
2 2 28 3 2/28/2020 16:51 2020
3 2 29 1 2/28/2020 16:51 2020
4 2 29 2 2/28/2020 16:51 2020
5 2 29 3 2/28/2020 16:51 2020

输出

move_data(df, 2021).dropna()

day month hour snapshot_datetime year
58 28 2 1.0 2/28/2020 16:51 2021
59 28 2 2.0 2/28/2020 16:51 2021
60 28 2 3.0 2/28/2020 16:51 2021

move_data(df, 2024).dropna()

day month hour snapshot_datetime year
58 28 2 1.0 2/28/2020 16:51 2024
59 28 2 2.0 2/28/2020 16:51 2024
60 28 2 3.0 2/28/2020 16:51 2024
61 29 2 1.0 2/28/2020 16:51 2024
62 29 2 2.0 2/28/2020 16:51 2024
63 29 2 3.0 2/28/2020 16:51 2024

注意如果您的数据中有每天的数据,则不需要dropna

关于python - 处理闰日,无论何时存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60456408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com