Reputation: 1186
Hi I'm trying to generate a time series forecasting model using python FbProphet model. I'm getting the following error during its implementation. Following is the error I received.
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () 36 f14.set_figheight(5) 37 f14.set_figwidth(15) ---> 38 fig = model_prop.plot(forecast, ax=ax14)
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\fbprophet\forecaster.py in plot(self, fcst, ax, uncertainty, plot_cap, xlabel, ylabel) 1520 return plot( 1521 m=self, fcst=fcst, ax=ax, uncertainty=uncertainty, -> 1522 plot_cap=plot_cap, xlabel=xlabel, ylabel=ylabel, 1523 ) 1524
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\fbprophet\plot.py in plot(m, fcst, ax, uncertainty, plot_cap, xlabel, ylabel, figsize) 68 fig = ax.get_figure() 69 fcst_t = fcst['ds'].dt.to_pydatetime() ---> 70 ax.plot(m.history['ds'].dt.to_pydatetime(), m.history['y'], 'k.') 71 ax.plot(fcst_t, fcst['yhat'], ls='-', c='#0072B2') 72 if 'cap' in fcst and plot_cap:
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\matplotlib__init__.py in inner(ax, *args, **kwargs) 1853 "the Matplotlib list!)" % (label_namer, func.name), 1854
RuntimeWarning, stacklevel=2) -> 1855 return func(ax, *args, **kwargs) 1856 1857 inner.doc = _add_data_doc(inner.doc,C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\matplotlib\axes_axes.py in plot(self, *args, **kwargs) 1526 1527 for line in self._get_lines(*args, **kwargs): -> 1528 self.add_line(line) 1529 lines.append(line) 1530
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\matplotlib\axes_base.py in add_line(self, line) 1930
line.set_clip_path(self.patch) 1931 -> 1932 self._update_line_limits(line) 1933 if not line.get_label(): 1934 line.set_label('_line%d' % len(self.lines))C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\matplotlib\axes_base.py in _update_line_limits(self, line) 1952 Figures out the data limit of the given line, updating self.dataLim. 1953
""" -> 1954 path = line.get_path() 1955 if path.vertices.size == 0: 1956 returnC:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\matplotlib\lines.py in get_path(self) 949 """ 950 if self._invalidy or self._invalidx: --> 951 self.recache() 952 return self._path 953
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\matplotlib\lines.py in recache(self, always) 650 if always or self._invalidx: 651 xconv = self.convert_xunits(self._xorig) --> 652 x = _to_unmasked_float_array(xconv).ravel() 653 else: 654 x = self._x
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\matplotlib\cbook__init__.py in _to_unmasked_float_array(x) 2048 return np.ma.asarray(x, float).filled(np.nan) 2049 else: -> 2050 return np.asarray(x, float) 2051 2052
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order) 536 537 """ --> 538 return array(a, dtype, copy=False, order=order) 539 540
TypeError: float() argument must be a string or a number, not 'datetime.datetime'
Following is the code that I tried out
for i in Brokers:
series2=TimeSeriesData[TimeSeriesData.BrokerName == b][['Gross_Premium']]
series2['ds']=Broker_Clusters.index
series2['y'] = series2.Gross_Premium
series2=series2.drop('Gross_Premium',axis=1)
series2['ds'] = series2['ds'].apply(str)
#divide into train and validation set
train = series2[:int(0.7*(len(series2)))]
test = series2[int(0.7*(len(series2))):]
model_prop=Prophet()
model_prop.fit(train)
forecast = model_prop.predict(test)
# Plot the forecast
f14, ax14 = plt.subplots(1)
f14.set_figheight(5)
f14.set_figwidth(15)
fig = model_prop.plot(forecast, ax=ax14)
Following is my test dataset
ds y
Date
2016-03-15 2016-03-15 00:00:00 0.00000
2016-04-15 2016-04-15 00:00:00 180534.47230
2016-05-15 2016-05-15 00:00:00 0.00000
2016-06-15 2016-06-15 00:00:00 2545.12000
2016-07-15 2016-07-15 00:00:00 0.00000
2016-08-15 2016-08-15 00:00:00 0.00000
2016-09-15 2016-09-15 00:00:00 0.00000
2016-10-15 2016-10-15 00:00:00 20637.88000
2016-11-15 2016-11-15 00:00:00 0.00000
2016-12-15 2016-12-15 00:00:00 0.00000
2017-01-15 2017-01-15 00:00:00 0.00000
2017-02-15 2017-02-15 00:00:00 0.00000
2017-03-15 2017-03-15 00:00:00 8878.08000
2017-04-15 2017-04-15 00:00:00 198174.32710
2017-05-15 2017-05-15 00:00:00 0.00000
2017-06-15 2017-06-15 00:00:00 3458.61875
2017-07-15 2017-07-15 00:00:00 0.00000
2017-08-15 2017-08-15 00:00:00 0.00000
2017-09-15 2017-09-15 00:00:00 0.00000
2017-10-15 2017-10-15 00:00:00 5159.47000
2017-11-15 2017-11-15 00:00:00 0.00000
2017-12-15 2017-12-15 00:00:00 0.00000
Upvotes: 1
Views: 3635
Reputation: 41
Tried adding the following right before plot()
call
pd.plotting.register_matplotlib_converters()
and it works for me.
Upvotes: 4
Reputation: 108
Your problem is type
of the date column you have. In your case, it is a string and supposes to be a timestamp.
Instead:
series2['ds'] = series2['ds'].apply(str)
Use:
series2['ds'] = list(map(lambda x: pd.to_datetime(x), series2['ds']))
Upvotes: 1