Skip to content Skip to sidebar Skip to footer

Pd.DataFrame.select_dtypes() Inculdes Timedelta Dtype

Why is it expected behavior that this test code: test = pd.DataFrame({'bool' :[False, True], 'int':[-1,2], 'float': [-2.5, 3.4], 'compl':np.array([1-1j, 5]),

Solution 1:

Because that's how it has been implemented:

np.issubdtype(np.timedelta64, np.number)
# True

More specifically,

np.issubdtype(np.timedelta64, np.integer)
# True

timedelta and datetime dtypes in numpy are internally represented by integer. This makes it easy to represent in memory, and makes arithmetic on datetimes fast.

If you want to exclude these types from your checks, you can specify an exclude argument:

test.select_dtypes(include=['number'], exclude=['datetime', 'timedelta'])

   int  float   compl
0   -1   -2.5  (1-1j)
1    2    3.4  (5+0j)

Solution 2:

Since numpy.timedelta is belong to numpy.number, if you only want the number numeric columns return

num= ['int16', 'int32', 'int64', 'float16', 'float32', 'float64','complex128']
test.select_dtypes(include=num)
Out[715]: 
    compl  float  int
0  (1-1j)   -2.5   -1
1  (5+0j)    3.4    2

Post a Comment for "Pd.DataFrame.select_dtypes() Inculdes Timedelta Dtype"