Home > Enterprise >  pandas exclude dtypes float64, float32 and float16
pandas exclude dtypes float64, float32 and float16

Time:01-05

right now I am using df.select_dtypes(exclude=["float"]) to exclude all "float" columns, but it does not detect e.g a column with dtype float16. I assume that "float" represents the np.float64.

Is there a way to catch all floats like float64, float32 and float16?

CodePudding user response:

You can use np.floating:

>>> df.dtypes
a    float16
b    float32
c    float64
d     object
e      int64
dtype: object

>>> df.select_dtypes(exclude=np.floating).dtypes
d    object
e     int64
dtype: object

CodePudding user response:

IIUC, try this MVCE:

df = pd.DataFrame(np.random.randn(5,4), columns=['f16', 'f32', 'f64', 'f'])

df['f16'] = df['f16'].astype('float16')
   
df['f32'] = df['f32'].astype('float32')

df['f64'] = df['f64'].astype('float64')

df['f'] = df['f'].astype('float')

df.info()

df.select_dtypes(exclude='float')

Output:

        f16
0 -0.447021
1  0.468506
2 -0.031860
3  0.540039
4  2.355469

Yep, "float" doesn't check for float16.

df.select_dtypes(exclude=['float', 'float16'])

Output:

Empty DataFrame
Columns: []
Index: [0, 1, 2, 3, 4]

Explicitly defined, "float16".

  • Related