skills/tabular/last-diff-lag-features/SKILL.md
Compute first-order difference between last and second-to-last rows per entity in panel data to capture recent trend direction and magnitude
npx skillsauth add wenmin-wu/ds-skills tabular-last-diff-lag-featuresInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
For panel/sequential data where each entity has multiple time-stamped rows, compute the first-order difference between the last two observations per entity. This captures the recent trend — whether a feature is increasing or decreasing — which is often more predictive than the raw value for tasks like credit default or churn prediction.
import pandas as pd
import numpy as np
def last_diff_features(df, group_col, num_features):
"""Compute last-minus-previous difference per entity.
Args:
df: DataFrame sorted by (group_col, time), multiple rows per entity
group_col: entity identifier column
num_features: list of numeric columns to diff
"""
diffs = []
ids = []
for entity_id, group in df.groupby(group_col):
diff = group[num_features].diff(1).iloc[[-1]].values
diffs.append(diff)
ids.append(entity_id)
result = pd.DataFrame(
np.concatenate(diffs, axis=0),
columns=[f'{c}_diff1' for c in num_features],
index=ids
)
result.index.name = group_col
return result
groupby().transform('diff').groupby().tail(1) avoids the Python loopdata-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF