pandas 验证日期格式 [英] Pandas validate date format

查看:72
本文介绍了 pandas 验证日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有什么好方法可以验证数据框的列中的所有项目均具有有效的日期格式?

Is there any nice way to validate that all items in a dataframe's column have a valid date format?

我的日期格式为 2010年8月11日.

我看到了这个通用答案,其中:

I saw this generic answer, where:

try:
    datetime.datetime.strptime(date_text, '%Y-%m-%d')
except ValueError:
    raise ValueError("Incorrect data format, should be YYYY-MM-DD")

来源: https://stackoverflow.com/a/16870699/1374488

但是我认为这对我来说不是很好(有效).

But I assume that's not good (efficient) in my case.

我假设我必须首先将字符串修改为大熊猫日期,如下所述:将字符串日期时间转换为熊猫日期时间

I assume I have to modify the strings to be pandas dates first as mentioned here: Convert string date time to pandas datetime

我是Python世界的新手,任何想法都值得赞赏.

I am new to the Python world, any ideas appreciated.

推荐答案

(从piRSquared的答案中借用的格式)

(format borrowed from piRSquared's answer)

if pd.to_datetime(df['date'], format='%d-%b-%Y', errors='coerce').notnull().all():
    # do something 

这是LYBL —突如其来"的方法.假设所有日期字符串均有效,这将返回 True -这意味着它们都将转换为实际的 pd.Timestamp 对象.无效的日期字符串被强制为 NaT ,该日期时间等效于 NaN .

This is the LYBL—"Look Before You Leap" approach. This will return True assuming all your date strings are valid - meaning they are all converted into actual pd.Timestamp objects. Invalid date strings are coerced to NaT, which is the datetime equivalent of NaN.

或者,

try:
    pd.to_datetime(df['date'], format='%d-%b-%Y', errors='raise')
    # do something
except ValueError:
    pass

这是EAFP —比权限更容易请求宽恕"的方法,当遇到无效的日期字符串时,会引发 ValueError .

This is the EAFP—"Easier to Ask Forgiveness than Permission" approach, a ValueError is raised when invalid date strings are encountered.

这篇关于 pandas 验证日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆