Python 数据类型“datetime64[ns]”和“<M8[ns]”之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29206612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:11:57  来源:igfitidea点击:

Difference between data type 'datetime64[ns]' and '<M8[ns]'?

pythonnumpypandasdatetime64

提问by LLaP

I have created a TimeSeries in pandas:

我在 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

I'm following on the example from 'Python for Data Analysis' book.

我正在关注“Python for Data Analysis”一书中的例子。

In the following paragraph, the author checks the index type:

在下面的段落中,作者检查了索引类型:

In [353]: ts.index.dtype

Out[353]: dtype('datetime64[ns]')

When I do exactly the same operation in the console I get:

当我在控制台中执行完全相同的操作时,我得到:

ts.index.dtype
dtype('<M8[ns]')

What is the difference between two types 'datetime64[ns]'and '<M8[ns]'?

'datetime64[ns]'和两种类型有什么区别'<M8[ns]'

And why do I get a different type?

为什么我会得到不同的类型?

采纳答案by unutbu

datetime64[ns]is a general dtype, while <M8[ns]is a specific dtype. General dtypes map to specific dtypes, but may be different from one installation of NumPy to the next.

datetime64[ns]是通用数据类型,<M8[ns]而是特定数据类型。一般 dtypes 映射到特定的 dtypes,但可能会因 NumPy 的一个安装而异。

On a machine whose byte order is little endian, there is no difference between np.dtype('datetime64[ns]')and np.dtype('<M8[ns]'):

在字节顺序为小端的机器上,np.dtype('datetime64[ns]')和之间没有区别 np.dtype('<M8[ns]')

In [6]: np.dtype('datetime64[ns]') == np.dtype('<M8[ns]')
Out[6]: True

However, on a big endian machine, np.dtype('datetime64[ns]')would equal np.dtype('>M8[ns]').

但是,在大端机器上,np.dtype('datetime64[ns]')将等于np.dtype('>M8[ns]').

So datetime64[ns]maps to either <M8[ns]or >M8[ns]depending on the endian-ness of the machine.

因此datetime64[ns]映射到<M8[ns]>M8[ns]取决于机器的字节序。

There are many other similar examples of general dtypes mapping to specific dtypes: int64maps to <i8or >i8, and intmaps to either int32or int64depending on the bit architecture of the OS and how NumPy was compiled.

还有许多其他类似的将一般 dtype 映射到特定 dtype 的示例: int64映射到<i8>i8,以及int映射到int32int64取决于操作系统的位体系结构以及 NumPy 的编译方式。



Apparently, the repr of the datetime64 dtype has change since the time the book was written to show the endian-ness of the dtype.

显然, datetime64 dtype 的 repr 自从本书编写以来就发生了变化,以显示 dtype 的字节序。

回答by jnPy

If this is generating errors in running your code, upgrading pandas and numpy synchronously is likely to solve the conflict in datetime datatype.

如果这在运行代码时产生错误,同步升级 pandas 和 numpy 可能会解决 datetime 数据类型的冲突。