fix(yahoo): clean OHLCV frames on their own copy

- dropping undated rows copies the frame before prices are coerced, so no chained-assignment write
This commit is contained in:
Yijia-Xiao
2026-09-24 05:00:36 +00:00
parent 4187716842
commit b176c9374b
2 changed files with 12 additions and 1 deletions
+11
View File
@@ -6,6 +6,8 @@ instead of `Date`, which would otherwise silently drop every indicator.
from __future__ import annotations
import warnings
import pandas as pd
import pytest
@@ -68,3 +70,12 @@ class TestCleanDataframeAcrossVersions:
df["close_5_sma"] # triggers calculation
assert "close_5_sma" in df.columns
assert df["close_5_sma"].notna().any()
@pytest.mark.unit
def test_cleaning_a_frame_with_undated_rows_writes_to_its_own_copy():
raw = pd.DataFrame({"Date": ["2026-01-08", None, "2026-01-09"], "Close": ["1", "2", "x"]})
with warnings.catch_warnings():
warnings.simplefilter("error")
cleaned = ohlcv._clean_dataframe(raw)
assert cleaned["Close"].tolist()[0] == 1.0
+1 -1
View File
@@ -105,7 +105,7 @@ def _clean_dataframe(data: pd.DataFrame) -> pd.DataFrame:
the latest in-range bar (#1201)."""
data = _ensure_date_column(data)
data["Date"] = _normalize_dates(data["Date"])
data = data.dropna(subset=["Date"])
data = data.dropna(subset=["Date"]).copy()
price_cols = [c for c in ["Open", "High", "Low", "Close", "Volume"] if c in data.columns]
data[price_cols] = data[price_cols].apply(pd.to_numeric, errors="coerce")