简体   繁体   中英

How do you get the last n-th item matching a condition in a pandas series (valuewhen from PineScript)

In PineScript there is a function called valuewhen that returns the n-th most recent occurrence value of a source when a condition is true. So for example, an index of zero will return the most recent value when the condition is true, and an index of one will return the second most recent.

How do you do this using Pandas in Python on a Series ? The function signature in PineScript is valuewhen(condition, source, occurrence) → series[float]

To implement this function, first drop unwanted values using reindex and a condition as a mask. Next, shift the series according to which occurrence is desired. Then, use reindex to add in the index values that was dropped from the mask and first reindex. These index values will point to np.nan . Finally, use ffill() to forward fill values onto the np.nan values.

This assumes that occurrences is a properly bounded non-negative number, source is an ordered sequence, and the condition is related to the source by their index .

This can be written in Python like this:

def valuewhen(condition, source, occurrence):
    return source \
        .reindex(condition[condition].index) \
        .shift(-occurrence) \
        .reindex(source.index) \
        .ffill()

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