NumPy字符串数组的算术运算

时间:2018-03-05 03:40:25

标签: python numpy math

我的代码如下:

import numpy as np

data =[['2015-07-21 22:18:04', -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, 10.1, -999.25, -999.25],
       ['2015-07-21 22:18:05', -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25,
        -999.25, -999.25, 10.1, -999.25, -999.25],
       ['2015-07-21 22:18:06', -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25,
        -999.25, -999.25, 10.1, -999.25, -999.25]]

arr = np.array(data)
depth = arr[:, 1]

print(type(depth))

diff = [depth[i] - depth[i - 1] for i in range(len(depth))][1:]
diff_index = [i for i, item in enumerate(diff) if item > 0]
data = [data[row] for row in diff_index]

当我运行我的代码时,我收到以下错误信息:

    diff = [depth[i] - depth[i - 1] for i in range(len(depth))][1:]
TypeError: unsupported operand type(s) for -: 'numpy.str_' and 'numpy.str_'

我之前从未使用过numpy,所以我很困惑。我找不到可以帮助我的在线文档,但可能是因为我不知道大多数numpy的东西。

我想从numpy数组中选择第i个元素,然后执行算术运算。如何修复我的代码以便它能完成这项工作?

1 个答案:

答案 0 :(得分:3)

In [364]: data =[['2015-07-21 22:18:04', -999.25, -999.25, -999.25, -999.25, -99...
     ...: arr = np.array(data)
     ...: depth = arr[:, 1]
     ...: 
In [365]: type(depth)       # type doesn't tell us anything important
Out[365]: numpy.ndarray
In [366]: depth.dtype       # but dtype does
Out[366]: dtype('<U19')
In [367]: arr
Out[367]: 
array([['2015-07-21 22:18:04', '-999.25', '-999.25', '-999.25',
        '-999.25', '-999.25', '-999.25', '-999.25', '-999.25', '-999.25',
        '-999.25', '-999.25', '-999.25', '10.1', '-999.25', '-999.25'],...],
      dtype='<U19')

因为第一个数字是字符串(日期),所以一切都是字符串:

In [368]: depth
Out[368]: array(['-999.25', '-999.25', '-999.25'], dtype='<U19')

我们可以将arr数组转换为float而不使用第一列:

In [369]: arr[:,1:].astype(float)
Out[369]: 
array([[-999.25, -999.25, -999.25, -999.25, -999.25, -999.25, -999.25,
        -999.25, -999.25, -999.25, -999.25, -999.25,   10.1 , -999.25,
        -999.25],
 ...])

或只是depth

In [370]: depth=arr[:,1].astype(float)
In [371]: diff = [depth[i] - depth[i - 1] for i in range(len(depth))][1:]
In [372]: diff
Out[372]: [0.0, 0.0]

但由于这是一个数组,我们不必使用列表计算:

In [373]: depth
Out[373]: array([-999.25, -999.25, -999.25])
In [374]: depth[1:]-depth[:-1]
Out[374]: array([0., 0.])