[Python] Python environment error, "TypeError: Choicelist and default value do not have a...

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have two python environments - one online through a class, and the other on my own computer.

    The following code works on the online environment, but gives an error on my local environment. Does anyone know what this error means, and have any suggestions for fixing my environment, or fixing my code? The online environment has a habit of losing my work, so I'd like to get this working on my own machine.

    This is the code

    custom_categories = ['cat_a', 'cat_b', 'cat_c', 'Other']
    custom_categories_filter = [
    (df['column_name'].str.contains('(A)', regex = False)),
    (df['column_name'].str.contains('(B)', regex = False)),
    (df['column_name'].str.contains('(C)', regex = False)),
    (df['column_name'].str.contains('(A)', regex = False) == False)
    & (df['column_name'].str.contains('(B)', regex = False) == False)
    & (df['column_name'].str.contains('(C)', regex = False) == False)
    ]
    df["custom_category"] = numpy.select(custom_categories_filter, custom_categories)


    It's intended to look through a column of a pandas data frame, search for certain terms in brackets, then put a value based on that term into a new column.

    This is the error:

    TypeError: Choicelist and default value do not have a common dtype:
    The DType <class 'numpy.dtypes._PyLongDType'> could not be promoted by <class 'numpy.dtypes.StrDType'>.
    This means that no common DType exists for the given inputs.
    For example they cannot be stored in a single array unless the dtype is `object`.
    The full list of DTypes is: (<class 'numpy.dtypes.StrDType'>, <class 'numpy.dtypes.StrDType'>, <class 'numpy.dtypes.StrDType'>, <class 'numpy.dtypes.StrDType'>, <class 'numpy.dtypes.StrDType'>, <class 'numpy.dtypes._PyLongDType'>)


    Online environment is Python 3.11.11, local is 3.10.8 - could it be the python version I'm using?

    Continue reading...

Compartilhe esta Página