jamalsenouci / causalimpact

Python port of CausalImpact R library
Apache License 2.0
262 stars 62 forks source link

fixed plot error causing matplotlib axvline to fail with time ranges #22

Closed raphael-assal closed 2 years ago

raphael-assal commented 2 years ago

When instanciating CausalImpact with date ranges for pre/post periods, the method plot causes matplotlib to fail: "ConversionError: Failed to convert value(s) to axis units".

This is because of matplotlib expects Timestamps on the horizontal axis of the plot created using a DatetimeIndex (self.data.index) and fails to convert the provided String argument (data_inter).

Here's a minimal code to reproduce this:

import numpy as np
import pandas as pd

self_data_index = pd.date_range('2021-01-01', '2021-01-31')
data_inter = '2021-01-15'

plt.plot(self_data_index, np.zeros(self_data_index.shape[0]))

plt.axvline(data_inter) # Fails
>>> ConversionError: Failed to convert value(s) to axis units: '2021-01-15'

plt.axvline(pd.Timestamp(data_inter)) # Works
>>> Works.