将具有混合字符串的列转换为数据帧

2024-10-01 07:45:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据框列,其值如下

$none
1558044313727
$none
1558058614585
...

我尝试了下面的查询,事件带有errors='ignore'它没有转换

df['epoch_date'] = pd.to_datetime(df['epoch_date'], unit='ms')
ERROR: ValueError: non convertible value $none with the unit 'ms'

如何忽略$none或将其转换为NaN值,并使用datetime值进行休息


Tags: to数据nonedfdatetimedate事件unit
1条回答
网友
1楼 · 发布于 2024-10-01 07:45:38

尝试使用errors = 'coerce'

df['epoch_date'] = pd.to_datetime(df['epoch_date'], unit='ms', errors = 'coerce')

这将把'$none'值(以及任何其他无效解析)转换为NaT,这是datetime64[ns]类型的NaN等价值

errors = 'ignore'失败,因为它只是在无效解析时返回输入,所以最后它试图将字符串放入datetime对象,但由于明显的原因失败了

相关问题 更多 >