prophet icon indicating copy to clipboard operation
prophet copied to clipboard

KeyError: 'y' with DateTime64

Open hassheh opened this issue 8 years ago • 3 comments

The code below throws an exception Code:

data = df[['a', 'Timeseries', 'c']]
dates = data['Timeseries']
dates = dates.sort_index()

m = Prophet()
m.fit(dates)
future = m.make_future_dataframe(period=366)

Data:

dtype: datetime64

0 2015-10-12 08:05:32.000 1 2015-10-06 15:03:45.000 2 2015-09-09 11:04:43.000 3 2015-09-22 15:30:10.000 4 2015-09-28 10:43:31.000 5 2015-10-01 12:54:22.000 6 2015-08-28 09:05:37.000 7 2015-08-31 10:21:01.000 8 2015-08-31 22:17:56.000 9 2015-08-31 22:21:23.000 10 2015-09-02 10:06:22.000 11 2015-09-02 15:50:57.000 12 2015-09-02 15:52:47.000 13 2015-09-08 10:05:33.000 14 2015-07-16 11:36:21.000 15 2015-07-21 11:06:18.000 16 2015-07-22 13:34:51.000 17 2015-07-30 08:21:35.000 18 2015-07-30 16:02:32.000 And so on

Error:

Out:

TypeError Traceback (most recent call last) pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

TypeError: an integer is required

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last) in () 1 #%% Prophet 2 m = Prophet() ----> 3 m.fit(dates) 4 future = m.make_future_dataframe(period=366)

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\fbprophet\forecaster.py in fit(self, df, **kwargs) 771 raise Exception('Prophet object can only be fit once. ' 772 'Instantiate a new object.') --> 773 history = df[df['y'].notnull()].copy() 774 if history.shape[0] < 2: 775 raise ValueError('Dataframe has less than 2 non-NaN rows.')

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pandas\core\series.py in getitem(self, key) 621 key = com._apply_if_callable(key, self) 622 try: --> 623 result = self.index.get_value(self, key) 624 625 if not is_scalar(result):

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key) 2558 try: 2559 return self._engine.get_value(s, k, -> 2560 tz=getattr(series.dtype, 'tz', None)) 2561 except KeyError as e1: 2562 if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

KeyError: 'y'

hassheh avatar Mar 21 '18 11:03 hassheh

The data you presented ( and maybe fitted ) only shows the datetimes, but not the values for that date (which should be placed inside a y column, the reason why it returns a 'y' KeyError)

Data fitted into Prophet must be something like this

ds y 2015-10-12 08:05:32.000 15 2015-10-06 15:03:45.000 16 2015-09-09 11:04:43.000 18 2015-09-22 15:30:10.000 20 2015-09-28 10:43:31.000 59 2015-10-01 12:54:22.000 78

fjcamillo avatar Mar 22 '18 10:03 fjcamillo

What if I don't have y column data? I mean each date is an event without any label. i want to predict those events. Any idea on how to use Prophet without y?

hassheh avatar Mar 22 '18 11:03 hassheh

If you're refering to fitting prophet with the 'Y' column. You basically couldn't create a model without giving it a label to fit into.

But, if you're refering to predicting, you could give Prophet a dataframe of the dates you want to predict

How: You must first fit Prophet with the complete data [ ds, y ].

m = Prophet()
m.fit(df)

Then create a dataframe with your own dates

our_own_dates = pd.DataFrame({'ds': [datetime.datetime(year=2018, month=3,day=4]})

Once fitted, the line

forecast = m.predict(our_own_dates)

should return the predicted values for that day

fjcamillo avatar Mar 23 '18 04:03 fjcamillo