Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all columns with datetime type using pandas?

Tags:

python

pandas

I have a huge DataFrame where the columns aren't ever in order nor do I know their name.

What do I do to find all the columns which are datetime types?

Most of the solutions online, the poster knows the name of the column so I am having a bit trouble as I do not. What can I do in this situation?

like image 776
J. Doe Avatar asked Feb 19 '17 02:02

J. Doe


1 Answers

You can use pandas.DataFrame.select_dtypes(), and include only the datetime64 type.

df.select_dtypes(include=['datetime64'])

Demo

>>> df
         dts1       dts2  ints
0  2012-01-01 2004-01-01     0
1  2012-01-02 2004-01-02     1
2  2012-01-03 2004-01-03     2
..        ...        ...   ...
97 2012-04-07 2004-04-07    97
98 2012-04-08 2004-04-08    98
99 2012-04-09 2004-04-09    99

>>> df.select_dtypes(include=['datetime64'])
         dts1       dts2
0  2012-01-01 2004-01-01
1  2012-01-02 2004-01-02
2  2012-01-03 2004-01-03
..        ...        ...
97 2012-04-07 2004-04-07
98 2012-04-08 2004-04-08
99 2012-04-09 2004-04-09
like image 149
miradulo Avatar answered Sep 26 '22 08:09

miradulo