Skip to content

Commit 2259bae

Browse files
committed
fix(labeling): keep timestamp-free legacy durations portable and unset (#94)
1 parent 9dcc1f6 commit 2259bae

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎src/ml4t/engineer/labeling/triple_barrier.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ def _build_labeling_result(
297297
) -> pl.DataFrame:
298298
"""Build the result DataFrame from labeling outputs."""
299299
calc_uniq = uniqueness is not None
300+
duration_series = (
301+
pl.Series(time_durations.tolist())
302+
if time_durations.dtype.kind == "O"
303+
else pl.Series(time_durations)
304+
)
300305

301306
if "event_time" in data.columns:
302307
label_dict: dict[str, Any] = {
@@ -306,7 +311,7 @@ def _build_labeling_result(
306311
"label_price": label_prices,
307312
"label_return": label_returns,
308313
"label_bars": bar_durations,
309-
"label_duration": time_durations,
314+
"label_duration": duration_series,
310315
"barrier_hit": barrier_hit,
311316
}
312317
if calc_uniq:
@@ -326,7 +331,7 @@ def _build_labeling_result(
326331
"label_price": pl.Series(label_prices, dtype=pl.Float64),
327332
"label_return": pl.Series(label_returns, dtype=pl.Float64),
328333
"label_bars": pl.Series(bar_durations, dtype=pl.Int64),
329-
"label_duration": pl.Series(time_durations),
334+
"label_duration": duration_series,
330335
"barrier_hit": pl.Series(barrier_hit, dtype=pl.String),
331336
}
332337
if calc_uniq:
@@ -365,7 +370,7 @@ def _triple_barrier_labels_single_group(
365370
duration_dtype = (
366371
pl.Duration(time_dtype.time_unit if isinstance(time_dtype, pl.Datetime) else "ms")
367372
if timestamp_col is not None
368-
else pl.Object
373+
else pl.Null
369374
)
370375
result = data.with_columns(
371376
label=pl.lit(None, dtype=pl.Int32),

‎tests/test_labeling_empty_schema.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,39 @@ def test_filtering_all_prices_still_produces_a_portable_typed_label_table(
4343
result.write_parquet(buffer)
4444
buffer.seek(0)
4545
assert pl.read_parquet(buffer).schema == result.schema
46+
47+
48+
@pytest.mark.parametrize("empty", [False, True])
49+
@pytest.mark.parametrize("grouped", [False, True])
50+
@pytest.mark.parametrize("events", ["dense", "sparse", "none"])
51+
def test_timestamp_free_legacy_labels_keep_unknown_duration_portable_without_changing_holding_bars(
52+
empty, grouped, events
53+
):
54+
prices = pl.DataFrame({"close": [100.0, 101.0, 102.0], "symbol": "A"})
55+
if not grouped:
56+
prices = prices.drop("symbol")
57+
if events != "dense":
58+
prices = prices.with_row_index("event_time").with_columns(
59+
pl.when(pl.col("event_time") % 2 == 0 if events == "sparse" else pl.col("close") > 1000)
60+
.then(pl.col("event_time"))
61+
.otherwise(None)
62+
.alias("event_time")
63+
)
64+
if empty:
65+
prices = prices.filter(pl.col("close") > 1000)
66+
config = LabelingConfig.triple_barrier(
67+
upper_barrier=None, lower_barrier=None, max_holding_period=1
68+
)
69+
result = triple_barrier_labels(prices, config)
70+
assert result.select(prices.columns).equals(prices)
71+
assert result.schema["label_duration"] == pl.Null
72+
assert result["label_duration"].is_null().all()
73+
if not empty:
74+
expected = {"dense": [1, 1, 0], "sparse": [1, None, 0], "none": [None] * 3}
75+
assert result["label_bars"].to_list() == expected[events]
76+
if events != "none":
77+
assert result["label_return"][0] == pytest.approx(0.01)
78+
buffer = BytesIO()
79+
result.write_parquet(buffer)
80+
buffer.seek(0)
81+
assert pl.read_parquet(buffer).equals(result)

0 commit comments

Comments
 (0)