0

I have a code that looks like this:

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]

When I run my code, I get the following error msg:

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

I've never used numpy before, so I am confused. I couldn't find online documentation that can help me, but its probably because I don't know most of numpy stuff.

I want to select the i-th element from the numpy array, and perform arithmetic operations. How can one fix my code so that it will do the job?

0

1 Answer 1

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

Because the first numbers are strings (dates), everything is strings:

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

We can convert the arr array into float without the the first column:

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],
 ...])

Or just 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]

But since this is an array, we don't have to use list calculations:

In [373]: depth
Out[373]: array([-999.25, -999.25, -999.25])
In [374]: depth[1:]-depth[:-1]
Out[374]: array([0., 0.])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.