votes up 4

cannot convert the series to (converter)

Package:
pandas
github stars 30911
Exception Class:
TypeError

Raise code

    """
    Install the scalar coercion methods.
    """

    def wrapper(self):
        if len(self) == 1:
            return converter(self.iloc[0])
        raise TypeError(f"cannot convert the series to {converter}")

    wrapper.__name__ = f"__{converter.__name__}__"
    return wrapper


# ----------------------------------------------------------------------
# Series class
😲 Agile task management is now easier than calling a taxi. #Tracklify

Ways to fix

votes up 2 votes down

Summary: Cannot convert Series type to scalar type like int, float, and long

Code to reproduce (WRONG):

import pandas as pd

df = mock_df = pd.DataFrame(data={
    'A': [1,2,3]
})

int(df['A']) #Try to convert Series to an integer will throw the exception

Working version (Fixed)

import pandas as pd

df = mock_df = pd.DataFrame(data={
    'A': [1,2,3]
})

int("1") # convert string to integration is ok
Jun 06, 2021 phoang88 answer
phoang88 615

Add a possible fix

Please authorize to post fix