【问题标题】:what are all the dtypes that pandas recognizes?熊猫识别的所有 dtypes 是什么?
【发布时间】:2015-05-28 13:03:53
【问题描述】:

对于 pandas,如果有任何数据类型,谁会知道

(i) float64int64(以及np.number 的其他变体,例如float32int8 等)

(ii)bool

(iii)datetime64timedelta64

例如字符串列,总是有dtypeobject 吗?

另外,我想知道,如果在上面的列表中除了 (i)、(ii) 和 (iii) 之外还有任何数据类型,pandas 不会使其成为 dtypeobject

【问题讨论】:

标签: python python-3.x pandas


【解决方案1】:

pandasnumpy 借用它的数据类型。有关这一点的演示,请参见以下内容:

import pandas as pd

df = pd.DataFrame({'A': [1,'C',2.]})
df['A'].dtype

>>> dtype('O')

type(df['A'].dtype)

>>> numpy.dtype

你可以找到有效numpy.dtypesin the documentation的列表:

'?'布尔值

'b'(有符号)字节

'B' 无符号字节

'i'(有符号)整数

'u'无符号整数

'f' 浮点数

'c' 复数浮点数

'm'时间增量

'M' 日期时间

'O' (Python) 对象

'S', 'a' 以零结尾的字节(不推荐)

'U' Unicode 字符串

'V' 原始数据(无效)

pandas 应该支持这些类型。使用带有上述任何选项的pandas.Series 对象的astype 方法作为输入参数将导致pandas 尝试将Series 转换为该类型(或至少回退到object类型); 'u' 是我看到的唯一一个 pandas 完全不理解:

df['A'].astype('u')

>>> TypeError: data type "u" not understood

这是一个numpy 错误,因为'u' 后面需要一个数字来指定每个项目的字节数(必须是有效的):

import numpy as np

np.dtype('u')

>>> TypeError: data type "u" not understood

np.dtype('u1')

>>> dtype('uint8')

np.dtype('u2')

>>> dtype('uint16')

np.dtype('u4')

>>> dtype('uint32')

np.dtype('u8')

>>> dtype('uint64')

# testing another invalid argument
np.dtype('u3')

>>> TypeError: data type "u3" not understood

总而言之,pandas 对象的astype 方法将尝试对任何对numpy.dtype 有效的参数做一些明智的事情。请注意,numpy.dtype('f')numpy.dtype('float32') 相同,numpy.dtype('f8')numpy.dtype('float64') 等相同。将参数传递给 pandas astype 方法也是如此。

要在 NumPy 中定位相应的数据类型类,Pandas docs 建议这样做:

def subdtypes(dtype):
    subs = dtype.__subclasses__()
    if not subs:
        return dtype
    return [dtype, [subdtypes(dt) for dt in subs]]

subdtypes(np.generic)

输出:

[numpy.generic,
 [[numpy.number,
   [[numpy.integer,
     [[numpy.signedinteger,
       [numpy.int8,
        numpy.int16,
        numpy.int32,
        numpy.int64,
        numpy.int64,
        numpy.timedelta64]],
      [numpy.unsignedinteger,
       [numpy.uint8,
        numpy.uint16,
        numpy.uint32,
        numpy.uint64,
        numpy.uint64]]]],
    [numpy.inexact,
     [[numpy.floating,
       [numpy.float16, numpy.float32, numpy.float64, numpy.float128]],
      [numpy.complexfloating,
       [numpy.complex64, numpy.complex128, numpy.complex256]]]]]],
  [numpy.flexible,
   [[numpy.character, [numpy.bytes_, numpy.str_]],
    [numpy.void, [numpy.record]]]],
  numpy.bool_,
  numpy.datetime64,
  numpy.object_]]

Pandas 接受这些类作为有效类型。例如,dtype={'A': np.float}

NumPy 文档contain 更多详细信息和图表:

【讨论】:

    【解决方案2】:

    在 Pandas 1.0.0 发布后于 2020 年 2 月编辑

    Pandas 主要为每个系列使用 NumPy 数组和数据类型(数据框是系列的集合,每个系列都可以有自己的数据类型)。 NumPy 的文档进一步解释了dtypedata typesdata type objects。此外,@lcameron05 提供的答案很好地描述了 numpy dtypes。此外,dtypes 上的 pandas 文档还有很多附加信息。

    pandas 对象中存储的主要类型有 float、int、bool、 datetime64[ns]、timedelta[ns] 和对象。此外这些 dtypes 有物品尺寸,例如int64 和 int32。

    默认整数类型是int64,浮点类型是float64, 无论平台(32 位或 64 位)。以下都会 导致 int64 dtypes。

    Numpy,但是在创建时会选择依赖于平台的类型 数组。以下将导致 int32 在 32 位平台上。 pandas 1.0.0 版的主要变化之一是引入了pd.NA 来表示标量缺失值(而不是之前的值np.nanpd.NaTNone,具体取决于使用情况)。

    Pandas 扩展了 NumPy 的类型系统,还允许用户在 extension types 上编写他们的内容。以下列出了所有 pandas 扩展类型。

    1) Time zone handling

    数据类型:可识别 tz 的日期时间(注意 NumPy 不支持可识别时区的日期时间)。

    数据类型:DatetimeTZDtype

    标量:Timestamp

    数组:arrays.DatetimeArray

    字符串别名:'datetime64[ns, ]'

    2) Categorical data

    数据类型:分类

    数据类型:CategoricalDtype

    标量:(无)

    数组:Categorical

    字符串别名:'category'

    3) Time span representation

    数据种类:周期(时间跨度)

    数据类型:PeriodDtype

    标量:Period

    数组:arrays.PeriodArray

    字符串别名:'period[]'、'Period[]'

    4) Sparse data structures

    数据种类:稀疏

    数据类型:SparseDtype

    标量:(无)

    数组:arrays.SparseArray

    字符串别名:'Sparse'、'Sparse[int]'、'Sparse[float]'

    5) IntervalIndex

    数据种类:区间

    数据类型:IntervalDtype

    标量:Interval

    数组:arrays.IntervalArray

    字符串别名:'interval'、'Interval'、'Interval[]'、'Interval[datetime64[ns, ]]'、'Interval[timedelta64[]]'

    6) Nullable integer data type

    数据类型:可为空的整数

    数据类型:Int64Dtype, ...

    标量:(无)

    数组:arrays.IntegerArray

    字符串别名:'Int8'、'Int16'、'Int32'、'Int64'、'UInt8'、'UInt16'、'UInt32'、'UInt64'

    7) Working with text data

    数据类型:字符串

    数据类型:StringDtype

    标量:str

    数组:arrays.StringArray

    字符串别名:'字符串'

    8) Boolean data with missing values

    数据类型:布尔值(带 NA)

    数据类型:BooleanDtype

    标量:bool

    数组:arrays.BooleanArray

    字符串别名:'boolean'

    【讨论】:

      【解决方案3】:

      在其他答案的基础上,pandas 还包含许多自己的 dtype。

      Pandas 和第三方库在几个方面扩展了 NumPy 的类型系统 地方。本节介绍 pandas 所做的扩展 内部。请参阅扩展类型了解如何编写自己的扩展 与熊猫一起使用。有关列表,请参阅扩展数据类型 已实现扩展的第三方库。

      下表列出了所有 pandas 扩展类型。见 相关文件

      https://pandas.pydata.org/pandas-docs/stable/user_guide/basics.html#basics-dtypes

      --更新链接--

      此外,从 pandas 1.0 开始,它有自己的字符串 dtype 和可为空的 dtype。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-11
        • 2017-09-23
        • 1970-01-01
        • 1970-01-01
        • 2017-02-17
        • 2014-02-11
        相关资源
        最近更新 更多