问题描述
我在df中有一个日期列.现在,我想通过逐列检查它是否与特定格式匹配.我是逐行完成的,但是要花很多时间才能运行.我想知道是否有一种方法可以立即检查列,该列可能包含空值或日期的其他格式.尽管我尝试按列进行操作(如果有一条不匹配的记录),但会进入例外.那么,我们该怎么做呢?
I have a date column in a df. Now I want to check if it is matching with a particular format or not by doing column by column. I did it row by row but it takes a lot of time to run. I want to know if there is a way for checking the column at once the column might contain nulls or a different format of a date. Although I tried doing it column-wise if there is one record that is not matching it goes into except. So, how can we do that?
代码:
df=
Date
0 12-22-2020
1 22-12-2020
3 22122020
4
5 02-22-2020
formatt='%m-%d-%Y'
try:
datetime = dt.strptime(str(df['Date']), formatt)
print(datetime )
except ValueError:
print('error')
这给出:'错误'
总体上是给予.但我希望显示接受的记录.我们该怎么做?
It's giving as a whole. But I want the accepted records to be displayed. How do we do it?
预期输出:
datetime =
Date
0 12-22-2020
4
5 02-22-2020
推荐答案
您应该将熊猫 to_datetime
函数与 errors ='coerce'
一起使用,并保持正确的行转换和行在初始列中为空.
You should use pandas to_datetime
function with errors='coerce'
and keep lines correctly converted and lines that were null in the initial column.
代码可能是
dates = pd.to_datetime(df['Date'], errors='coerce', format='%m-%d-%Y')
dates = dates[(~dates.isna())|df['Date'].isnull()]
它给出:
0 2020-12-22
4 NaT
5 2020-02-22
这篇关于有什么直接方法可以在python中逐列检查日期格式(使用datetime)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!