简短的答案是IEEE 754指定
NaN为一个
float值。
至于您应该如何将转换
pd.Series为特定的数值数据类型,我更喜欢
pd.to_numeric在可能的地方使用。以下示例说明了原因。
import pandas as pdimport numpy as nps = pd.Series([1, 2.5, 3, 4, 5.5]) # s.dtype = float64s = s.astype(float) # s.dtype = float64s = pd.to_numeric(s, downcast='float') # s.dtype = float32t = pd.Series([1, np.nan, 3, 4, 5]) # s.dtype = float64t = t.astype(int) # ValueErrort = pd.to_numeric(t, downcast='integer') # s.dtype = float64u = pd.Series([1, 2, 3, 4, 5, 6]) # s.dtype = int64u = u.astype(int) # s.dtype = int32u = pd.to_numeric(u, downcast='integer') # s.dtype = int8



