pythonpandasdataframetypeerror

TypeError: unhashable type: 'set' while renameing the DF in pandas version 2.1.4


i have a huge dataframe now i want to rename one based on the values inside of the other one, if the columns match

for index,col in enumerate(news1.columns):
    for index_dic,col_dic in enumerate(dic.columns):
        if col==col_dic:
            print(col,dic.iloc[1, index_dic])
            print(type(col))
            print(type(dic.iloc[1, index_dic]))
            news1.rename(columns={f'{col}':f'{dic.iloc[1, index_dic]}'},inplace=True)

traceback of the error:

----> 1 news1.rename(columns={'c15.10':'WA_ambiguous-expectation'})

File c:\Users\\anaconda3\envs\PythonCourse2023\Lib\site-packages\pandas\core\frame.py:5518, in DataFrame.rename(self, mapper, index, columns, axis, copy, inplace, level, errors)
   5399 def rename(
   5400     self,
   5401     mapper: Renamer | None = None,
   (...)
   5409     errors: IgnoreRaise = "ignore",
   5410 ) -> DataFrame | None:
   5411     """
   5412     Rename columns or index labels.
   5413 
   (...)
   5516     4  3  6
   5517     """
-> 5518     return super()._rename(
   5519         mapper=mapper,
   5520         index=index,
   5521         columns=columns,
   5522         axis=axis,
   5523         copy=copy,
   5524         inplace=inplace,
   5525         level=level,
   5526         errors=errors,
   5527     )

File c:\Users\\anaconda3\envs\PythonCourse2023\Lib\site-packages\pandas\core\generic.py:1086, in NDFrame._rename(self, mapper, index, columns, axis, copy, inplace, level, errors)
   1079         missing_labels = [
   1080             label
   1081             for index, label in enumerate(replacements)
   1082             if indexer[index] == -1
   1083         ]
   1084         raise KeyError(f"{missing_labels} not found in axis")
-> 1086 new_index = ax._transform_index(f, level=level)
   1087 result._set_axis_nocheck(new_index, axis=axis_no, inplace=True, copy=False)
   1088 result._clear_item_cache()

File c:\Users\\anaconda3\envs\PythonCourse2023\Lib\site-packages\pandas\core\indexes\base.py:6465, in Index._transform_index(self, func, level)
   6463     return type(self).from_arrays(values)
   6464 else:
-> 6465     items = [func(x) for x in self]
   6466     return Index(items, name=self.name, tupleize_cols=False)

File c:\Users\\anaconda3\envs\PythonCourse2023\Lib\site-packages\pandas\core\indexes\base.py:6465, in <listcomp>(.0)
   6463     return type(self).from_arrays(values)
   6464 else:
-> 6465     items = [func(x) for x in self]
   6466     return Index(items, name=self.name, tupleize_cols=False)

File c:\Users\anaconda3\envs\PythonCourse2023\Lib\site-packages\pandas\core\common.py:507, in get_rename_function.<locals>.f(x)
    506 def f(x):
--> 507     if x in mapper:
    508         return mapper[x]
    509     else:

the result of the prints are:

c15.10 WA_ambiguous-expectation
<class 'str'>
<class 'str'>

i can't figure out why i get the error. would appreciate any help


Solution

  • there was a bug in the pandas version 2.1.4 which caused the issue, I updated the pandas to version 2.2.0 and it fixed the problem