我有一个df
doc_date date_string
2019-06-03 WW0306
2019-06-07 EH0706
2019-08-08 19685
2019-08-09 258
2019-08-10 441573556
doc_date
为dateimte64
数据类型,date_string
为string
,删除非数字字符
s = df['date_string'].str.replace(r'\D+', '')
doc_date date_string
2019-06-03 0306
2019-06-07 0706
2019-08-08 19685
2019-08-09 258
2019-08-10 441573556
s1 = to_datetime(s, errors='ignore', format='%d%m')
doc_date date_string
2019-06-03 1900-06-03
2019-06-07 1900-06-07
2019-08-08 19685
2019-08-09 258
2019-08-10 441573556
这里我想知道如何忽略那些date_string
不能转换为datetime的行;所以我可以创建一个布尔掩码
c1 = (df.doc_date.dt.dayofyear - s1.dt.dayofyear).abs().le(180)
另一件事是如何获得c1
与s
相同的长度,任何date_string
不能转换成datetime
的False
在c1
使用
errors='coerce'
将不匹配的模式值转换为NaT
工作日期时间型函数:或更常用(0.24.2,所以输出不同):
总之:
相关问题 更多 >
编程相关推荐