preprocessing.imputation.custom_weights(index, weights_series)
Return 0 if index is in or near any gap.
Parameters
| index |
pd.Index |
The index to check. |
required |
| weights_series |
pd.Series |
Series containing weights. |
required |
Returns
| float |
float |
The weight corresponding to the index. |
Examples
import numpy as np
import pandas as pd
from spotforecast2_safe.preprocessing.imputation import custom_weights
# Build a small synthetic weights Series with a DatetimeIndex
idx = pd.date_range("2024-01-01", periods=5, freq="h")
weights_series = pd.Series([1.0, 1.0, 0.0, 0.0, 1.0], index=idx)
# Single-index scalar lookup
w = custom_weights(idx[0], weights_series)
print(f"Index: {idx[0]}, Weight: {w}")
assert w == 1.0
# pd.Index slice lookup returns a numpy array
result = custom_weights(idx[:3], weights_series)
print(result)
assert np.allclose(result, [1.0, 1.0, 0.0])
Index: 2024-01-01 00:00:00, Weight: 1.0
[1. 1. 0.]