How to not plan absent periods

I am trying to build time series data where there is no data at certain periods. Data is loaded into a dataframe, and I draw it using df.plot(). The problem is that the missing periods are connected during the construction of the chart, giving the impression that this value exists during this period, while it is absent.

Here is an example of a problem

problem

There is no data between Sep 01 and Sep 08, and also between Sep 09 and Sep 25, but the data is displayed so that it seems that there are values ​​in this period.

I would like to have zero values ​​visualized during this period, or no values ​​at all. How to do it?

To be clear, I do not have NaN values ​​for the periods [Sep 01, Sep 08], [Sep 09, Sep 29], but there is no data at all (not even in the time index).

+4
source share
2 answers

You must add the missing dates in your data framework with NaN values. Then, when they are depicted, these NaNs break the line - you will get several line segments, with empty periods between them.

This answer explains how to add missing dates to your framework. To summarize, this should do the trick:

df = df.reindex(pd.DatetimeIndex(df.index), fill_value=NaN)
+3
source

Consider pd.Series s

s = pd.Series(
    np.arange(10), pd.date_range('2016-03-31', periods=10)
).replace({3: np.nan, 6: np.nan})

s.plot()

enter image description here

You can see that they np.nanwere skipped.
However:

s.fillna(0).plot()

enter image description here

0 not skipped.

I suggest s.replace(0, np.nan).plot()

+3
source

Source: https://habr.com/ru/post/1667964/


All Articles