'TypeError: cannot convert the series to <class 'int'> python

In this function I'm trying to convert a series object to an int object. But when I try it I keep geting this error message TypeError: cannot convert the series to <class 'int'>. I have already tried to use .astype(int), but I had the same result. Can someone help me?

Ps : When I execute the function below outside the "sla()" function, it works fine

This is the function which converts the series type to an int type:

def current_user_section(df,ID):    
    current_user = df.loc[df["ID"] == ID]          
    section = int(current_user["SECTION"])
    return section

Input/Output:

x = current_user_section(users_df,"5DBEF04B")
print(x)
113

Function where "current_user_section" is called:

def sla(path):
  file_with_extension = os.path.split(path)[1] # separa o file path em head e tail, armazenando apenas o tail na variavel
  file_name = os.path.splitext(file_with_extension)[0] # retira a extensao/tipo do arquivo do tail, deixando apenas o nome do arquivo
  print("Parabens, voce concluiu o tutorial ",file_name,"\n")  
  section = current_user_data(users_df,ID)[1]
  videos = videos_to_watch(section,ID)
  if is_list_empty(videos,section,ID) == True: # se tiver vazia vai pedir o ID de novo
      print("Nao ha novos tutoriais disponiveis.")
      return None,None
  else:      
    print("Deseja continuar assistir a mais um tutorial?\n")
    c_s = (input("Digite (s) para sim e (n) para nao: ")).lower()
    return c_s,videos

Output:

Traceback (most recent call last):
  File "d:\Users\raulc\Documents\AMBIENTES\run_videos.py", line 105, in <module>
    c_s = sla(path)[0]
  File "d:\Users\raulc\Documents\AMBIENTES\run_videos.py", line 71, in sla
    section = current_user_section(users_df,ID)
  File "d:\Users\raulc\Documents\AMBIENTES\mercedes.py", line 131, in current_user_section
    section = int(current_user["SECTION"])
  File "D:\Users\raulc\Documents\AMBIENTES\raulenv\lib\site-packages\pandas\core\series.py", line 191, in wrapper
    raise TypeError(f"cannot convert the series to {converter}")
TypeError: cannot convert the series to <class 'int'>


Solution 1:[1]

If you want to convert a Series to int, use:

def current_user_section(df,ID):
    current_user = df.loc[df["ID"] == ID]
    section = current_user["SECTION"].astype(int)
    return section

But maybe you want:

def current_user_section(df,ID):
    current_user = df.loc[df["ID"] == ID]
    section = int(current_user["SECTION"].squeeze())
    return section

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Corralien