Pandas - 检查是否有任何列是日期时间并将其更改为日期格式字符串 (yyyy-mm-dd)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54247148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas - Check if any column is date time and change it to date format string (yyyy-mm-dd)
提问by MMS
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.
我们如何使用 Pandas 来检查是否有任何列是日期时间数据类型列,然后仅将该列转换为日期格式字符串 (yyyy-mm-dd) 数据类型列。
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.
请帮我解决这个问题。
回答by anky
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:
发布此内容,您可以调用dt.date
该系列,例如:
>>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
来自文档: unuder select_dtypes
To select datetimes, use np.datetime64, 'datetime' or 'datetime64' To select timedeltas, use np.timedelta64, 'timedelta' or 'timedelta64
要选择日期时间,请使用 np.datetime64、'datetime' 或 'datetime64' 要选择 timedeltas,请使用 np.timedelta64、'timedelta' 或 'timedelta64'
回答by Karn Kumar
You can check like below with df.dtypes
:
你可以像下面这样检查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..
或者特别是如果您想查看哪些列是日期时间,请按如下方式使用 numpy。所以,numpy 给你一个详细的选择过程..
>>> 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