what is the most pythonic way to select columns based on there dtype? (only needing the columns and not the entire df as with select_dtypes ) .
assuming we are looking for datetime columns, currently we are using :
date_cols = [col for col in df.columns if "datetime" in str(df[col].dtype]
another option is
date_cols = [col for col in df.columns if isinstance(df[col].dtype, pd.core.dtypes.dtypes.XXXX)]
but could not find the correct dtype for dtype('<M8[ns]').
np.datetime64 does not detect it either.
Use pandas.api.types.is_datetime64_dtype, for list of all possible functions check general_utility_functions:
rng = pd.date_range('2017-04-03', periods=10)
df = pd.DataFrame({'Date': rng, 'a': range(10)})
print (df)
date_cols = [col for col in df.columns if pd.api.types.is_datetime64_dtype(df[col])]
print (date_cols)
['Date']
Simplier is DataFrame.select_dtypes with columns:
date_cols = df.select_dtypes('datetime64').columns
print (date_cols)
Index(['Date'], dtype='object')
Or:
date_cols = df.columns[df.dtypes.map(pd.api.types.is_datetime64_dtype)]
print (date_cols)
Index(['Date'], dtype='object')
I would do:
from pandas.api.types import is_datetime64_dtype
ts = df.dtypes.apply(is_datetime64_dtype)
ts[ts].index
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With