当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。