[Python] pandas 2.2.3: `astype(str)` returns `np.str_`

Discussão em 'Python' iniciado por Stack, Abril 2, 2025.

  1. Stack

    Stack Membro Participativo

    Latest pandas version casts types into np types. To cast a series of integer to strings I thought astype(str) would have been enough:

    import pandas as pd
    import numpy as np


    list_of_str = list(pd.Series([1234, 123, 345]).to_frame()[0].unique().astype(str))
    list_of_str


    returns [np.str_('1234'), np.str_('123'), np.str_('345')].

    And also

    list_of_str = list(pd.Series([1234, 123, 345]).to_frame()[0].unique().astype(np.str_))
    list_of_str


    returns [np.str_('1234'), np.str_('123'), np.str_('345')].

    Is there an efficient way to cast to python type string, that does not require list comprehension, like:

    list_of_str = [str(s) for s in list_of_str]
    list_of_str


    that finally returns ['1234', '123', '345'] ?

    Continue reading...

Compartilhe esta Página