2

The function below uses slices to get the maximum value between 2 indexes at once. So it gets the maximum value between 0 and 10 and then 10 and 12 and such. The function is derived from the answer to this post post. Is there a way I could could replace the list comprehensions in the form of a pandas function like pd.Series(). Or if possible do it as a numpy function.

list_ = np.array([9887.89, 9902.99, 9902.99, 9910.23, 9920.79, 9911.34, 9920.01, 9927.51, 9932.3, 9932.33, 9928.87, 9929.22, 9929.22, 9935.24, 9935.24, 9935.26, 9935.26, 9935.68, 9935.68, 9940.5])
indexes = np.array([0,10,12,14])
chunks = np.split(list_, indexes[1:-1])
MAX=([c.max() for c in chunks])
2
  • 1
    Why do you want to do this? Is it for performance reasons or do you need results in the form of a numpy array? Commented Jan 29, 2021 at 0:09
  • df.iloc[0:10].max() just replace 0/10 with what you want Commented Jan 29, 2021 at 0:11

1 Answer 1

3

I would recommend this:

MAX = np.maximum.reduceat(list_,indexes[:-1])

output:

array([9932.33, 9929.22, 9940.5 ])

Another way that will NOT increase your performance and is merely a replacement for list comprehension in your answer (in fact, might even be slightly slower):

max = np.vectorize(np.max)
MAX = max(chunks)
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way I could get the indexes of 9932.33, 9929.22, 9940.5 as well. I tried to implement argmax to it howwver it didnt workout
@tonyselcuk Quite a different question but does this post answer your question stackoverflow.com/a/41835843/4975981?
@tonyselcuk Glad it resolved. Please feel free to accept the answer to close the question. Thank you.
Sorry about the delay just did it now.

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.