jamalsenouci / causalimpact

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

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). #18

Closed chandanshikhar1 closed 3 years ago

chandanshikhar1 commented 5 years ago

I was able to run the code all fine until I upgraded some of the packages and broke the code that was running fine. Looks like something related to Python3.6. Can you help me fix this ?

Even the example code given in the documentation is breaking.

import numpy as np
import pandas as pd
from statsmodels.tsa.arima_process import arma_generate_sample
import matplotlib
import seaborn as sns
%matplotlib inline
matplotlib.rcParams['figure.figsize'] = (15, 6)

# Data Prep
np.random.seed(1)
x1 = arma_generate_sample(ar=[0.999], ma=[0.9], nsample=100) + 100
y = 1.2 * x1 + np.random.randn(100)
y[71:100] = y[71:100] + 10
data = pd.DataFrame(np.array([y, x1]).T, columns=["y","x1"])

# Model
pre_period = [0,69]
post_period = [70,99]
impact = CausalImpact(data, pre_period, post_period)
impact.run()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-62-a062d48e2479> in <module>()
     18 post_period = [70,99]
     19 impact = CausalImpact(data, pre_period, post_period)
---> 20 impact.run()

/Users/shikhardua/anaconda3/lib/python3.6/site-packages/causalimpact/analysis.py 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/shikhardua/anaconda3/lib/python3.6/site-packages/causalimpact/analysis.py in _format_input(self, data, pre_period, post_period, model_args, ucm_model, post_period_response, alpha)
    205         # representing time points
    206         if data is not None:
--> 207             data = self._format_input_data(data)
    208 
    209         # Check <pre_period> and <post_period>

/Users/shikhardua/anaconda3/lib/python3.6/site-packages/causalimpact/analysis.py in _format_input_data(self, data)
     75         # Must not have NA in covariates (if any)
     76         if len(data.columns) >= 2:
---> 77             if np.any(pd.isnull(data.iloc[:, 1:])):
     78                 raise ValueError("covariates must not contain null values")
     79 

/Users/shikhardua/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py in __nonzero__(self)
    951         raise ValueError("The truth value of a {0} is ambiguous. "
    952                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 953                          .format(self.__class__.__name__))
    954 
    955     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
chandanshikhar1 commented 5 years ago

I tried running line 77 with my dataset and it returns a series

---> 77 if np.any(pd.isnull(data.iloc[:, 1:])):

Austria    False
Croatia    False
France     False
Germany    False
Italy      False
Poland     False
Romania    False
Spain      False
Turkey     False
dtype: bool