Hello Experts…first of, sorry for this newbie question.
I encountered the code below"

if site_q05[site_q05 < lowerlimit].shape[0] > 1:

I understand the if statement but it has two condtion,
1st = site_q05 < lowerlimit (inside the bracket)
2nd = site_q05[site_q05 < lowerlimit].shape[0] > 1

Please help how to interpret this code…including the shape[0]…thank you.

will be either False or True, which are equal to 0 or 1. The subscription will pick out 1 of at least 2 items in site_q05. What is odd is that site_q05 is supposed to be both subscriptable and comparable to lowerlimit, which would seem to be a number, which is not subscriptable. That aside, the item from site_q05 is subscripted and the resulting item compared with 1. The expression might be clearer with parentheses.

Oh thank you @tjreedy…so you mean the:
1st = will either true or false…let say site_q05 is greater than lowerlimit then it is a false. Thus, it will not proceed?
On other hand, let say the site_q05 is less than lowerlimit, it is true…Thus, it will proceed to check if spare[0] is greater than 1?

Not necessarily. We don’t know what kind of things site_q05 and lowerlimit are, but site_q05 is probably not a list, since most objects do not support less/greater than comparisons with lists.

I would hazard to guess that site_q05 is a numpy.array or a pandas.DataFrame, based on the presence of the shape property. In which case site_q05 < lowerlimit is an index mask (e.g. [True, False, False, True, ...]), and site_q05[site_q05 < lowerlimit] is a subarray of site_q05 containing only the indices where the mask are True.

site_q05[site_q05 < lowerlimit].shape[0] is then the size of the first dimension of the subarray, and the if checks whether or not it is greater than 1.

Alexander, that makes sense. Rhett, unless you know something about numpy and pandas, this expression will not make sense. I know just a bit and was puzzled because I have read about but never seen an index mask in the wild. In any case, if there is no exception, the whole expression will be evaluated and the if body will execute if (obscure value) > 1.

Hello @tjreedy …here is more complete code…really need to understand this…sorry that I continue disturbing you…please help explain…I dont know who to ask already,…thanks

These are the first, fifth, and ninth deciles of the data in the col column of the df DataFrame (the fifth decile is more commonly known as the median). The q05 and q95 variables seem improperly named; they should be either q10 and q90 or they should be created with .quantile(0.05) and .quantile(0.95), respectivelly.

groupby_site = df.groupby("ch_num") splits the data into groups according to the values of the column named “ch_num”.

lowlimit = q50 - (sigma * (q50 - q05) / val) calculates a scalar value of some kind, based on the standard deviation, median, first decile, and val. I do not recognize the formula.

site_q05 = groupby_site[col].quantile(0.05) calculates the 5% quantile of each of the groups.

As discussed above, (site_q05[site_q05 < lowlimit]).shape[0] is the size of the first dimension of a subframe created by taking only the indices in site_q05 where the values of site_q05 are less than lowlimit. If the size of this dimension is not at least 2, “Limit error” is printed.

Since the index mask site_q05 < lowlimit contains only one True value, the subframe site_q05[site_q05 < lowlimit] contains only the value at that specific index. The size of the subframe’s first dimension is therefore 1.

A pandas.Series instance has an index and a series of values. The leftmost column is the index. When you create a subarray using an index mask, it contains only the values and indices where the mask is True. In this case, the first (index 0) and final (index 3) values met the mask condition.