简体   繁体   中英

How to Index Values From a where Condition Tuple in Python?

I am trying to get the first and last element of a tuple created from a where condition. I am getting stuck because all of the values satisfying the where condition are stored in the 0th index of s_time_ind rather than have 0,1,2,...,48 indices.

These are the first values of interest in the time array. There are 1111 values and they are float64 type.

769     7.000000
770     7.001389
771     7.002778
772     7.004167
773     7.005556
774     7.006944
775     7.008333
776     7.009722
777     7.011111
778     7.012500
779     7.013889
780     7.015278
781     7.016667
782     7.018056
783     7.019444
784     7.020833
785     7.022222
786     7.023611
787     7.025000
788     7.026389
789     7.027778
790     7.029167
791     7.030556
792     7.031944
793     7.033333
794     7.034722
795     7.036111
796     7.037500
797     7.038889
798     7.040278
799     7.041667
800     7.043056
801     7.044444
802     7.045833
803     7.047222
804     7.048611
805     7.050000
806     7.051389
807     7.052778
808     7.054167
809     7.055556
810     7.056944
811     7.058333
812     7.059722
813     7.061111
814     7.062500
815     7.063889
816     7.065278
817     7.066667

This is the condition I have coded and it is printing the correct indices that I need but it's storing them in the 0th index of s_time_ind so I can't pull out the first and last index by doing s_time_ind[0] to get 769 and s_time_ind[48] to get 817 .

s_time_ind = np.where((time >= 7.000000) & (time <= 7.066667))
print(s_time_ind)

(array([769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781,
       782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794,
       795, 796, 797, 798, 799, 800, 801, 802, 803, 804, 805, 806, 807,
       808, 809, 810, 811, 812, 813, 814, 815, 816, 817], dtype=int64),)

When I enter print(s_time_ind[0]) I get all of the values.

[769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
 805 806 807 808 809 810 811 812 813 814 815 816 817]

And when I enter print(s_time_ind[1]) or any value greater than 0 I get the following error:

IndexError                                Traceback (most recent call last)
<ipython-input-126-b4a0e1537e07> in <module>
      1 s_time_ind = np.where((time >= 7.000000) & (time <= 7.066667))
----> 2 print(s_time_ind[1])

IndexError: tuple index out of range

I want to be able to enter s_time_ind[0] and get 769 and s_time_ind[48] to get 817 .

np.where returns a python tuple with one array per dimension. You happen to have a 1D array, so you are not noticing that the result is surrounded in parentheses:

(array([...], dtype=int64),)

Notice the comma at the end. That comma tells you that the outer parentheses represent a tuple , not just casual grouping.

The result you are looking for is the array in the beginning of the tuple. You can extract it by doing:

s_time_ind = np.where((time >= 7.000000) & (time <= 7.066667))
s_time_ind = s_time_ind[0]   # Now s_time_ind is the array of indices, not a tuple

More concisely:

s_time_ind = np.where((time >= 7.000000) & (time <= 7.066667))[0]

Since the one-arg version of np.where is a wrapper for np.nonzero , it's better to use the latter for indexing a mask:

s_time_ind = np.nonzero((time >= 7.000000) & (time <= 7.066667))[0]

The best option is to use np.flatnonzero for one-dimensional arrays:

s_time_ind = np.flatnonzero((time >= 7.000000) & (time <= 7.066667))

No extracting necessary this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM