Pandas: ValueError: cannot convert float NaN to integer

后端 未结 5 810
攒了一身酷
攒了一身酷 2020-12-03 00:49

I get ValueError: cannot convert float NaN to integer for following:

df = pandas.read_csv(\'zoom11.csv\')
df[[\'x\']] = df[[\'x\']].astype(i         


        
5条回答
  •  猫巷女王i
    2020-12-03 01:34

    ValueError: cannot convert float NaN to integer

    From v0.24, you actually can. Pandas introduces Nullable Integer Data Types which allows integers to coexist with NaNs.

    Given a series of whole float numbers with missing data,

    s = pd.Series([1.0, 2.0, np.nan, 4.0])
    s
    
    0    1.0
    1    2.0
    2    NaN
    3    4.0
    dtype: float64
    
    s.dtype
    # dtype('float64')
    

    You can convert it to a nullable int type (choose from one of Int16, Int32, or Int64) with,

    s2 = s.astype('Int32') # note the 'I' is uppercase
    s2
    
    0      1
    1      2
    2    NaN
    3      4
    dtype: Int32
    
    s2.dtype
    # Int32Dtype()
    

    Your column needs to have whole numbers for the cast to happen. Anything else will raise a TypeError:

    s = pd.Series([1.1, 2.0, np.nan, 4.0])
    
    s.astype('Int32')
    # TypeError: cannot safely cast non-equivalent float64 to int32
    

提交回复
热议问题