The main types stored in pandas objects are float, int, bool,
datetime64[ns], timedelta[ns], and object. In addition these dtypes
have item sizes, e.g. int64 and int32.
By default integer types are int64 and float types are float64,
REGARDLESS of platform (32-bit or 64-bit). The following will all
result in int64 dtypes.
Numpy, however will choose platform-dependent types when creating
arrays. The following WILL result in int32 on 32-bit platform.
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 and third-party libraries extend NumPy’s type system in a few
places. This section describes the extensions pandas has made
internally. See Extension types for how to write your own extension
that works with pandas. See Extension data types for a list of
third-party libraries that have implemented an extension.
The following table lists all of pandas extension types. See the
respective document
还有一个是uint8。
关于熊猫的文档有很多信息。
pandas
从numpy
借用其数据类型。有关演示,请参见以下内容:您可以找到有效的
numpy.dtypes
in the documentation列表:pandas
应该支持这些类型。使用pandas.Series
对象的astype
方法作为输入参数,将导致pandas
尝试将Series
转换为该类型(或者至少返回到object
类型);'u'
是我看到的唯一一个pandas
完全不理解的方法:这是一个
numpy
错误,其结果是'u'
需要后跟一个数字,该数字指定中每个项的字节数(需要有效):总之,
pandas
对象的astype
方法将尝试对任何对numpy.dtype
有效的参数执行一些合理的操作。请注意,numpy.dtype('f')
与numpy.dtype('float32')
相同,numpy.dtype('f8')
与numpy.dtype('float64')
等相同。将参数传递给pandas
astype
方法也是如此。要在NumPy中定位相应的数据类型类,Pandas docs建议如下:
输出:
熊猫接受这些类作为有效类型。例如,
dtype={'A': np.float}
。NumPy文档contain更多详细信息和图表:
在其他答案的基础上,熊猫还包含了一些自己的数据类型。
https://pandas.pydata.org/pandas-docs/stable/getting_started/basics.html#dtypes
此外,pandas 1.0将有一个字符串dtype。
相关问题 更多 >
编程相关推荐