PacktPublishing / Modern-Time-Series-Forecasting-with-Python

Modern Time Series Forecasting with Python, published by Packt
MIT License
378 stars 200 forks source link

Chapter 08 : 01-Forecasting with ML #27

Open JOHNPAUL-ADIMS opened 11 months ago

JOHNPAUL-ADIMS commented 11 months ago
sample_train_df = train_df.loc[train_df.LCLid == "MAC000193", :]
sample_test_df = test_df.loc[test_df.LCLid == "MAC000193", :]

# display(sample_train_df)
train_features, train_target, train_original_target = feat_config.get_X_y(sample_train_df, categorical=False, exogenous=False
)
# Loading the Validation as test
test_features, test_target, test_original_target = feat_config.get_X_y(
    sample_test_df, categorical=False, exogenous=False
)
del sample_train_df, sample_test_df

I continue to get this error message.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[14], line 5
      2 sample_test_df = test_df.loc[test_df.LCLid == "MAC000193", :]
      4 # display(sample_train_df)
----> 5 train_features, train_target, train_original_target = feat_config.get_X_y(sample_train_df, categorical=False, exogenous=False
      6 )
      7 # # Loading the Validation as test
      8 # test_features, test_target, test_original_target = feat_config.get_X_y(
      9 #     sample_test_df, categorical=False, exogenous=False
     10 # )
     11 # del sample_train_df, sample_test_df

File ~\OneDrive - Florida A&M University\COURSES\Online\Modern-Time-Series-Forecasting-with-Python-main\src\forecasting\ml_forecasting.py:153, in FeatureConfig.get_X_y(self, df, categorical, exogenous)
    150 feature_list = list(set(feature_list))
    151 delete_index_cols = list(set(self.index_cols) - set(self.feature_list))
    152 (X, y, y_orig) = (
--> 153     df.loc[:, set(feature_list + self.index_cols)]
    154     .set_index(self.index_cols, drop=False)
    155     .drop(columns=delete_index_cols),
    156     df.loc[:, [self.target] + self.index_cols].set_index(
    157         self.index_cols, drop=True
    158     )
    159     if self.target in df.columns
    160     else None,
    161     df.loc[:, [self.original_target] + self.index_cols].set_index(
    162         self.index_cols, drop=True
    163     )
    164     if self.original_target in df.columns
    165     else None,
    166 )
    167 return X, y, y_orig

File ~\anaconda3\envs\modern_ts\lib\site-packages\pandas\core\indexing.py:1091, in _LocationIndexer.__getitem__(self, key)
   1089 @final
   1090 def __getitem__(self, key):
-> 1091     check_dict_or_set_indexers(key)
   1092     if type(key) is tuple:
   1093         key = tuple(list(x) if is_iterator(x) else x for x in key)

File ~\anaconda3\envs\modern_ts\lib\site-packages\pandas\core\indexing.py:2618, in check_dict_or_set_indexers(key)
   2610 """
   2611 Check if the indexer is or contains a dict or set, which is no longer allowed.
   2612 """
   2613 if (
   2614     isinstance(key, set)
   2615     or isinstance(key, tuple)
   2616     and any(isinstance(x, set) for x in key)
   2617 ):
-> 2618     raise TypeError(
   2619         "Passing a set as an indexer is not supported. Use a list instead."
   2620     )
   2622 if (
   2623     isinstance(key, dict)
   2624     or isinstance(key, tuple)
   2625     and any(isinstance(x, dict) for x in key)
   2626 ):
   2627     raise TypeError(
   2628         "Passing a dict as an indexer is not supported. Use a list instead."
   2629     )

TypeError: Passing a set as an indexer is not supported. Use a list instead.
manujosephv commented 10 months ago

May be a pandas version issue.. as an easy fix you can just wrap line #153 in ml_forecasting.py:153, in FeatureConfig.get_X_y(self, df, categorical, exogenous) with a list

df.loc[:, set(feature_list + self.index_cols)]

to

df.loc[:, list(set(feature_list + self.index_cols))]
taimoorkh commented 1 month ago
  1. go to src\forecasting\ml_forcasting.py
  2. change the code line # 153 as mentioned by @manujosephv
  3. df.loc[:, set(feature_list + self.index_cols)] to df.loc[:, list(set(feature_list + self.index_cols))]

It will work fine.