admin管理员组

文章数量:1530085

ValueError: cannot convert to 'int64'-dtype NumPy array with missing values. Specify an appropriate 'na_value' for this dtype.

目录

ValueError: cannot convert to 'int64'-dtype NumPy array with missing values. Specify an appropriate 'na_value' for this dtype.

问题:

解决:

完整错误:


问题:

注意,使用Int64可,此处I大写,但是在进入算法模型的时候又会出现问题,包庇了缺失值;

df_in['label'] = df_in['label'].astype('Int64')

使用小写int64,发生错误;

df_in = df_origin
#'int64'
df_in['label'] = df_in['label'].astype('int64')

解决:

正确的做法在缺失值填充之后再进行格式转换处理;

df_in = df_origin
#'int64'
# df_in['label'] = df_in['label'].astype('int64')


df_in = df_in.fillna(0)
df_in['label'] = df_in['label'].astype('int64')

完整错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-60-f515db1e7d91> in <module>
      1 df_in = df_origin
      2 #'Int64'
----> 3 df_in['label'] = df_in['label'].astype(np.int64)

D:\anaconda\lib\site-packages\pandas\core\generic.py in astype(self, dtype, copy, errors)
   5875         else:
   5876             # else, only a single dtype is given
-> 5877             new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
   5878             return self._constructor(new_data).__finalize__(self, method="astype")
   5879 

D:\anaconda\lib\site-packages\pandas\core\internals\managers.py in astype(self, dtype, copy, errors)
    629         self, dtype, copy: bool = False, errors: str = "raise"
    630     ) -> "BlockManager":
--> 631         return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
    632 
    633     def convert(

D:\anaconda\lib\site-packages\pandas\core\internals\managers.py in apply(self, f, align_keys, ignore_failures, **kwargs)
    425                     applied = b.apply(f, **kwargs)
    426                 else:
--> 427                     applied = getattr(b, f)(**kwargs)
    428             except (TypeError, NotImplementedError):
    429                 if not ignore_failures:

D:\anaconda\lib\site-packages\pandas\core\internals\blocks.py in astype(self, dtype, copy, errors)
    646         if self.is_extension:
    647             try:
--> 648                 values = self.values.astype(dtype)
    649             except (ValueError, TypeError):
    650                 if errors == "ignore":

D:\anaconda\lib\site-packages\pandas\core\arrays\integer.py in astype(self, dtype, copy)
    472             na_value = lib.no_default
    473 
--> 474         return self.to_numpy(dtype=dtype, na_value=na_value, copy=False)
    475 
    476     def _values_for_argsort(self) -> np.ndarray:

D:\anaconda\lib\site-packages\pandas\core\arrays\masked.py in to_numpy(self, dtype, copy, na_value)
    219             ):
    220                 raise ValueError(
--> 221                     f"cannot convert to '{dtype}'-dtype NumPy array "
    222                     "with missing values. Specify an appropriate 'na_value' "
    223                     "for this dtype."

ValueError: cannot convert to 'int64'-dtype NumPy array with missing values. Specify an appropriate 'na_value' for this dtype.

本文标签: dtypeValueErrorConvertmissingvalues