私は、CSVを介して供給されたデータセットを持っています。そのCSVがうるう年で供給されているときはいつでも、それは2月の29日が含まれており、それが非閏年に供給されるたびにそれはしていません。私は、データのみの単年度を取得し、私は長年のX量のために前方にそのデータをコピーする必要があります。
それは数年内に存在する場合、私はそれはそれが必要年に存在しない場合、それはそれを作成しないし、すべき第29回を削除する必要があります。
EDIT:
全体像が提示されるように、私が先頭に背をつもりです。
私は、単一のデータフレームを作るために利用することを3つのCSVファイルを与えています:
- DF0は、私はフィルタに必要とDF1からの最終データフレームを作成することを4列が含まれています
- DF2は、私はフィルタに必要と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年
私はすべてのプロジェクトのために、すべての前進の年、それまでその1年間のデータフレームとAPPENDを取らなければなりません。そう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日から作成されなければならないとき。
これは今の前のポスト、私の謝罪の部分的な重複です。私は、全体の問題で、なぜ私は私が解決しようとしているものを解決しようとしていますを表示するために展開する必要がありました。
私はそれは閏日のために、この問題を取り除く場合は、別の方法で、最終的なデータフレームを作成することが幸せです。
EDIT 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)
ドロップが実際に動作していないため、私は間違ったファイルを持っていました。
私が終わったのはここです。これは、最もエレガントではないかもしれないが、それは作業を行います。
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_solar', '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'])]
dest_key_name = 'dw/project_8760/{}/{}/{}'.format(row['project_id'], file_partition_time, 'df.csv')
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()
df_stg_2 = pd.DataFrame()
df_final = pd.DataFrame()
for y in range(2, 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'].strip('%')) * (y - 1)/ 100))
df_stg_2 = df_stg_2.append(df_stg_1)
df_stg_3 = df1_filtered.append(df_stg_2)
if df1_filtered[(df1_filtered['month'] == 2) & (df1_filtered['day'] == 29)].empty is False :
filter_final_1 = df_stg_3[(df_stg_3.year % 4 != 0) & (df_stg_3.month == 2)]
filter_final_2 = filter_final_1[(filter_final_1.day == 29)]
df_final = pd.concat([df_stg_3, filter_final_2, filter_final_2]).drop_duplicates(keep=False)
else:
df_stg_3_filtered = df_stg_3[(df_stg_3.year % 4 == 0) & (df_stg_3.month == 2)]
df_stg_4 = df_stg_3_filtered[(df_stg_3_filtered.day == 28)]
df_stg_4['day'] = 29
df_final = pd.concat([df_stg_3, df_stg_4])
df_final.sort_values(by=['year', 'month', 'day', 'hour'])