AssertionError: Shape of new values must be compatible with manager shape

Viewed 1317

Error with pandas apply I have a pandas data frame with following columns

col = ["File_Path", "Function_Body", "Prediction", "Line_Number"]

I am applying get_prediction() function on column Function body and it returns three values

  1. List (Prediction): Ex. [1,1,0,0,0]
  2. List (Confidence): Ex. [64.000, 88.000,0,0,0]
  3. List of List (Top 5 Tokens with line Number): [['int', 5], ['ret', 6],['char', 5],['sum', 4],['i', 2]]

Following piece of code, runs fine for one dataset but gives subjected error for other dataset.

final_df["Prediction"],  final_df["Confidence"], final_df["Tokens"] = zip(*final_df["Function_Body"].apply(lambda x:get_prediction(x)))

Error:

  File "nginx_fast.py", line 404, in <module>
    final_df["Prediction"],  final_df["Confidence"], final_df["Tokens"] = zip(*final_df["Function_Body"].apply(lambda x:get_prediction(x)))
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py", line 2938, in __setitem__
    self._set_item(key, value)
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py", line 3001, in _set_item
    NDFrame._set_item(self, key, value)
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py", line 3624, in _set_item
    self._data.set(key, value)
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py", line 1067, in set
    "Shape of new values must be compatible with manager shape"
AssertionError: Shape of new values must be compatible with manager shape

I checked the shape and type of value being returned before return statement, it looks fine to me.

0 Answers
Related