What is the most efficient and Pythonic way of calculating limsup/liminf in pandas?

Question:

Limsup is defined as the supremum of a sequence onward. In other words, at the current moment one can look at the future values and get the maximum of them to create the limsup.

Question

What is the most efficient and Pythonic way of calculating limsup/liminf in pandas?

My try

I am calculating the limsup using a for loop which I am sure is not an efficient way.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
x = np.random.randn(2000)
y = np.cumsum(x)
df = pd.DataFrame(y, columns=['value'])
df['lim_sup'] = 0
fig, ax = plt.subplots(figsize=(20, 4))
for i in range(len(df)):
  df['lim_sup'].iloc[i] = df['value'].iloc[i:].max()

df['value'].plot(ax=ax)
df['lim_sup'].plot(ax=ax, color='r')
ax.legend(['value', 'limsup'])
plt.show()

enter image description here

Asked By: Sepide

||

Answers:

Reverse the values and use cummax to get the cumulative maximum from the bottom up:

df["suprema"] = df.loc[::-1, "value"].cummax()

This column should probably be referred to as the suprema for m >= n, rather than the limsup.

Answered By: Stuart
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.