以下に示すような入力リストとデータフレームがあります。
[{"type": "linear",
"from": "2020-02-04T20:00:00.000Z",
"to": "2020-02-03T20:00:00.000Z",
"days":3,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
},
{"type": "quadratic",
"from": "2020-02-03T20:00:00.000Z",
"to": "2020-02-10T20:00:00.000Z",
"days":3,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
},
{"type": "polynomial",
"from": "2020-02-05T20:00:00.000Z",
"to": "2020-02-03T20:00:00.000Z",
"days":3,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
}]
df:
Date t_factor
2020-02-01 5
2020-02-02 23
2020-02-03 14
2020-02-04 23
2020-02-05 23
2020-02-06 23
2020-02-07 30
2020-02-08 29
2020-02-09 100
2020-03-10 38
2020-03-11 38
2020-03-12 38
2020-03-13 70
2020-03-14 70
ステップ1:辞書の「from」キーの値に基づいてリストを並べ替えます
[
{"type": "quadratic",
"from": "2020-02-03T20:00:00.000Z",
"to": "2020-02-10T20:00:00.000Z",
"days":3,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
},
{"type": "linear",
"from": "2020-02-04T20:00:00.000Z",
"to": "2020-02-03T20:00:00.000Z",
"days":3,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
},
{"type": "polynomial",
"from": "2020-02-05T20:00:00.000Z",
"to": "2020-02-03T20:00:00.000Z",
"days":3,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
}]
ステップ2:dfの最小日付として「from」キーの値を持つ辞書を追加し、「to」はソートされたリストの最初の辞書の「from」日付である必要があります。"days" = 0、 "coef":[0.1,0.1,0.1,0.1,0.1,0.1]。
{"type": "df_first",
"from": "2020-02-01T20:00:00.000Z",
"to": "2020-02-03T20:00:00.000Z",
"days":0,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
}
ステップ3:「from」キーの値がdfの最小日付の7日後である辞書を追加し、「to」はfromの1日後でなければなりません。
{"type": "df_mid",
"from": "2020-02-08T20:00:00.000Z",
"to": "2020-02-09T20:00:00.000Z",
"days":0,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
}
ステップ4:dfの最大日付として「from」キーの値を持つ辞書を追加し、「to」は「from」と同じである必要があります。
{"type": "df_last",
"from": "2020-02-14T20:00:00.000Z",
"to": "2020-02-14T20:00:00.000Z",
"days":0,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
}
手順5:「開始」日付に基づいてすべての辞書を並べ替えます。
期待される出力:
[{"type": "df_first",
"from": "2020-02-01T20:00:00.000Z",
"to": "2020-02-03T20:00:00.000Z",
"days":0,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
},
{"type": "quadratic",
"from": "2020-02-03T20:00:00.000Z",
"to": "2020-02-10T20:00:00.000Z",
"days":3,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
},
{"type": "linear",
"from": "2020-02-04T20:00:00.000Z",
"to": "2020-02-03T20:00:00.000Z",
"days":3,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
},
{"type": "polynomial",
"from": "2020-02-05T20:00:00.000Z",
"to": "2020-02-03T20:00:00.000Z",
"days":3,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
},
{"type": "df_mid",
"from": "2020-02-08T20:00:00.000Z",
"to": "2020-02-09T20:00:00.000Z",
"days":0,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
},
{"type": "df_last",
"from": "2020-02-14T20:00:00.000Z",
"to": "2020-02-14T20:00:00.000Z",
"days":0,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
}
]
ステップ6:
各辞書の「to」値を次の辞書の「from」値に置き換えます。最後の辞書の「to」値はそのままです。
期待される最終出力:
[{"type": "df_first",
"from": "2020-02-01T20:00:00.000Z",
"to": "2020-02-03T20:00:00.000Z",
"days":0,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
},
{"type": "quadratic",
"from": "2020-02-03T20:00:00.000Z",
"to": "2020-02-04T20:00:00.000Z",
"days":3,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
},
{"type": "linear",
"from": "2020-02-04T20:00:00.000Z",
"to": "2020-02-05T20:00:00.000Z",
"days":3,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
},
{"type": "polynomial",
"from": "2020-02-05T20:00:00.000Z",
"to": "2020-02-08T20:00:00.000Z",
"days":3,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
},
{"type": "df_mid",
"from": "2020-02-08T20:00:00.000Z",
"to": "2020-02-14T20:00:00.000Z",
"days":0,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
},
{"type": "df_last",
"from": "2020-02-14T20:00:00.000Z",
"to": "2020-02-14T20:00:00.000Z",
"days":0,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
}
]
機能の定義add_dct
辞書のリストとして引数をとりますlst
と_type
、_from
と_to
とに、新しい辞書を追加しますlst
:
dmin, dmax = df['Date'].min(), df['Date'].max()
def add_dct(lst, _type, _from, _to):
lst.append({
'type': _type,
'from': _from if isinstance(_from, str) else _from.strftime("%Y-%m-%dT20:%M:%S.000Z"),
'to': _to if isinstance(_to, str) else _to.strftime("%Y-%m-%dT20:%M:%S.000Z"),
'days': 0,
"coef":[0.1,0.1,0.1,0.1,0.1,0.1]
})
predefined
要件に応じて、次の手順に従います。
# STEP 1
lst = sorted(lst, key=lambda d: pd.Timestamp(d['from']))
# STEP 2
add_dct(lst, 'df_first', dmin, lst[0]['from'])
# STEP 3
add_dct(lst, 'df_mid', dmin + pd.Timedelta(days=7), dmin + pd.Timedelta(days=8))
# STEP 4
add_dct(lst, 'df_last', dmax, dmax)
# STEP 5
lst = sorted(lst, key=lambda d: pd.Timestamp(d['from']))
結果:
[{'type': 'df_first',
'from': '2020-02-01T20:00:00.000Z',
'to': '2020-02-03T20:00:00.000Z',
'days': 0,
'coef': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]},
{'type': 'quadratic',
'from': '2020-02-03T20:00:00.000Z',
'to': '2020-02-10T20:00:00.000Z',
'days': 3,
'coef': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]},
{'type': 'linear',
'from': '2020-02-04T20:00:00.000Z',
'to': '2020-02-03T20:00:00.000Z',
'days': 3,
'coef': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]},
{'type': 'polynomial',
'from': '2020-02-05T20:00:00.000Z',
'to': '2020-02-03T20:00:00.000Z',
'days': 3,
'coef': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]},
{'type': 'df_mid',
'from': '2020-02-08T20:00:00.000Z',
'to': '2020-02-09T20:00:00.000Z',
'days': 0,
'coef': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]},
{'type': 'df_last',
'from': '2020-03-14T20:00:00.000Z',
'to': '2020-03-14T20:00:00.000Z',
'days': 0,
'coef': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1]}]
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加