jamalsenouci / causalimpact

Python port of CausalImpact R library
Apache License 2.0
265 stars 63 forks source link

Error in the index attribute #6

Closed nalsabhan closed 6 years ago

nalsabhan commented 6 years ago

First of all, thanks for this package. Moving back and forth from python to R is frustrating. I have an issue. My index is RangeIndex(start=0, stop=152, step=1) and pre_period = [0,109]; post_period = [111,151]. I have 2 columns in pandas and when I pass it to CausalImpact I get the following error:

AttributeError                            Traceback (most recent call last)
<ipython-input-86-f56cb9d3e7f1> in <module>()
----> 1 impact.run()

/Users/nawafalsabhan/anaconda/lib/python2.7/site-packages/causalimpact/analysis.pyc in run(self)
     33                                     self.params["ucm_model"],
     34                                     self.params["post_period_response"],
---> 35                                     self.params["alpha"])
     36 
     37         # Depending on input, dispatch to the appropriate Run* method()

/Users/nawafalsabhan/anaconda/lib/python2.7/site-packages/causalimpact/analysis.pyc in _format_input(self, data, pre_period, post_period, model_args, ucm_model, post_period_response, alpha)
    209         # Check <pre_period> and <post_period>
    210         if data is not None:
--> 211             checked = self._format_input_prepost(pre_period, post_period, data)
    212             pre_period = checked["pre_period"]
    213             post_period = checked["post_period"]

/Users/nawafalsabhan/anaconda/lib/python2.7/site-packages/causalimpact/analysis.pyc in _format_input_prepost(self, pre_period, post_period, data)
    104         pre_dtype = np.array(pre_period).dtype
    105         post_dtype = np.array(post_period).dtype
--> 106         if isinstance(data.index, pd.tseries.index.DatetimeIndex):
    107             pre_period = [pd.to_datetime(date) for date in pre_period]
    108             post_period = [pd.to_datetime(date) for date in post_period]

AttributeError: 'module' object has no attribute 'index'
bange83 commented 6 years ago

hey you might try to Change the line if isinstance(data.index, pd.tseries.index.DatetimeIndex): to if isinstance(data.index, pd.core.indexes.datetimes.DatetimeIndex):

this is probably caused by a too new Pandas Version you use where they changed some stuff with the index

jamalsenouci commented 6 years ago

ah well spotted, that seems to have fixed it now, i had moved to pandas 0.20 but was running tests against 0.19 so they didn't pick this up