[Python] Pandas distinction between str and object types

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

  1. Stack

    Stack Membro Participativo

    Numpy seems to make a distinction between str and object types. For instance I can do:

    >>> import pandas as pd
    >>> import numpy as np
    >>> np.dtype(str)
    dtype('S')
    >>> np.dtype(object)
    dtype('O')


    Where dtype('S') and dtype('O') corresponds to str and object respectively.

    However pandas seem to lack that distinction and coerce str to object.

    >>> df = pd.DataFrame({'a': np.arange(5)})
    >>> df.a.dtype
    dtype('int64')
    >>> df.a.astype(str).dtype
    dtype('O')
    >>> df.a.astype(object).dtype
    dtype('O')


    Forcing the type to dtype('S') does not help either.

    >>> df.a.astype(np.dtype(str)).dtype
    dtype('O')
    >>> df.a.astype(np.dtype('S')).dtype
    dtype('O')


    Is there any explanation for this behavior?

    Continue reading...

Compartilhe esta Página