From 7e5d30b648c83da0727d00f8afb973055813b5a7 Mon Sep 17 00:00:00 2001 From: Stefan Jansen Date: Tue, 24 Nov 2026 16:55:21 -0500 Subject: [PATCH] Fix Yahoo extra installation and preserve UTC in batch bars --- pyproject.toml | 2 ++ scripts/verify_distribution.py | 16 ++++++++++++ scripts/verify_yahoo_quickstart.py | 42 ++++++++++++++++++++++++++++++ src/ml4t/data/providers/yahoo.py | 11 ++------ uv.lock | 3 +++ 5 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 scripts/verify_yahoo_quickstart.py diff --git a/pyproject.toml b/pyproject.toml index 749fa2b..54b541e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -112,6 +112,8 @@ dependencies = [ # Provider-specific dependencies yahoo = [ "yfinance>=1.5.2", + "pyarrow>=14.0.0", + "pyarrow>=26.0.0; python_version >= '3.15'", ] databento = [ "databento>=0.38.0", diff --git a/scripts/verify_distribution.py b/scripts/verify_distribution.py index dbff0b4..52d8093 100644 --- a/scripts/verify_distribution.py +++ b/scripts/verify_distribution.py @@ -176,6 +176,22 @@ def verify_install(archive: Path, version: str) -> None: subprocess.run( [str(python), str(Path(__file__).with_name("run_readme_quickstart.py"))], check=True ) + subprocess.run( + [ + "uv", + "run", + "--isolated", + "--no-project", + "--python", + sys.executable, + "--with", + f"{archive}[yahoo]", + "python", + "-I", + str(Path(__file__).with_name("verify_yahoo_quickstart.py")), + ], + check=True, + ) subprocess.run( ["uv", "pip", "install", "--python", str(python), f"{archive}[oanda]"], check=True, diff --git a/scripts/verify_yahoo_quickstart.py b/scripts/verify_yahoo_quickstart.py new file mode 100644 index 0000000..6c9132e --- /dev/null +++ b/scripts/verify_yahoo_quickstart.py @@ -0,0 +1,42 @@ +"""Exercise single and batch conversion using only the installed Yahoo extra.""" + +from datetime import UTC, datetime +from unittest.mock import patch + +import pandas as pd +import polars as pl + +from ml4t.data.providers import YahooFinanceProvider + + +def main() -> None: + with YahooFinanceProvider() as provider: + for timezone, utc_hour in [(None, 0), ("America/New_York", 5)]: + index = pd.DatetimeIndex(["2025-11-03"], tz=timezone, name="Date") + frame = pd.DataFrame( + { + field: pd.array([value], dtype="Int64") + for field, value in [ + ("Open", 100), + ("High", 101), + ("Low", 99), + ("Close", 100), + ("Volume", 150), + ] + }, + index=index, + ) + multi = pd.concat({"SPY": frame, "QQQ": frame}, axis=1).swaplevel(axis=1) + with patch("yfinance.download", return_value=multi): + single = provider.fetch_ohlcv("SPY", "2025-11-03", "2025-11-03", "daily") + batch = provider.fetch_batch_ohlcv(["SPY", "QQQ"], "2025-11-03", "2025-11-03") + stamp = datetime(2025, 11, 3, utc_hour, tzinfo=UTC) + for data, symbols in [(single, ["SPY"]), (batch, ["QQQ", "SPY"])]: + assert data.schema["timestamp"] == pl.Datetime("us", "UTC"), data.schema + assert data["timestamp"].to_list() == [stamp] * len(symbols) + assert data["symbol"].to_list() == symbols + assert data["volume"].to_list() == [150.0] * len(symbols) + + +if __name__ == "__main__": + main() diff --git a/src/ml4t/data/providers/yahoo.py b/src/ml4t/data/providers/yahoo.py index 7e4ee2c..1d4df31 100644 --- a/src/ml4t/data/providers/yahoo.py +++ b/src/ml4t/data/providers/yahoo.py @@ -430,16 +430,9 @@ def _convert_to_polars(self, df_pandas: pd.DataFrame, symbol: str) -> pl.DataFra } ) - timestamp_type = df.schema["timestamp"] - timestamp = pl.col("timestamp") - if isinstance(timestamp_type, pl.Datetime) and timestamp_type.time_zone is not None: - timestamp = timestamp.dt.convert_time_zone("UTC") - else: - timestamp = timestamp.cast(pl.Datetime).dt.replace_time_zone("UTC") - return ( df.with_columns( - timestamp, + pl.col("timestamp").cast(pl.Datetime("us", "UTC")), pl.col("open").cast(pl.Float64), pl.col("high").cast(pl.Float64), pl.col("low").cast(pl.Float64), @@ -746,7 +739,7 @@ def _convert_batch_to_polars( # Cast to proper types BEFORE concat (ensures schema compatibility) df_symbol = df_symbol.with_columns( [ - pl.col("timestamp").cast(pl.Datetime), + pl.col("timestamp").cast(pl.Datetime("us", "UTC")), pl.col("open").cast(pl.Float64), pl.col("high").cast(pl.Float64), pl.col("low").cast(pl.Float64), diff --git a/uv.lock b/uv.lock index 82c350a..e81771b 100644 --- a/uv.lock +++ b/uv.lock @@ -1645,6 +1645,7 @@ oanda = [ { name = "six" }, ] yahoo = [ + { name = "pyarrow" }, { name = "yfinance" }, ] @@ -1734,9 +1735,11 @@ requires-dist = [ { name = "pyarrow", marker = "python_full_version >= '3.15' and extra == 'all'", specifier = ">=26.0.0" }, { name = "pyarrow", marker = "python_full_version >= '3.15' and extra == 'all-providers'", specifier = ">=26.0.0" }, { name = "pyarrow", marker = "python_full_version >= '3.15' and extra == 'databento'", specifier = ">=26.0.0" }, + { name = "pyarrow", marker = "python_full_version >= '3.15' and extra == 'yahoo'", specifier = ">=26.0.0" }, { name = "pyarrow", marker = "extra == 'all'", specifier = ">=14.0.0" }, { name = "pyarrow", marker = "extra == 'all-providers'", specifier = ">=14.0.0" }, { name = "pyarrow", marker = "extra == 'databento'", specifier = ">=14.0.0" }, + { name = "pyarrow", marker = "extra == 'yahoo'", specifier = ">=14.0.0" }, { name = "pybreaker", specifier = ">=1.0.0" }, { name = "pydantic", specifier = ">=2.12,<3" }, { name = "pydantic", marker = "python_full_version >= '3.15'", specifier = ">=2.14.1" },