pandas_select.column.HasDtype

class HasDtype(include=None, exclude=None)[source]

Select columns based on the column dtypes.

Parameters
  • include (scalar or list-like) – A selection of dtypes or strings to be included/excluded. At least one of these parameters must be supplied.

  • exclude (scalar or list-like) – A selection of dtypes or strings to be included/excluded. At least one of these parameters must be supplied.

Raises

ValueError – If both of include and exclude are empty; if include and exclude have overlapping elements; if any kind of string dtype is passed in.

Notes

  • To select all numeric types, use numpy.number, 'number' or :class:`AllNumeric.

  • To select strings you must use the object, string dtype if pandas version > 1.0.0, or or AllStr

  • See the numpy dtype hierarchy

  • To select datetimes, use numpy.datetime64, 'datetime' or 'datetime64'.

  • To select timedeltas, use numpy.timedelta64, 'timedelta' or 'timedelta64'.

  • To select Pandas categorical dtypes, use 'category' or AllCat.

  • To select Pandas datetimetz dtypes, use 'datetimetz' or 'datetime64[ns, tz]'.

Examples

>>> df = pd.DataFrame({"a": [1, 2],
...                    "b": [True, False],
...                    "c": [1.0, 2.0]})
>>> df
   a      b    c
0  1   True  1.0
1  2  False  2.0
>>> df[HasDtype("int")]
   a
0  1
1  2
>>> import numpy as np
>>> df[HasDtype(include=np.number, exclude=["int"])]
     c
0  1.0
1  2.0