Prophet: TypeError: ufunc 'isfinite' not supported for the input types

Created on 27 Feb 2017  路  15Comments  路  Source: facebook/prophet

I'm getting this error after calling plot or plot_components methods.

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

My first assumption was that something was wrong with the dtypes, but they seam fine:

ds datetime64[ns]
y int64
dtype: object

I'm not sure if this has to do with numpy, matplotlib o prophet. Any ideas?

bug py

Most helpful comment

I'm not sure what the precise cause of this is, but I think this could be an issue with how matplotlib handles the the numpy datetime objects. The code below replicates the error for me.

date_range = pd.date_range("01-01-2017", "31-12-2017")

df = pd.DataFrame({"ds":date_range , "upper": [2 for _ in range(len(date_range))], "lower": [1 for _ in range(len(date_range))]})

fig, axes = plt.subplots(figsize=(14,5))
axes.fill_between(df.ds, y1=df.lower, y2=df.upper)

```python

TypeError Traceback (most recent call last)
in ()
4
5 fig, axes = plt.subplots(figsize=(14,5))
----> 6 ax.fill_between(df.ds, y1=df.lower, y2=df.upper)

/app/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, args, *kwargs)
1708 warnings.warn(msg % (label_namer, func.__name__),
1709 RuntimeWarning, stacklevel=2)
-> 1710 return func(ax, args, *kwargs)
1711 pre_doc = inner.__doc__
1712 if pre_doc is None:

/app/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in fill_between(self, x, y1, y2, where, interpolate, step, **kwargs)
4830
4831 # Convert the arrays so we can work with them
-> 4832 x = ma.masked_invalid(self.convert_xunits(x))
4833 y1 = ma.masked_invalid(self.convert_yunits(y1))
4834 y2 = ma.masked_invalid(self.convert_yunits(y2))

/app/anaconda3/lib/python3.6/site-packages/numpy/ma/core.py in masked_invalid(a, copy)
2386 cls = type(a)
2387 else:
-> 2388 condition = ~(np.isfinite(a))
2389 cls = MaskedArray
2390 result = a.view(cls)

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Try the following work around, which converts the numpy datetime64[ns] type to an array of python datetime.datetime objects, which I think resolves the issue.

```python

date_range = pd.date_range("01-01-2017", "31-12-2017")

df = pd.DataFrame({"ds":date_range , "upper": [2 for _ in range(len(date_range))], "lower": [1 for _ in range(len(date_range))]})

fig, axes = plt.subplots(figsize=(14,5))
axes.fill_between(df.ds.dt.to_pydatetime(), y1=df.lower, y2=df.upper)

All 15 comments

how does your ds look?
i get the same error when i try to load not a single day per entry, but a higher resolution as 2017-02-27T12:38:03Z

edit: this is some numpy-issue... http://stackoverflow.com/questions/23808327/unexpected-exception-in-numpy-isfinite

This is the head of my dataframe:

0 2015-10-01 82
1 2015-10-02 76
2 2015-10-03 76
3 2015-10-04 65
4 2015-10-05 62

Using %Y-%m-%d datetime format

This problem came from python 2. The plots worked on python 3.

This should work with Python 2 also, do you have a traceback that shows what line in Prophet the error is coming from?

(i am running python3)

TypeError                                 Traceback (most recent call last)
<ipython-input-270-34f56bf61ffc> in <module>()
----> 1 m.plot_components(forecast)

ipython/venv/lib/python3.6/site-packages/fbprophet/forecaster.py in plot_components(self, fcst, uncertainty)
    666             ax.fill_between(
    667                 fcst['ds'].values, fcst['trend_lower'], fcst['trend_upper'],
--> 668                 color=forecast_color, alpha=0.2)
    669         ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
    670         ax.xaxis.set_major_locator(MaxNLocator(nbins=7))

ipython/venv/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1890                     warnings.warn(msg % (label_namer, func.__name__),
   1891                                   RuntimeWarning, stacklevel=2)
-> 1892             return func(ax, *args, **kwargs)
   1893         pre_doc = inner.__doc__
   1894         if pre_doc is None:

ipython/venv/lib/python3.6/site-packages/matplotlib/axes/_axes.py in fill_between(self, x, y1, y2, where, interpolate, step, **kwargs)
   4770 
   4771         # Convert the arrays so we can work with them
-> 4772         x = ma.masked_invalid(self.convert_xunits(x))
   4773         y1 = ma.masked_invalid(self.convert_yunits(y1))
   4774         y2 = ma.masked_invalid(self.convert_yunits(y2))

ipython/venv/lib/python3.6/site-packages/numpy/ma/core.py in masked_invalid(a, copy)
   2343         cls = type(a)
   2344     else:
-> 2345         condition = ~(np.isfinite(a))
   2346         cls = MaskedArray
   2347     result = a.view(cls)

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Could you attach the fcst dataframe that produces this issue so I can try to replicate it?

forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()

                ds               yhat            yhat_lower     yhat_upper
2332    2018-02-23 15:32:19 -2709.268537    -1.956903e+06   1.804301e+06
2333    2018-02-24 15:32:19 -2716.817938    -1.967014e+06   1.814499e+06
2334    2018-02-25 15:32:19 -2724.358051    -1.977111e+06   1.821530e+06
2335    2018-02-26 15:32:19 -2732.206147    -1.986902e+06   1.830387e+06
2336    2018-02-27 15:32:19 -2739.408457    -1.993734e+06   1.840862e+06

is that enough? otherwise i will have to make my jupyter notebook read new data :-)

I thought it might be the timestamps but I made a dataset with similar timestamps and couldn't replicate the issue with it (dataset attached). If you just send input data that produces this issue I could hopefully replicate it and fix it from that. Thanks!

test_data.txt

sorry, but i dont have the dataset anymore. i have been playing around with an jupyter notebook and fetching data from a TSDB at the time :-/

@MatiasSanchezCabrera do you still have a dataset that produces this issue that you could share?

I wasn't able to replicate this but that aspect of the plotting has been pretty thoroughly overhauled so I don't expect to see this issue anymore.

I'm not sure what the precise cause of this is, but I think this could be an issue with how matplotlib handles the the numpy datetime objects. The code below replicates the error for me.

date_range = pd.date_range("01-01-2017", "31-12-2017")

df = pd.DataFrame({"ds":date_range , "upper": [2 for _ in range(len(date_range))], "lower": [1 for _ in range(len(date_range))]})

fig, axes = plt.subplots(figsize=(14,5))
axes.fill_between(df.ds, y1=df.lower, y2=df.upper)

```python

TypeError Traceback (most recent call last)
in ()
4
5 fig, axes = plt.subplots(figsize=(14,5))
----> 6 ax.fill_between(df.ds, y1=df.lower, y2=df.upper)

/app/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, args, *kwargs)
1708 warnings.warn(msg % (label_namer, func.__name__),
1709 RuntimeWarning, stacklevel=2)
-> 1710 return func(ax, args, *kwargs)
1711 pre_doc = inner.__doc__
1712 if pre_doc is None:

/app/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in fill_between(self, x, y1, y2, where, interpolate, step, **kwargs)
4830
4831 # Convert the arrays so we can work with them
-> 4832 x = ma.masked_invalid(self.convert_xunits(x))
4833 y1 = ma.masked_invalid(self.convert_yunits(y1))
4834 y2 = ma.masked_invalid(self.convert_yunits(y2))

/app/anaconda3/lib/python3.6/site-packages/numpy/ma/core.py in masked_invalid(a, copy)
2386 cls = type(a)
2387 else:
-> 2388 condition = ~(np.isfinite(a))
2389 cls = MaskedArray
2390 result = a.view(cls)

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Try the following work around, which converts the numpy datetime64[ns] type to an array of python datetime.datetime objects, which I think resolves the issue.

```python

date_range = pd.date_range("01-01-2017", "31-12-2017")

df = pd.DataFrame({"ds":date_range , "upper": [2 for _ in range(len(date_range))], "lower": [1 for _ in range(len(date_range))]})

fig, axes = plt.subplots(figsize=(14,5))
axes.fill_between(df.ds.dt.to_pydatetime(), y1=df.lower, y2=df.upper)

@JKPoynter Thanks for the fix on this. This has now been included in v0.2.1 which was just pushed to PyPI.

@bletham, I am using pandas version '0.24.2' and following the snippet of my code which still results in the error TypeError: ufunc 'isfinite' not supported for the input types

df = pd.read_csv('Filename.csv') \ df['Date'] = df['Date'].astype('datetime64') \ df['Date'] = df.Date.dt.to_pydatetime() \ result = seasonal_decompose(df['ACURA'], model='multiplicative', freq=12)

Any help is greatly appreciated.

@monika0603 it looks like the seasonal_decompose function is from statsmodel, not this package. Your best bet will be to ask on their issue tracker (https://github.com/statsmodels/statsmodels)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dsvrsec picture dsvrsec  路  3Comments

L471 picture L471  路  3Comments

ChaymaeHarfoush picture ChaymaeHarfoush  路  3Comments

ahash52 picture ahash52  路  3Comments

rev3ks picture rev3ks  路  3Comments