9

I have a Numpy rec array from which I would like to do some quick queries similar to SQL: SELECT * where array['phase'] == "P". I would like to get a Record Array as output with each row corresponding to a row from the original array that met the query criteria. Any ideas? I am pretty sure I have done this before, but just cannot remember the function.

Thanks

rec.array([ (5295499, 2.8123965, 127.20443, 0.0, 1237680436.06, 908, -19.942589, 134.33951, 0.3888, 'P', 0.19513991),
       (5295499, 2.8123965, 127.20443, 0.0, 1237680436.06, 1387, -18.102, 125.639, 0.11, 'P', 1.2515257),
       (5447254, 39.025873, 143.31065, 0.0, 1245455521.85, 1512, 33.121667, 130.87833, 0.573, 'LR', 45.099504)], 
      dtype=[('eventid', '<i4'), ('eventlat', '<f8'), ('eventlon', '<f8'), ('eventdepth', '<f8'), ('eventtime', '<f8'), ('stationid', '<i4'), ('stationlat', '<f8'), ('stationlon', '<f8'), ('stationelv', '<f8'), ('phase', '|S7'), ('timeresidual', '<f8')])
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
Rishi
  • 3,538
  • 5
  • 29
  • 40

2 Answers2

9

Try:

array[array['phase']=='P']

array['phase']=='P' returns a boolean numpy array. When idx is a boolean array, array[idx] returns an array composed of those rows where idx is True.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 1
    This is genius! But just to clarify for other ppl: array here does not refer to numpy.array, it refers to the name of your record array. For instance, if I had a recarray called data, then I would want to do: data[data['phase'] == 'P']] – Rishi Sep 06 '11 at 00:39
0

Try this code:

array[array.phase=='P'] 
CuberChase
  • 4,458
  • 5
  • 33
  • 52