Fill up missing datetime with NaN or supress straight line in line plot Fill up missing datetime with NaN or supress straight line in line plot pandas pandas

Fill up missing datetime with NaN or supress straight line in line plot


Let's say we have your example data frame but with the three rows in the middle missing:

In [65]: dfOut[65]:              DATETIME     LEVEL0 2019-04-23 16:30:00  0.0870741 2019-04-23 16:35:00  0.0930892 2019-04-23 16:40:00  0.0811033 2019-04-23 17:00:00  0.0871594 2019-04-23 17:05:00  0.0871745 2019-04-23 17:10:00  0.087188

Now we can fill those missing values by indexing the DataFrame with the DATETIME column and then resample() that. Afterwards we kann reset the index again to turn the index back into a normal column again:

In [66]: df.set_index('DATETIME').resample('5min').first().reset_index()Out[66]:              DATETIME     LEVEL0 2019-04-23 16:30:00  0.0870741 2019-04-23 16:35:00  0.0930892 2019-04-23 16:40:00  0.0811033 2019-04-23 16:45:00       NaN4 2019-04-23 16:50:00       NaN5 2019-04-23 16:55:00       NaN6 2019-04-23 17:00:00  0.0871597 2019-04-23 17:05:00  0.0871748 2019-04-23 17:10:00  0.087188


You just need to plot two lines instead of one.Divide the dataset where you need it and plot them separately.Here a simple example with two straight line

plt.plot([1,2,3,4],[5,5,5,5])plt.plot([7,8,9,10],[6,6,6,6])plt.show()

Example plot