Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在忽略空字符串时,将字符串转换为DataFrame中的Datetime对象_Python_Pandas_Dataframe_Datetime - Fatal编程技术网

Python 在忽略空字符串时,将字符串转换为DataFrame中的Datetime对象

Python 在忽略空字符串时,将字符串转换为DataFrame中的Datetime对象,python,pandas,dataframe,datetime,Python,Pandas,Dataframe,Datetime,我在数据框中有一列date,它要么是空的,要么是这种格式的字符串 '2021-06-04T15:14:30.512Z' 我想将其转换为Datetime对象,但不确定如何处理空列。我想保持它们为空或null,但转换其余的。 我试图使用它将字符串转换为日期时间 from dateutil import parser a = '2021-06-04T15:14:30.512Z' datetime.fromisoformat(a) 但无法在忽略空字符串的情况下跨dataframe列使用此选项。 有人能

我在数据框中有一列
date
,它要么是空的,要么是这种格式的字符串
'2021-06-04T15:14:30.512Z'
我想将其转换为Datetime对象,但不确定如何处理空列。我想保持它们为空或null,但转换其余的。 我试图使用它将字符串转换为日期时间

from dateutil import parser
a = '2021-06-04T15:14:30.512Z'
datetime.fromisoformat(a)
但无法在忽略空字符串的情况下跨dataframe列使用此选项。 有人能帮我吗? 谢谢

尝试使用
to\u datetime()
方法中的
errors='concurve'
参数:

df['date']=pd.to_datetime(df['date'],errors='coerce')
如果只需要日期部分:

df['date']=pd.to_datetime(df['date'],errors='coerce').dt.date
df['date']=pd.to_datetime(df['date'],errors='coerce').dt.date