✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚾️ It's finally Baseball season! Root for the home team... & Register for our Sports Analytics Webinar!

How can I fill gaps in a bar chart in Plotly?

I want to create a bar chart with a zero bar gap in Plotly using the following data:

          date  mood
0   2017-08-17   NaN
1   2017-08-18   NaN
2   2017-08-19   NaN
3   2017-08-20   NaN
4   2017-08-21   NaN
5   2017-08-22   NaN
6   2017-08-23   NaN
7   2017-08-24   NaN
8   2017-08-25   NaN
9   2017-08-26   NaN
10  2017-08-27   NaN
...        ...   ...
267 2018-08-03  56.0
268 2018-08-06  56.0
269 2018-08-07  55.0
270 2018-08-08  54.0
271 2018-08-09  54.0
272 2018-08-10  54.0
273 2018-08-13  54.0
274 2018-08-14  54.0
275 2018-08-15  53.0
276 2018-08-16  53.0

The y-axis contains the mood values while the x-axis has all of the dates between the 17th of August, 2018 (which is included) and the 17th of August, 2017 (which is not included). Some of the values for mood are “null” and that should be displayed in the chart. However, as you can notice, not all of the dates in that period are in the “date” column of the data frame. Thus, empty gaps appear in the chart because Plotly recognises that the data in the x-axis is a time series and the chart looks like this:

I want to fill those gaps by giving every missing date a corresponding mood value equal to the last previous non-empty value. For example, “2018-08-04” and “2018-08-05” are missing dates and their corresponding y-values should be the same as the one of "2018-08-03” - “56.0”.

I was looking for an easy way to do this but I couldn’t find one. There is a “connectgaps” attribute in Plotly but it can only be used in line charts and there isn’t a similar quick solution for bar charts.

Is there a solution for that problem that doesn’t involve modifying the dataframe? Any advise or guidance would be greatly appreciated. Thank you.

I would filter those NaN using a transform: