Date 使用Numpy保存日期格式的列

Date 使用Numpy保存日期格式的列,date,numpy,datetime,Date,Numpy,Datetime,我试图使用以下Python代码保存一个带有日期列的文本文件。我在运行最后一行时遇到以下错误消息: “是否{IndexError}元组索引超出范围” 错误消息所指的索引是什么?该如何更正代码 In [263]: np.savetxt('test',column) .... ValueError: Expected 1D or 2D array, got 0D array instead 更改为保存1d阵列: In [264]: np.savetxt('test',[column]) ------

我试图使用以下Python代码保存一个带有日期列的文本文件。我在运行最后一行时遇到以下错误消息:

“是否{IndexError}元组索引超出范围”

错误消息所指的索引是什么?该如何更正代码

In [263]: np.savetxt('test',column)
....
ValueError: Expected 1D or 2D array, got 0D array instead
更改为保存1d阵列:

In [264]: np.savetxt('test',[column])
---------------------------------------------------------------------------
...
TypeError: float() argument must be a string or a number, not 'datetime.datetime'


TypeError: Mismatch between array dtype ('[('date', '<M8[us]')]') and format specifier ('%.18e')
二维时间数组:

In [271]: times = np.arange(np.datetime64('2018-02-09'),np.datetime64('2018-02-27'))
In [272]: times = times.reshape(3,-1)
In [273]: np.savetxt('test',times, fmt='%s')
In [274]: cat test
2018-02-09 2018-02-10 2018-02-11 2018-02-12 2018-02-13 2018-02-14
2018-02-15 2018-02-16 2018-02-17 2018-02-18 2018-02-19 2018-02-20
2018-02-21 2018-02-22 2018-02-23 2018-02-24 2018-02-25 2018-02-26

一维结构化数组的字段将被视为列:

In [288]: dt=np.dtype('<M8[D],<M8[D],<M8[D]')
In [290]: arr = np.array([tuple(x) for x in times.T.tolist()],dt)
In [291]: np.savetxt('test',arr,fmt='%s')
In [292]: cat test
2018-02-09 2018-02-15 2018-02-21
2018-02-10 2018-02-16 2018-02-22
2018-02-11 2018-02-17 2018-02-23
2018-02-12 2018-02-18 2018-02-24
2018-02-13 2018-02-19 2018-02-25
2018-02-14 2018-02-20 2018-02-26

In [296]: np.savetxt('test',arr,fmt='%s, %10s; %20s')
In [297]: cat test
2018-02-09, 2018-02-15;           2018-02-21
2018-02-10, 2018-02-16;           2018-02-22
...
[288]中的
:dt=np.dtype('
更改为保存1d阵列:

In [264]: np.savetxt('test',[column])
---------------------------------------------------------------------------
...
TypeError: float() argument must be a string or a number, not 'datetime.datetime'


TypeError: Mismatch between array dtype ('[('date', '<M8[us]')]') and format specifier ('%.18e')
二维时间数组:

In [271]: times = np.arange(np.datetime64('2018-02-09'),np.datetime64('2018-02-27'))
In [272]: times = times.reshape(3,-1)
In [273]: np.savetxt('test',times, fmt='%s')
In [274]: cat test
2018-02-09 2018-02-10 2018-02-11 2018-02-12 2018-02-13 2018-02-14
2018-02-15 2018-02-16 2018-02-17 2018-02-18 2018-02-19 2018-02-20
2018-02-21 2018-02-22 2018-02-23 2018-02-24 2018-02-25 2018-02-26

一维结构化数组的字段将被视为列:

In [288]: dt=np.dtype('<M8[D],<M8[D],<M8[D]')
In [290]: arr = np.array([tuple(x) for x in times.T.tolist()],dt)
In [291]: np.savetxt('test',arr,fmt='%s')
In [292]: cat test
2018-02-09 2018-02-15 2018-02-21
2018-02-10 2018-02-16 2018-02-22
2018-02-11 2018-02-17 2018-02-23
2018-02-12 2018-02-18 2018-02-24
2018-02-13 2018-02-19 2018-02-25
2018-02-14 2018-02-20 2018-02-26

In [296]: np.savetxt('test',arr,fmt='%s, %10s; %20s')
In [297]: cat test
2018-02-09, 2018-02-15;           2018-02-21
2018-02-10, 2018-02-16;           2018-02-22
...

[288]中的
:dt=np.dtype(“
column
是0d数组。
savetxt
将1d数组保存为列,将2d数组保存为列行。版本1.14有一条不同的错误消息:
预期为1d或2d数组,改为获得0d数组。
column
是0d数组。
savetxt
将1d数组保存为列,将2d数组保存为列行。版本1.14有一条不同的错误消息:
预期为1D或2D数组,取而代之的是0D数组
谢谢。但是如果我想保留结构化数组格式np.array(dt,dtype),我该怎么办?结构化数组格式是什么意思?在我的具体示例中,我的意思是我想使用格式列=np.array(dt,dtype),其中dtype指定了数据类型。我看你已经编辑了你的答案。+1让我明天试一试,然后接受你的答案。谢谢。谢谢。但是如果我想保留结构化数组格式np.array(dt,dtype),我该怎么办?你说的结构化数组格式是什么意思?在我的具体示例中,我的意思是我想使用格式列=np.array(dt,dtype),其中dtype指定数据类型。我看到您已经编辑了您的答案。+1让我明天试一试,然后接受您的答案。谢谢。