为什么将NaN视为浮动货币? [英] Why is NaN considered as a float?

查看:63
本文介绍了为什么将NaN视为浮动货币?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pandas中,当我们尝试将包含NaN值的系列转换为带有如下代码段的整数

In pandas when we are trying to cast a series which contains NaN values to integer with a snippet such as below

df.A = df.A.apply(int),我经常看到错误消息

df.A = df.A.apply(int) , i often see an error message

ValueError: cannot convert float NaN to integer

我知道NaN的值不能转换为整数.但是我对在这种情况下抛出的ValueError很好奇.它说 float NaN无法转换为整数.

I understand that NaN values can't be converted to integer. But i am curious about the ValueError thrown in this case. it says float NaN can't be converted to integer.

NaN值视为浮点对象有什么特定的原因吗?还是这种情况与显示的错误消息有关?

Is there any specific reason why NaN values are treated as float objects? or is this the case of some issue with the error messages displayed?

推荐答案

简短的答案是 IEEE 754 NaN指定为float值.

The short answer is IEEE 754 specifies NaN as a float value.

关于将pd.Series转换为特定数字数据类型应采取的措施,我更喜欢使用

As for what you should do about converting a pd.Series to specific numeric data types, I prefer to use pd.to_numeric where possible. The below examples demonstrate why.

import pandas as pd
import numpy as np

s = pd.Series([1, 2.5, 3, 4, 5.5])        # s.dtype = float64
s = s.astype(float)                       # s.dtype = float64
s = pd.to_numeric(s, downcast='float')    # s.dtype = float32

t = pd.Series([1, np.nan, 3, 4, 5])       # s.dtype = float64
t = t.astype(int)                         # ValueError
t = pd.to_numeric(t, downcast='integer')  # s.dtype = float64

u = pd.Series([1, 2, 3, 4, 5, 6])         # s.dtype = int64
u = u.astype(int)                         # s.dtype = int32
u = pd.to_numeric(u, downcast='integer')  # s.dtype = int8

这篇关于为什么将NaN视为浮动货币?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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