Skip to content

Instantly share code, notes, and snippets.

@kchawla-pi
Created June 16, 2022 21:14
Show Gist options
  • Save kchawla-pi/4e66e81d3ec993d52db0a5c0551a41ea to your computer and use it in GitHub Desktop.
Save kchawla-pi/4e66e81d3ec993d52db0a5c0551a41ea to your computer and use it in GitHub Desktop.
Coworking with Himja,
from pprint import pprint
import numpy as np
import pandas as pd
def fill_first_base_price_of_above_one_window(df, indices):
df["window_base_price"].at[indices[-1]] = df["base_price"].at[indices[0]]
def mock_data(values):
return pd.DataFrame({
"base_price": values,
"windowing_values": values ** 2,
}
)
df = mock_data(values=np.array([0.3, 4.3, 5.6, 2.1, 0.6, 0.7, 0.3, 1.4, 1.5, 0.9]))
pprint(df)
print("-" * 50)
df["window"] = df["windowing_values"] > 1
df["window"] = df["window"].astype(int)
df["window_base_price"] = np.nan
pprint(df)
print("-" * 50)
above_one_indices = df.groupby(["window"]).groups[True]
window_indices = df.groupby((df['window'].shift() != df['window']).cumsum()).groups
[
fill_first_base_price_of_above_one_window(df, indices)
for group_, indices in window_indices.items()
if set(indices).issubset(above_one_indices)
]
pprint(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment