如何将数据帧与数组时间戳合并并在数组条件下绘图?

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

我有一个时间序列数据帧和一个数组,如下所示

>> DataFrame
            datetime   value
...
0 2019-09-17 23:55:00  210.38
1 2019-09-17 23:56:00  210.50
2 2019-09-17 23:57:00  210.51
3 2019-09-17 23:58:00  210.40
4 2019-09-17 23:59:00  210.41
...
>>timestamp array
[Timestamp('2019-09-17 00:05:00'),
 Timestamp('2019-09-17 00:16:00'),
 Timestamp('2019-09-17 01:21:00'),
 Timestamp('2019-09-17 01:26:00'),
 Timestamp('2019-09-17 01:31:00'),
 Timestamp('2019-09-17 01:56:00'),
 Timestamp('2019-09-17 02:06:00'),
 Timestamp('2019-09-17 02:11:00'),
 Timestamp('2019-09-17 13:36:00'),
 Timestamp('2019-09-17 13:41:00'),
 Timestamp('2019-09-17 13:46:00'),
 Timestamp('2019-09-17 14:00:00'),
 Timestamp('2019-09-17 14:26:00')]

我想合并这两个对象,并且任何行的'datetime'值都与数组中的时间戳值相同,新列的值等于1,其他等于0。

然后,我想在图表上绘制垂直虚线或仅在数组中每次绘制一个高亮的值点。

请帮助!谢谢

python pandas matplotlib
1个回答
0
投票

我认为您需要:

#if necessary
#df['datetime'] = pd.to_datetime(df['datetime'])

df['new'] = df['datetime'].isin(arr).astype(int)

或:

df['new'] = np.where(df['datetime'].isin(arr), 1, 0)
© www.soinside.com 2019 - 2024. All rights reserved.