當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python cudf.to_numeric用法及代碼示例

用法:

cudf.to_numeric(arg, errors='raise', downcast=None)

將參數轉換為數值類型。

參數

argcolumn-convertible

要轉換為數字類型的對象

errors{‘raise’, ‘ignore’, ‘coerce’},默認 ‘raise’

解析期間處理錯誤的策略。

  • ‘raise’ 將通知用戶遇到的所有錯誤。
  • ‘ignore’ 將跳過錯誤並返回 arg
  • ‘coerce’ 會將無效值保留為空值。
downcast{‘integer’, ‘signed’, ‘unsigned’, ‘float’},默認無

如果設置,將嘗試將down-convert 解析結果的數據類型設為可能的最小類型。對於每個 downcast 類型,此方法將從以下集合中確定可能的最小 dtype:

  • {‘integer’, ‘signed’}:所有大於或等於np.int8的整數類型
  • {‘unsigned’}:所有大於或等於np.uint8的無符號類型
  • {‘float’}:所有大於或等於np.float32的浮點類型

請注意,向下轉換行為與解析解耦。無論errors 參數如何,都會引發向下轉換期間遇到的錯誤。

返回

係列或ndarray

根據輸入,如果傳入series,則返回series,否則返回ndarray

注意

與 pandas 的一個重要區別是此函數不接受混合的數字/非數字類型序列。例如 [1, 'a'] 。接收到此類輸入時將引發 TypeError,無論 errors 參數如何。

例子

>>> s = cudf.Series(['1', '2.0', '3e3'])
>>> cudf.to_numeric(s)
0       1.0
1       2.0
2    3000.0
dtype: float64
>>> cudf.to_numeric(s, downcast='float')
0       1.0
1       2.0
2    3000.0
dtype: float32
>>> cudf.to_numeric(s, downcast='signed')
0       1
1       2
2    3000
dtype: int16
>>> s = cudf.Series(['apple', '1.0', '3e3'])
>>> cudf.to_numeric(s, errors='ignore')
0    apple
1      1.0
2      3e3
dtype: object
>>> cudf.to_numeric(s, errors='coerce')
0      <NA>
1       1.0
2    3000.0
dtype: float64

相關用法


注:本文由純淨天空篩選整理自rapids.ai大神的英文原創作品 cudf.to_numeric。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。