本文介绍了数据类型"datetime64 [ns]"和“< M8 [ns]"之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在熊猫中创建了一个TimeSeries:

I have created a TimeSeries in pandas:

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 for Data Analysis"一书中的示例.

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

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

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]')

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

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

为什么我要使用其他类型?

And why do I get a different type?

推荐答案

datetime64[ns]是常规dtype,而<M8[ns]是特定的dtype.常规dtype映射到特定dtype,但与从NumPy的一个安装到下一个安装可能有所不同.

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.

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

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

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

但是,在大型字节序计算机上,np.dtype('datetime64[ns]')等于np.dtype('>M8[ns]').

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

因此,datetime64[ns]会根据计算机的字节序映射到<M8[ns]>M8[ns].

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

还有许多其他类似的通用dtype映射到特定dtype的示例:int64映射到<i8>i8,并且int映射到int32int64取决于操作系统的位架构以及NumPy的编译方式.

There are many other similar examples of general dtypes mapping to specific dtypes:int64 maps to <i8 or >i8, and int maps to either int32 or int64depending on the bit architecture of the OS and how NumPy was compiled.

很显然,自从撰写本书以来,datetime64 dtype的代表已经改变,以显示dtype的字节序.

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 [ns]"和“&lt; M8 [ns]"之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 18:39