【发布时间】:2019-09-19 15:03:59
【问题描述】:
Python 2.7:
尝试:
从一维 Numpy 数组中添加一个带有 datetype64(D) 的列 (arr_date) 到现有的多维 Numpy 数组(数据)
出现以下错误:
- 'TypeError: 无效类型提升'
- 'numpy.AxisError:axis 1 is out of bounds for array of dimension 1'
创建的列,需要附加:
>> arr_date
<<
[['2019-04-21']
['2019-04-21']
['2019-04-21']]
尝试在新 Numpy 数组 (arr_date) 中的源(数据)中提供的 3 列中创建一个日期时间对象,并使用以下方法将其添加到旧数组(数据)中:
- np.c_
- np.append
- np.hstack
- np.column_stack
- np.连接
data = [(2019, 4, 21, 4.9, -16.5447, -177.1961, 22.4, 'US')
(2019, 4, 21, 4.8, -9.5526, 109.6003, 10. , 'UK')
(2019, 4, 21, 4.6, -7.2737, 124.0192, 554.9, 'FR')]
arr_date = np.zeros((len(data),1), dtype='datetime64[D]')
i = 0
while i < len(data):
date = dt.date(data [i][0], data[i][1], data[i][2])
arr_date[i][0] = date
i += 1
test1 = np.column_stack((data,arr_date))
np.c_[data, np.zeros(len(data))]
test2 = np.concatenate(data.reshape(-1,1), arr_date.reshape(-1,1), axis=1)
np.append(data, arr_date, axis = 1)
np.stack((data, arr_date), axis=-1)
np.hstack((data, arr_date))
test3 = np.column_stack((data, arr_date))
【问题讨论】:
-
这些函数都使用
np.concatenate,这意味着输入必须具有兼容的数据类型和兼容的形状。如果一个失败了,其他的很可能也会失败,特别是如果这是一个 dtype 问题。 -
data是什么。它看起来像一个元组列表,除了元组之间缺少逗号。它是结构化数组吗? (3,) 是什么形状?dtype是什么。 -
是通过将
csv与genfromtxt类似的内容加载生成的data? -
是的,数据是通过加载 csv 并使用模块 genfromtxt 生成的。
标签: python arrays numpy append concatenation