print(datadf) with tqdm(total=len(factordf)) as pbar: def calcfactor(window):…
- User
print(data_df) with tqdm(total=len(factor_df)) as pbar: def calc_factor(window): pbar.update(1) if window.isnull().sum() < 15: return np.nan return window.std()
def calc_series(series): return series.rolling(window=31, min_periods=15).apply(calc_factor) data_df["68_volatility_monthly_raw"] = data_df.groupby("stock_id")["daily_return"].transform(lambda x: calc_series(x))Add a condition for function "calc_factor": If the number of values 0 is greater than 10, then return np.nan
Assistant
To add the condition for the function "calc_factor", you can modify the lambda function inside the "transform" method. Here's the modified code:
import numpy as np from tqdm import tqdm # ... (previous code) data_df["68_volatility_monthly_raw"] = data_df.groupby("stock_id")["daily_return"].transform(lambda x: calc_series(x)) def calc_factor(window): if np.sum(window == 0) > 10: return np.nan else: if window.isnull().sum() < 15: return np.nan return window.std() def calc_series(series): return series.rolling(window=31, min_periods=15).apply(calc_factor) # ... (previous code)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.