prophet icon indicating copy to clipboard operation
prophet copied to clipboard

'list' object has no attribute 'tolist'

Open RiskySignal opened this issue 3 years ago • 1 comments

While fit my data by updating fitted model, the following error appears

Traceback (most recent call last):
  File "/dockerdata/jiangszhang/anaconda3/envs/abnormal_detection/lib/python3.7/site-packages/joblib/externals/loky/process_executor.py", line 436, in _process_worker
    r = call_item()
  File "/dockerdata/jiangszhang/anaconda3/envs/abnormal_detection/lib/python3.7/site-packages/joblib/externals/loky/process_executor.py", line 288, in __call__
    return self.fn(*self.args, **self.kwargs)
  File "/dockerdata/jiangszhang/anaconda3/envs/abnormal_detection/lib/python3.7/site-packages/joblib/_parallel_backends.py", line 595, in __call__
    return self.func(*args, **kwargs)
  File "/dockerdata/jiangszhang/anaconda3/envs/abnormal_detection/lib/python3.7/site-packages/joblib/parallel.py", line 263, in __call__
    for func, args, kwargs in self.items]
  File "/dockerdata/jiangszhang/anaconda3/envs/abnormal_detection/lib/python3.7/site-packages/joblib/parallel.py", line 263, in <listcomp>
    for func, args, kwargs in self.items]
  File "/home/jiangszhang/private_sz/abnormal_detection/abnormal_detection/kafka_comsumer/detection_utils.py", line 84, in get_abnormal_score
    prophet_model.fit(staff_dataframe, init=warm_starting_setting)
  File "/dockerdata/jiangszhang/anaconda3/envs/abnormal_detection/lib/python3.7/site-packages/prophet/forecaster.py", line 1171, in fit
    self.params = self.stan_backend.fit(stan_init, dat, **kwargs)
  File "/dockerdata/jiangszhang/anaconda3/envs/abnormal_detection/lib/python3.7/site-packages/prophet/models.py", line 90, in fit
    kwargs['inits'] = self.prepare_data(kwargs['init'], stan_data)[0]
  File "/dockerdata/jiangszhang/anaconda3/envs/abnormal_detection/lib/python3.7/site-packages/prophet/models.py", line 164, in prepare_data
    'y': data['y'].tolist(),
AttributeError: 'list' object has no attribute 'tolist'

After some tries, I found prophet==1.0.1 works fine but the latest version prophet==1.1 not.

RiskySignal avatar Jul 13 '22 01:07 RiskySignal

So there must be an error in your input data. I have no issue running this on mine, but the pd.Series of data['y'] should have the method tolist(). So, the issue is that this data is reading the Series as a list instead of a pandas Series object. If we had an example of what the y column in your data looked like, we could provide some guidance.

ghost avatar Aug 06 '22 03:08 ghost

def fit(self, stan_init, stan_data, **kwargs):
    (stan_init, stan_data) = self.prepare_data(stan_init, stan_data)

    if 'inits' not in kwargs and 'init' in kwargs:
        kwargs['inits'] = self.prepare_data(kwargs['init'], stan_data)[0]

I think the problem is here (method fit in prophet/python/prophet/models.py). if "init" in kwargs, then stan_data will be pass into self.prepare_data, but stan_data is a dict not DataFrame.

thecourageyu avatar Oct 24 '22 03:10 thecourageyu

I got the same issue. I have a model and tried to warm-start a retrain by passing a Stan init instance into the init= arg, and my input data is a Pandas data frame. I think the issue was caused because in CmdStanPyBackend.fit, it first processes the (stan_init, stan_data) = self.prepare_data(stan_init, stan_data) regardless, so the input data frame was converted into a output data dict and reprocessed again by self.prepare_data(kwargs['init'], stan_data)[0]. For the 2nd processing, tolist() does not work simply because now the input is no longer a data frame but a data dict.

jackXiaofeiHu avatar Dec 16 '22 01:12 jackXiaofeiHu

models.py

def fit(self, stan_init, stan_data, **kwargs):
    (stan_init, stan_data) = self.prepare_data(stan_init, stan_data)

    if 'inits' not in kwargs and 'init' in kwargs:
        kwargs['inits'] = self.prepare_data(kwargs['init'], stan_data)[0]
        del kwargs['init']  # add this 

and change the dict below

@staticmethod
def prepare_data(init, data) -> Tuple[dict, dict]:
    cmdstanpy_data = {
        'T': data['T'],
        'S': data['S'],
        'K': data['K'],
        'tau': data['tau'],
        'trend_indicator': data['trend_indicator'],
        'y': list(data['y']),
        't': list(data['t']),
        'cap': list(data['cap']),
        't_change': list(data['t_change']),
        's_a': list(data['s_a']),
        's_m': list(data['s_m']),
        'X': data['X'] if isinstance(data['X'], list) else list(data['X'].to_numpy()),
        'sigmas': data['sigmas']
    }

mihawk2016 avatar Dec 27 '22 10:12 mihawk2016

Hello, is there any progress in fixing the bug? I see that the PR has been approved?

dari-o avatar Jan 10 '23 16:01 dari-o

Thanks everyone for picking this up, such a silly bug :man_facepalming: The problem is in the fit() function: the kwargs['inits'] = self.prepare_data(...) line should be called before the (stan_init, stan_data) = self.prepare_data(...) line. Should be a quick fix.

tcuongd avatar Jan 11 '23 15:01 tcuongd