Skip to content
Open
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
20 changes: 14 additions & 6 deletions .github/workflows/build-and-deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ concurrency:
cancel-in-progress: true
permissions:
contents: read
pages: write
id-token: write
jobs:
docs:
name: Build docs
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/checkout@v4
- name: Setup
Expand All @@ -41,10 +36,23 @@ jobs:
uses: actions/upload-pages-artifact@v3
with:
path: docs/_build/html

deploy:
name: Deploy docs
if: startsWith(github.ref, 'refs/tags/v') && github.event_name == 'push'
needs: docs
runs-on: ubuntu-latest
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Deploy to GitHub Pages
if: startsWith(github.ref, 'refs/tags/v') && github.event_name == 'push'
id: deployment
uses: actions/deploy-pages@v4

doctest:
name: Run doctests
runs-on: ubuntu-latest
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/ci-lint-typecheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ jobs:
- uses: actions/checkout@v6

- name: Ruff check
uses: astral-sh/ruff-action@v3
uses: astral-sh/ruff-action@v4.1.0
with:
version-file: uv.lock
args: "check"

- name: Ruff format (check)
uses: astral-sh/ruff-action@v3
uses: astral-sh/ruff-action@v4.1.0
with:
version-file: uv.lock
args: "format --check"

typecheck:
Expand Down
14 changes: 11 additions & 3 deletions src/backtest_lib/market/polars_impl/_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,20 @@ def to_series(
# the scalar types are properly kept track of.
def _rhs(
self, other: VectorOps[Scalar] | ScalarU
) -> tuple[pl.Series | T, type[ScalarU]]:
) -> tuple[pl.Series | ScalarU, type[ScalarU]]:
if isinstance(other, (int, float)):
return self._scalar_type(other), self._scalar_type
scalar_type = (
float if self._scalar_type is float or isinstance(other, float) else int
)
return other, scalar_type
if isinstance(other, PolarsTimeseries):
if other._axis is self._axis or other._axis.labels == self._axis.labels:
return other._vec, other._scalar_type
scalar_type = (
float
if self._scalar_type is float or other._scalar_type is float
else int
)
return other._vec, scalar_type
raise ValueError("Axis mismatch: operations require identical PeriodAxis.")
raise TypeError("Only scalars or same-axis PolarsTimeseries are supported.")

Expand Down
9 changes: 9 additions & 0 deletions tests/unit/market/polars_impl/test_polars_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ def test_arithmetic_with_timeseries(small_timeseries, timeseries_type) -> None:
assert combined.to_series().to_list() == [11.0, 22.0, 33.0]


def test_arithmetic_promotes_mixed_numeric_types(timeseries_type) -> None:
periods = [np.datetime64("2024-01-01"), np.datetime64("2024-01-02")]
ints = timeseries_type.from_vectors([1, 2], periods)
floats = timeseries_type.from_vectors([0.5, 1.5], periods)

assert (ints + 0.5).to_series().to_list() == [1.5, 2.5]
assert (floats + ints).to_series().to_list() == [1.5, 3.5]


def test_axis_mismatch_raises(timeseries_type) -> None:
left = timeseries_type.from_vectors(
values=[1.0, 2.0],
Expand Down
Loading