我在 pandas 中创建了一个 TimeSeries:
In [346]: from datetime import datetime
In [347]: dates = [datetime(2011, 1, 2), datetime(2011, 1, 5), datetime(2011, 1, 7),
.....: datetime(2011, 1, 8), datetime(2011, 1, 10), datetime(2011, 1, 12)]
In [348]: ts = Series(np.random.randn(6), index=dates)
In [349]: ts
Out[349]:
2011-01-02 0.690002
2011-01-05 1.001543
2011-01-07 -0.503087
2011-01-08 -0.622274
2011-01-10 -0.921169
2011-01-12 -0.726213
我正在关注“用于数据分析的 Python”一书中的示例。
在以下段落中,作者检查了索引类型:
In [353]: ts.index.dtype
Out[353]: dtype('datetime64[ns]')
当我在控制台中执行完全相同的操作时,我得到:
ts.index.dtype
dtype('<>
两种类型有什么区别'datetime64[ns]'
Và '<>
?
为什么我会得到不同的类型?
datetime64[ns]
是一般 dtype,而 <>
是一个特定的数据类型。一般 dtypes 映射到特定 dtypes,但可能与 NumPy 的一个安装不同。
在字节序为little endian的机器上,两者没有区别 np.dtype('datetime64[ns]')
Và np.dtype('<>
:
In [6]: np.dtype('datetime64[ns]') == np.dtype('<>
Out[6]: True
但是,在大端机器上,np.dtype('datetime64[ns]')
等于 np.dtype('>M8[ns]')
.
Vì thế datetime64[ns]
ánh xạ tới <>
hoặc >M8[ns]
取决于机器的字节序。
还有许多其他类似的通用 dtype 映射到特定 dtype 的示例: int64
ánh xạ tới <>
hoặc >i8
, 和 số nguyên
ánh xạ tới int32
hoặc int64
取决于操作系统的位架构以及 NumPy 的编译方式。
显然,datetime64 dtype 的 repr 自从这本书被写出来以显示 dtype 的字节顺序以来发生了变化。
Tôi là một lập trình viên xuất sắc, rất giỏi!