pythonpandastypesseriesdtype

More detailed info about data type that is common throughout a Series?


I have a dataframe column consisting entirely of a common type dict. Is there any way to query the Series type to reveal the common data type? It currently only tells me that it is an object, which I understand is an array of references. But if the things being referenced are of the same type for an entire Series, it would be useful to know this fact, as well as the specific common type.

>>> df = pd.DataFrame([[{'c':[1,2]}],[{'d':[3,4]}]],columns=['A'])
                  A
   0  {'c': [1, 2]}
   1  {'d': [3, 4]}

>>> df['A'].dtype
   dtype('O')

>>> type(df['A'])
   pandas.core.series.Series

Solution

  • An object Series could contain more than one python type. If you want to test this you could map type, then check if the output is unique/nunique:

    df['A'].map(type).unique()
    # array([<class 'dict'>], dtype=object)
    
    df['A'].map(type).nunique()
    # 1
    

    If you know that the type is homogeneous, then just test the first item:

    type(df['A'].iloc[0])
    # dict