Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pythonic way to detect specific pandas column type

Tags:

pandas

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.

like image 280
skibee Avatar asked Aug 11 '26 10:08

skibee


2 Answers

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')
like image 50
jezrael Avatar answered Aug 14 '26 12:08

jezrael


I would do:

from pandas.api.types import is_datetime64_dtype

ts = df.dtypes.apply(is_datetime64_dtype)
ts[ts].index
like image 32
hirolau Avatar answered Aug 14 '26 12:08

hirolau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!