Hi everyone, I am facing this error while performi...
# neural-forecast
s
Hi everyone, I am facing this error while performing cross validation:
Copy code
model = NeuralForecast(
                [AutoNBEATS(**model_args)],
                freq=new_freq,
            )
model.fit(df=data, val_size=horizon)
crossvalidation_df = model.cross_validation()

...
TypeError: Can only merge Series or DataFrame objects, a <class 'NoneType'> was passed
The model is being tuned using
optuna
. Thanks in advance for any help regarding this!
j
Can you provide a bit more of the stacktrace? This isn't enough to find the error
s
Thanks for your response @José Morales
Here's the stacktrace:
Copy code
TypeError                                 Traceback (most recent call last) 
Cell In[1], line 1                                                         
----> 1 crossvalidation_df = model.cross_validation()                                                                                                 
                                                                           
File /home/$USER/neuralforecast/neuralforecast/core.py:542, in NeuralForecast.cross_validation(self, df, static_df, n_windows, step_size, val_size,
 test_size, sort_df, use_init_models, verbose, **data_kwargs)                                                                                         
    539 fcsts_df = pd.concat([fcsts_df, fcsts], axis=1)
    541 # Add original input df's y to forecasts DataFrame
--> 542 fcsts_df = fcsts_df.merge(df, how="left", on=["unique_id", "ds"])
    543 return fcsts_df                                                                                                                               
                                     
File ~/anaconda3/envs/$CONDA_ENV/lib/python3.10/site-packages/pandas/core/frame.py:9843, in DataFrame.merge(self, right, how, on, left_on, right_on, left_ind
ex, right_index, sort, suffixes, copy, indicator, validate)                                                                                           
   9824 @Substitution("")
   9825 @Appender(_merge_doc, indents=2)
   9826 def merge(              
   (...)
   9839     validate: str | None = None,
   9840 ) -> DataFrame:
   9841     from pandas.core.reshape.merge import merge
-> 9843     return merge(
   9844         self,
   9845         right,
   9846         how=how,
   9847         on=on,
   9848         left_on=left_on,
   9849         right_on=right_on,
   9850         left_index=left_index,
   9851         right_index=right_index,
   9852         sort=sort,
   9853         suffixes=suffixes,
   9854         copy=copy,
   9855         indicator=indicator,
   9856         validate=validate,
   9857     )

File ~/anaconda3/envs/$CONDA_ENV/lib/python3.10/site-packages/pandas/core/reshape/merge.py:148, in merge(left, right, how, on, left_on, right_on, le[12/1635]
 right_index, sort, suffixes, copy, indicator, validate)
    131 @Substitution("\nleft : DataFrame or named Series")
    132 @Appender(_merge_doc, indents=0)
    133 def merge(
   (...)
    146     validate: str | None = None,
    147 ) -> DataFrame:
--> 148     op = _MergeOperation(
    149         left,
    150         right,
    151         how=how,
    152         on=on,
    153         left_on=left_on,
    154         right_on=right_on,
    155         left_index=left_index,
    156         right_index=right_index,
    157         sort=sort,
    158         suffixes=suffixes,
    159         indicator=indicator,
    160         validate=validate,
    161     )
    162     return op.get_result(copy=copy)

File ~/anaconda3/envs/$CONDA_ENV/lib/python3.10/site-packages/pandas/core/reshape/merge.py:681, in _MergeOperation.__init__(self, left, right, how, on, left_
on, right_on, axis, left_index, right_index, sort, suffixes, indicator, validate)
    664 def __init__(
    665     self,
    666     left: DataFrame | Series,
   (...)
    678     validate: str | None = None,
    679 ) -> None:
    680     _left = _validate_operand(left)
--> 681     _right = _validate_operand(right)
    682     self.left = self.orig_left = _left
    683     self.right = self.orig_right = _right

File ~/anaconda3/envs/$CONDA_ENV/lib/python3.10/site-packages/pandas/core/reshape/merge.py:2575, in _validate_operand(obj)
   2573     return obj.to_frame()
   2574 else:
-> 2575     raise TypeError(
   2576         f"Can only merge Series or DataFrame objects, a {type(obj)} was passed"
   2577     )

TypeError: Can only merge Series or DataFrame objects, a <class 'NoneType'> was passed
j
Ah yeah, looks like it's a bug. Can you provide the df to the cross_validation again? e.g.
crossvalidation_df = model.cross_validation(df=df)
. I've opened https://github.com/Nixtla/neuralforecast/issues/800 to fix it
s
Sure, thanks! I can help with a PR.
This fix works for me, but I keep getting the
y
column as
nan
. How do I fix it?
Is it not necessary to avoid the
val_size
columns while training in the cross validation function? I believe the
nan
occurs because it is expecting
y
outside the training dataset.
Seems like the issue of
nan
is popping up when the frequency inference is incorrect. Naturally, the merge with the original
df
isn't happening properly due to mismatched timestamps.
j
The frequency isn't inferred as far as I remember, it should use the one you provide in the constructor
1