tcassou / causal_impact

Python package for causal inference using Bayesian structural time-series models.
232 stars 32 forks source link

"unhashable type: 'slice'" error when running ci.plot() #6

Closed zburkert closed 5 years ago

zburkert commented 5 years ago

I just downloaded Causal Impact from github and was attempting a test run with the data and code below on python 3.6.3.

I'm getting an "unhashable type: 'slice'" error when attempting the ci.plot() command, though ci.plot_components() works without issue.

Error seems to be coming from line 118 in causal_impact.py in plot(self)

Not sure if anyone else has run into this and/or has a work around, but figured I'd post since I haven't seem much documentation on this online.

========Code============ Source: https://github.com/tcassou/causal_impact/issues/5).

from causal_impact.causal_impact import CausalImpact import pandas as pd import sys from io import StringIO

DATA = """ t,y,x1,x2\n 2016-02-20 22:41:20,110.0,134.0,128.0\n 2016-02-20 22:41:30,125.0,134.0,128.0\n 2016-02-20 22:41:40,123.0,134.0,128.0\n 2016-02-20 22:41:50,128.0,134.0,128.0\n 2016-02-20 22:42:00,114.0,134.0,128.0\n 2016-02-20 22:42:10,125.0,133.0,128.0\n 2016-02-20 22:42:20,119.0,133.0,128.0\n 2016-02-20 22:42:30,121.0,133.0,128.0\n 2016-02-20 22:42:40,139.0,133.0,128.0\n 2016-02-20 22:42:50,107.0,133.0,128.0\n 2016-02-20 22:43:00,115.0,132.0,128.0\n 2016-02-20 22:43:10,91.0,132.0,128.0\n 2016-02-20 22:43:20,107.0,132.0,128.0\n 2016-02-20 22:43:30,124.0,132.0,128.0\n 2016-02-20 22:43:40,116.0,131.0,128.0\n 2016-02-20 22:43:50,110.0,131.0,128.0\n 2016-02-20 22:44:00,100.0,131.0,128.0\n 2016-02-20 22:44:10,110.0,131.0,128.0\n 2016-02-20 22:44:20,113.0,129.0,128.0\n 2016-02-20 22:44:30,103.0,129.0,128.0\n 2016-02-20 22:44:40,117.0,129.0,128.0\n 2016-02-20 22:44:50,125.0,129.0,128.0\n 2016-02-20 22:45:00,115.0,129.0,128.0\n 2016-02-20 22:45:10,114.0,128.0,128.0\n 2016-02-20 22:45:20,138.0,128.0,128.0\n 2016-02-20 22:45:30,117.0,128.0,128.0\n 2016-02-20 22:45:40,104.0,128.0,128.0\n 2016-02-20 22:45:50,123.0,128.0,128.0\n 2016-02-20 22:46:00,122.0,128.0,128.0\n 2016-02-20 22:46:10,150.0,128.0,128.0\n 2016-02-20 22:46:20,127.0,128.0,128.0\n 2016-02-20 22:46:30,139.0,128.0,128.0\n 2016-02-20 22:46:40,139.0,127.0,127.0\n 2016-02-20 22:46:50,109.0,127.0,127.0\n 2016-02-20 22:47:00,107.0,127.0,127.0\n 2016-02-20 22:47:10,94.0,127.0,127.0\n 2016-02-20 22:47:20,112.0,127.0,127.0\n 2016-02-20 22:47:30,107.0,127.0,127.0\n 2016-02-20 22:47:40,126.0,127.0,127.0\n 2016-02-20 22:47:50,114.0,127.0,127.0\n 2016-02-20 22:48:00,129.0,127.0,127.0\n 2016-02-20 22:48:10,113.0,126.0,127.0\n 2016-02-20 22:48:20,114.0,126.0,127.0\n 2016-02-20 22:48:30,116.0,126.0,127.0\n 2016-02-20 22:48:40,110.0,125.0,126.0\n 2016-02-20 22:48:50,131.0,125.0,126.0\n 2016-02-20 22:49:00,109.0,125.0,126.0\n 2016-02-20 22:49:10,114.0,125.0,127.0\n 2016-02-20 22:49:20,116.0,125.0,126.0\n 2016-02-20 22:49:30,113.0,124.0,125.0\n 2016-02-20 22:49:40,108.0,124.0,125.0\n 2016-02-20 22:49:50,120.0,124.0,125.0\n 2016-02-20 22:50:00,106.0,123.0,125.0\n 2016-02-20 22:50:10,123.0,123.0,125.0\n 2016-02-20 22:50:20,123.0,123.0,124.0\n 2016-02-20 22:50:30,135.0,123.0,124.0\n 2016-02-20 22:50:40,127.0,123.0,124.0\n 2016-02-20 22:50:50,140.0,123.0,123.0\n 2016-02-20 22:51:00,139.0,123.0,123.0\n 2016-02-20 22:51:10,137.0,123.0,123.0\n 2016-02-20 22:51:20,123.0,123.0,123.0\n 2016-02-20 22:51:30,160.0,122.0,123.0\n 2016-02-20 22:51:40,173.0,122.0,123.0\n 2016-02-20 22:51:50,236.0,122.0,123.0\n 2016-02-20 22:52:00,233.0,122.0,123.0\n 2016-02-20 22:52:10,193.0,122.0,123.0\n 2016-02-20 22:52:20,169.0,122.0,123.0\n 2016-02-20 22:52:30,167.0,122.0,123.0\n 2016-02-20 22:52:40,172.0,121.0,123.0\n 2016-02-20 22:52:50,148.0,121.0,123.0\n 2016-02-20 22:53:00,125.0,121.0,123.0\n 2016-02-20 22:53:10,132.0,121.0,123.0\n 2016-02-20 22:53:20,165.0,121.0,123.0\n 2016-02-20 22:53:30,154.0,120.0,123.0\n 2016-02-20 22:53:40,158.0,120.0,123.0\n 2016-02-20 22:53:50,135.0,120.0,123.0\n 2016-02-20 22:54:00,145.0,120.0,123.0\n 2016-02-20 22:54:10,163.0,119.0,122.0\n 2016-02-20 22:54:20,146.0,119.0,122.0\n 2016-02-20 22:54:30,120.0,119.0,121.0\n 2016-02-20 22:54:40,149.0,118.0,121.0\n 2016-02-20 22:54:50,140.0,118.0,121.0\n 2016-02-20 22:55:00,150.0,117.0,121.0\n 2016-02-20 22:55:10,133.0,117.0,120.0\n 2016-02-20 22:55:20,143.0,117.0,120.0\n 2016-02-20 22:55:30,145.0,117.0,120.0\n 2016-02-20 22:55:40,145.0,117.0,120.0\n 2016-02-20 22:55:50,176.0,117.0,120.0\n 2016-02-20 22:56:00,134.0,117.0,120.0\n 2016-02-20 22:56:10,147.0,117.0,120.0\n 2016-02-20 22:56:20,131.0,117.0,120.0"""

df = pd.read_csv(StringIO(DATA)) df["t"] = pd.to_datetime(df["t"]) df.index = df["t"] del df["t"]

ci = CausalImpact(df, pd.to_datetime('2016-02-20 22:51:20')) ci.run() ci.plot()

tcassou commented 5 years ago

Hi @zburkert, thanks for reporting I'll take a look!

tcassou commented 5 years ago

Hi again @zburkert I think this is coming from the version of statsmodels, could you share your version number? They introduced a breaking change in 0.9, and indeed the error comes from line 118: image

If you install from pip (pip install causal-impact) and don't have any conflict with statsmodels it should work fine.

Now that being said this is an annoying issues and I'll release an updated version compatible with older versions of statsmodels.

Thanks again for reporting!

tcassou commented 5 years ago

Just uploaded a new version (1.0.3), you can find it here, it should now work for versions of statsmodels < 0.9. Let me know if you still experience issues!

zburkert commented 5 years ago

Hi @tcassou,

I just updated my causal-impact package to 1.0.3 and statsmodels to 0.9.0 and reran the code above. The updates worked well. Thank you for the timely response.

One question I have; is there a way to bring up the data associated with the predictions being plotted for further analysis/investigation?

tcassou commented 5 years ago

Great to hear @zburkert ! What data would you need? Predictions? Confidence intervals? All is more or less directly available through the CausalImpact object you create, but I can also expose it more conveniently, I think that makes sense.

zburkert commented 5 years ago

All of the above.

I figured it would all be in the CausalImpact object, however I'm not entirely sure of the correct syntax to pull data out. If you could add a line or two on pulling data from the CausalImpact object once created on the Github page that would be great.

Best,

Zachary

On Tue, Oct 16, 2018 at 12:10 PM Thomas Cassou notifications@github.com wrote:

Great to hear @zburkert https://github.com/zburkert ! What data would you need? Predictions? Confidence intervals? All is more or less directly available through the CausalImpact object you create, but I can also expose it more conveniently, I think that makes sense.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tcassou/causal_impact/issues/6#issuecomment-430361664, or mute the thread https://github.com/notifications/unsubscribe-auth/AlGACrVVbNfDARzXOaOG6zA7X50k912wks5uli8wgaJpZM4XaT3n .

-- Zachary Burkert Data Analyst

P +1 661 644 3996 Connect with me on LinkedIn https://www.linkedin.com/in/zacharyburkert

https://app.salesforceiq.com/r?target=5aff13b9c9e77c00797716e9&t=AFwhZf15-c4xWEK-8W_pqm5xc_giikHKuCbkP-5juHmSQi5fRP3vFLx55CZUzXBl8B9-XJ2KKVWo-Bdq80XX0i3Vy47gYm975cNWqkpA_9ROLXDordt9VawO8kTIBQurQg8LK6kb6n_3

https://app.salesforceiq.com/r?target=5aff13b9c9e77c00797716ea&t=AFwhZf15-c4xWEK-8W_pqm5xc_giikHKuCbkP-5juHmSQi5fRP3vFLx55CZUzXBl8B9-XJ2KKVWo-Bdq80XX0i3Vy47gYm975cNWqkpA_9ROLXDordt9VawO8kTIBQurQg8LK6kb6n_3

https://app.salesforceiq.com/r?target=5aff13b9c9e77c00797716eb&t=AFwhZf15-c4xWEK-8W_pqm5xc_giikHKuCbkP-5juHmSQi5fRP3vFLx55CZUzXBl8B9-XJ2KKVWo-Bdq80XX0i3Vy47gYm975cNWqkpA_9ROLXDordt9VawO8kTIBQurQg8LK6kb6n_3

https://app.salesforceiq.com/r?target=5aff13b9c9e77c00797716ec&t=AFwhZf15-c4xWEK-8W_pqm5xc_giikHKuCbkP-5juHmSQi5fRP3vFLx55CZUzXBl8B9-XJ2KKVWo-Bdq80XX0i3Vy47gYm975cNWqkpA_9ROLXDordt9VawO8kTIBQurQg8LK6kb6n_3

https://app.salesforceiq.com/r?target=5aff13b9c9e77c00797716ed&t=AFwhZf15-c4xWEK-8W_pqm5xc_giikHKuCbkP-5juHmSQi5fRP3vFLx55CZUzXBl8B9-XJ2KKVWo-Bdq80XX0i3Vy47gYm975cNWqkpA_9ROLXDordt9VawO8kTIBQurQg8LK6kb6n_3

tcassou commented 5 years ago

I just created a new issue (#7) to use a dedicated thread, I'll try to release something in the coming days!

tcassou commented 5 years ago

@zburkert I just released 1.1.0 which exposes a result attribute containing all data used in the plots.

Happy to collect your feedback if you try it out!