Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 16 additions & 0 deletions scripts/verify_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 42 additions & 0 deletions scripts/verify_yahoo_quickstart.py
Original file line number Diff line number Diff line change
@@ -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()
11 changes: 2 additions & 9 deletions src/ml4t/data/providers/yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading