Quick Trick: Restore NAs After P123 Rank Downloads for Native Handling in XGBoost and LightGBM (at home with downloads).
When P123 exports ranked factors (in the [-1, 1] scale), all NAs are replaced with 0. Here’s a fast workaround:
df_rank[df_rank == 0] = np.nan
Why it works:
Only one non-NA stock can have a true rank of exactly 0. If you have many NAs, this single misclassified stock won’t matter — and your model (like XGBoost or LightGBM) can now use native NA handling (at home with the downloads).
Result:
One valid stock will be incorrectly marked as np.nan, but all true NAs will be correctly restored. A very small error — and a practical tradeoff if you want models to learn from missingness.