我有一个基于分钟的货币数据,如下所示。我想要做的是,如果缺少一分钟,例如如果它从 00:02:00 跳到 00:05:00 我想创建一个具有相同“日期”和“AUDJPY”值的新行。我希望下面的数据框是:我不知道该怎么做,我什至不明白如何根据条件添加行。
AUDJPY Date Time
0 76.150 2020-01-02 00:00:00
1 76.151 2020-01-02 00:01:00
2 76.152 2020-01-02 00:02:00
3 76.162 2020-01-02 00:05:00
4 76.164 2020-01-02 00:06:00
5 76.175 2020-01-02 00:07:00
6 76.162 2020-01-02 00:08:00
7 76.153 2020-01-02 00:09:00
AUDJPY Date Time
0 76.150 2020-01-02 00:00:00
1 76.151 2020-01-02 00:01:00
2 76.152 2020-01-02 00:02:00
3 76.152 2020-01-02 00:03:00
4 76.152 2020-01-02 00:04:00
5 76.162 2020-01-02 00:05:00
6 76.164 2020-01-02 00:06:00
7 76.175 2020-01-02 00:07:00
8 76.162 2020-01-02 00:08:00
9 76.153 2020-01-02 00:09:00
答案 0 :(得分:1)
你可以试试:
df['Date-Time'] = pd.to_datetime(df['Date'] + ' ' + df['Time'])
k = df.set_index('Date-Time').asfreq('T').fillna(method = 'ffill').drop(['Date', 'Time'], 1).reset_index()
如果要将日期/时间与日期时间分开:
k[['Date', 'Time']] = k['Date-Time'].astype(str).str.split(' ', expand = True)