BlackPhoenix
BlackPhoenix

Reputation: 306

Cut numpy matrix rows with upper and lower limit in python without pandas

Suppose I have some numpy matrix N x M where the column 0 is time, and that I want to select all the rows (which will be consecutive) for which the time value t is such that tmin < t < tmax. I would like to do it without using Pandas, and avoiding loops.

I have tried something like:

data = data[ tmin < data[:, 0] <=tmax]

But I obtain the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

On the other side, if I do the following:

data = data[ data[:, 0] <=tmax]

then I obtain the correct result. Can somebody please explain to me how could I write the first line of code in a similar compact (pythonic) way, but avoiding the value error? I am not sure how to use the .any() or .all() functions to obtain what I need. I have also seen the numpy.clip function, but that doesn't cut the rows with time values outside the boundaries, it only substitutes these values with (tmin, tmax).

Upvotes: 1

Views: 385

Answers (1)

itamar kanter
itamar kanter

Reputation: 1360

use numpy bitwise_and (or the & operator as shorthand)

np.random.seed(2021)
a = np.random.choice(10, (6,6))
print(a)
[[4 5 9 0 6 5]
 [8 6 6 6 6 1]
 [5 7 1 1 5 2]
 [0 3 1 0 2 6]
 [4 8 5 1 6 7]
 [5 6 9 5 6 9]]
tmin = 0
tmax = 5
a[(a[:, 0] > tmin) & (a[:, 0] <= tmax)]
array([[4, 5, 9, 0, 6, 5],
       [5, 7, 1, 1, 5, 2],
       [4, 8, 5, 1, 6, 7],
       [5, 6, 9, 5, 6, 9]])

Upvotes: 1

Related Questions