SIENTIAPDE-1646

Simplify merge in retrain: expose timestamp explicitly for join

Refactor evaluation_data merge to reset both frames' index into a
"timestamp" column before joining, instead of merging on index then
copying it out afterward. Same validate='one_to_one' safety check,
fewer intermediate steps.
This commit is contained in:
2026-09-25 12:45:19 -03:00
parent f323da7b56
commit d27f6573fe

View File

@@ -531,23 +531,19 @@ class MLFlow(SientiaMonitoring):
# Keep heavy model fitting outside MLflow run timing. # Keep heavy model fitting outside MLflow run timing.
prediction_data = wrapper.retrain(data).rename(columns={target: 'prediction'}) prediction_data = wrapper.retrain(data).rename(columns={target: 'prediction'})
# Merge prediction data with retrain data. Both sides are indexed by the pivoted # Both frames are indexed by the pivoted timestamp: expose it as an explicit column
# timestamp, so one row per timestamp on each side; `validate` fails loudly instead # and join on it. `validate` fails loudly instead of silently multiplying rows if the
# of silently multiplying rows if the wrapper ever returns duplicated timestamps. # wrapper ever returns duplicated timestamps.
evaluation_data = pd.merge( evaluation_data = (
data, data.reset_index(names='timestamp')
prediction_data, .merge(
left_index=True, prediction_data.reset_index(names='timestamp'),
right_index=True, on='timestamp',
how='left', how='left',
validate='one_to_one', validate='one_to_one',
) )
.rename(columns={target: 'target'})
# Rename target column to "target", then move the index into a "timestamp" column .sort_values(by='timestamp', ascending=True, ignore_index=True)
evaluation_data = evaluation_data.rename(columns={target: 'target'})
evaluation_data['timestamp'] = evaluation_data.index
evaluation_data = evaluation_data.reset_index(drop=True).sort_values(
by='timestamp', ascending=True
) )
run_name = f'{model_name}-retrain-{datetime.now().strftime("%Y%m%d%H%M%S")}' run_name = f'{model_name}-retrain-{datetime.now().strftime("%Y%m%d%H%M%S")}'