Skip to content

Instantly share code, notes, and snippets.

@Coldsp33d
Last active January 20, 2019 06:21
Show Gist options
  • Save Coldsp33d/56cc06edc5455d6de51d21c82fafb21f to your computer and use it in GitHub Desktop.
Save Coldsp33d/56cc06edc5455d6de51d21c82fafb21f to your computer and use it in GitHub Desktop.
import pandas as pd
import matplotlib.pyplot as plt
import perfplot
def cs(df):
df = df.copy()
m = df.value.eq(1) # Cached for efficiency.
df['previous_smaller_index'] = np.where(
m, -1, len(df) - np.triu(df.value.values < df.value[:,None]).argmax(1) - 1
)[::-1]
df['previous_1_index'] = (df.assign(X=m.cumsum())
.groupby('X')['X']
.transform('idxmax')
.mask(m, -1))
return df
def cs2(df):
df = df.copy()
m = df.value.eq(1) # Cached for efficiency.
df['previous_smaller_index'] = np.where(
m, -1, len(df) - np.triu(df.value.values < df.value[:,None]).argmax(1) - 1
)[::-1]
df['previous_1_index'] = (np.where(m, -1,
df.index.where(m).to_series(index=df.index).ffill(downcast='infer')))
return df
def spghttCd(df):
df = df.copy()
df['previous_smaller_index'] = (
df.apply(lambda x: df.index[:x.name][(x.value>df.value)[:x.name]].max(),
axis=1))
df['previous_1_index'] = (
pd.Series(df.index.where(df.value==1)).shift()[df.value!=1].ffill())
return df.fillna(-1, downcast='infer')
def wb(df):
df = df.copy()
l=list(zip(df['index'],df.value))[::-1]
t=[]
n=len(l)
for x in l:
if x[1]==1:
t.append(-1)
else:
t.append(next(y for y in l[n-x[0]:] if y[1]<x[1])[0])
df['previous_smaller_index']=t[::-1]
df['previous_1_index']=df['index'].where(df.value==1).ffill().where(df.value!=1,-1).astype(int)
return df
def quick_concat(df_, n):
df = pd.concat([df_] * n, ignore_index=True)
df['index'] = df.index
return df
data = {
'index': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6},
'value': {0: 1, 1: 1, 2: 2, 3: 3, 4: 2, 5: 1, 6: 1}
}
df_ = pd.DataFrame(data).assign(
previous_smaller_index=np.nan, previous_1_index=np.nan)
df_
# index value prev_smaller_idx prev_1_idx
# 0 0 1 NaN NaN
# 1 1 1 NaN NaN
# 2 2 2 NaN NaN
# 3 3 3 NaN NaN
# 4 4 2 NaN NaN
# 5 5 1 NaN NaN
# 6 6 1 NaN NaN
perfplot.show(
setup=lambda n: quick_concat(df_, n),
kernels=[cs, cs2, spghttCd, wb],
labels=['coldspeed', 'coldspeed2', 'SpghttCd', 'W-B'],
n_range=[2**k for k in range(0, 7)],
xlabel='N (scale for `df_`)',
logy=True,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment