简体   繁体   中英

Pandas - Check if any column is date time and change it to date format string (yyyy-mm-dd)

How can we use pandas to check, if any column is a datetime datatype column and then convert only that column to a date format string (yyyy-mm-dd) datatype column.

I have multiple columns that are datetime. So, I cannot go by column name and change it. But would rather prefer a way that checks and then changes it.

Kindly help me with this.

You can check like below with df.dtypes :

>>> df
   PERSON ID MOVING DATE           PLACE
0          1  2018-01-01        New York
1          1  2018-01-20  Rio de Janeiro
2          1  2018-02-13          London
3          2  2017-06-12          Seatle
4          2  2016-10-10      New Mexico
5          3  2017-09-19       Sao Paulo
6          3  2015-12-11      Bangladesh


>>> df.dtypes
PERSON ID               int64
MOVING DATE    datetime64[ns]
PLACE                  object
dtype: object

Or in particular if you want to see which columns are datetime then use numpy as follows. SO, numpy gives you a detailed selection process..

>>> df.select_dtypes(include=[np.datetime64])
  MOVING DATE
0  2018-01-01
1  2018-01-20
2  2018-02-13
3  2017-06-12
4  2016-10-10
5  2017-09-19
6  2015-12-11

You can do same to determine if the columns having numbers

>>> df.select_dtypes(include=[np.number])
   PERSON ID
0          1
1          1
2          1
3          2
4          2
5          3
6          3

another to determine if the columns having object type:

>>> df.select_dtypes(include=[np.object])
            PLACE
0        New York
1  Rio de Janeiro
2          London
3          Seatle
4      New Mexico
5       Sao Paulo
6      Bangladesh
df = pd.DataFrame(data={'date':(['2018-12-08 00:00:00','2018-12-08 00:10:00','2018-12-08 01:10:00']),'B':[5,4,3],'C':[4,3,2]})

>>df

    date                B   C
0   2018-12-08 00:00:00 5   4
1   2018-12-08 00:10:00 4   3
2   2018-12-08 01:10:00 3   2

>>df.dtypes

date     object
B        int64
C        int64

>>df[df.select_dtypes(['object']).columns]=df[df.select_dtypes(['object']).columns].apply(pd.to_datetime)

Post this you can call dt.date on the series like:

>>df['date'].dt.date
0    2018-12-08
1    2018-12-08
2    2018-12-08

Or for selecting multiple columns(Note, the above might fail if you have any other object columns not resembling a date , in that case use like below)

df[['col1','col2']] = df[['col1','col2']].apply(pd.to_datetime)

From the docs: unuder select_dtypes

To select datetimes, use np.datetime64, 'datetime' or 'datetime64' To select timedeltas, use np.timedelta64, 'timedelta' or 'timedelta64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM