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


Python pandas.api.types.infer_dtype用法及代碼示例

用法:

pandas.api.types.infer_dtype()

有效地推斷傳遞的 val 或 list-like 值數組的類型。返回說明類型的字符串。

參數

value標量、列表、ndarray 或 pandas 類型
skipna布爾值,默認為真

推斷類型時忽略 NaN 值。

返回

str

說明輸入數據的常見類型。

結果可以包括:
  • string
  • bytes
  • floating
  • integer
  • mixed-integer
  • mixed-integer-float
  • decimal
  • complex
  • categorical
  • boolean
  • datetime64
  • datetime
  • date
  • timedelta64
  • timedelta
  • time
  • period
  • mixed
  • unknown-array

拋出

TypeError

如果 ndarray-like 但無法推斷 dtype

注意

  • ‘mixed’ 是任何非專業化事物的總稱

  • ‘mixed-integer-float’ 是浮點數和整數

  • ‘mixed-integer’ 是整數與非整數的混合

  • ‘unknown-array’ 是數組(具有 dtype 屬性)但具有 pandas 未知的 dtype(例如外部擴展數組)的總稱

例子

>>> import datetime
>>> infer_dtype(['foo', 'bar'])
'string'
>>> infer_dtype(['a', np.nan, 'b'], skipna=True)
'string'
>>> infer_dtype(['a', np.nan, 'b'], skipna=False)
'mixed'
>>> infer_dtype([b'foo', b'bar'])
'bytes'
>>> infer_dtype([1, 2, 3])
'integer'
>>> infer_dtype([1, 2, 3.5])
'mixed-integer-float'
>>> infer_dtype([1.0, 2.0, 3.5])
'floating'
>>> infer_dtype(['a', 1])
'mixed-integer'
>>> infer_dtype([Decimal(1), Decimal(2.0)])
'decimal'
>>> infer_dtype([True, False])
'boolean'
>>> infer_dtype([True, False, np.nan])
'boolean'
>>> infer_dtype([pd.Timestamp('20130101')])
'datetime'
>>> infer_dtype([datetime.date(2013, 1, 1)])
'date'
>>> infer_dtype([np.datetime64('2013-01-01')])
'datetime64'
>>> infer_dtype([datetime.timedelta(0, 1, 1)])
'timedelta'
>>> infer_dtype(pd.Series(list('aabc')).astype('category'))
'categorical'

相關用法


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