Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup project to support multiple versions of polars #72

Merged
merged 6 commits into from
Nov 5, 2024
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
18 changes: 14 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,33 @@ on:
jobs:
run:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9"]
polars-version: ["1.12.0"]
include:
- python-version: "3.8"
polars-version: "0.20.27"
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python 3.8
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: '3.8'
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: pip install --editable .[dev]
run: |
pip install --editable .[dev]
pip install polars==${{ matrix.polars-version }}
- name: Run tests and collect coverage
run: pytest --cov
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4-beta
with:
name: coverage-polars-${{ matrix.polars-version }}
flags: smart-tests
verbose: true
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ tmp
.cache
docs/reference
dist
*.log
*.log
.idea/
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ classifiers = [
"License :: OSI Approved :: MIT License",
"Natural Language :: English",
]
dependencies = ["polars==0.20.27", "numpy", "pyarrow", "pandas"]
dependencies = ["polars>=0.20,<2", "numpy", "pyarrow", "pandas"]

[project.optional-dependencies]
dev = [
Expand Down
7 changes: 6 additions & 1 deletion src/pyoframe/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
MIT License
"""

import importlib.metadata
import typing
from dataclasses import dataclass
from enum import Enum
import typing
from typing import Literal, Optional, Union

import polars as pl
from packaging import version

# We want to try and support multiple major versions of polars
POLARS_VERSION = version.parse(importlib.metadata.version("polars"))

COEF_KEY = "__coeff"
VAR_KEY = "__variable_id"
Expand Down
34 changes: 28 additions & 6 deletions src/pyoframe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@

import pandas as pd
import polars as pl
from packaging import version

from pyoframe._arithmetic import _add_expressions, _get_dimensions
from pyoframe.constants import (
COEF_KEY,
CONST_TERM,
CONSTRAINT_KEY,
DUAL_KEY,
POLARS_VERSION,
RC_COL,
RESERVED_COL_KEYS,
SLACK_COL,
Expand Down Expand Up @@ -271,7 +273,18 @@ def _set_to_polars(set: "SetTypes") -> pl.DataFrame:
elif isinstance(set, Constraint):
df = set.data.select(set.dimensions_unsafe)
elif isinstance(set, SupportsMath):
df = set.to_expr().data.drop(RESERVED_COL_KEYS).unique(maintain_order=True)
if POLARS_VERSION.major < 1:
df = (
set.to_expr()
.data.drop(RESERVED_COL_KEYS)
.unique(maintain_order=True)
)
else:
df = (
set.to_expr()
.data.drop(RESERVED_COL_KEYS, strict=False)
.unique(maintain_order=True)
)
elif isinstance(set, pd.Index):
df = pl.from_pandas(pd.DataFrame(index=set).reset_index())
elif isinstance(set, pd.DataFrame):
Expand Down Expand Up @@ -601,7 +614,7 @@ def __mul__(
data = (
self.data.join(
multiplier,
on=dims_in_common,
on=dims_in_common if len(dims_in_common) > 0 else None,
how="inner" if dims_in_common else "cross",
)
.with_columns(pl.col(COEF_KEY) * pl.col(COEF_KEY + "_right"))
Expand Down Expand Up @@ -1258,7 +1271,10 @@ def __repr__(self):
)

def to_expr(self) -> Expression:
return self._new(self.data.drop(SOLUTION_KEY))
if POLARS_VERSION.major < 1:
return self._new(self.data.drop(SOLUTION_KEY))
else:
return self._new(self.data.drop(SOLUTION_KEY, strict=False))

def _new(self, data: pl.DataFrame):
e = Expression(data.with_columns(pl.lit(1.0).alias(COEF_KEY)))
Expand Down Expand Up @@ -1334,7 +1350,13 @@ def next(self, dim: str, wrap_around: bool = False) -> Expression:

expr = self.to_expr()
data = expr.data.rename({dim: "__prev"})
data = data.join(
wrapped, left_on="__prev", right_on="__next", how="inner"
).drop(["__prev", "__next"])

if POLARS_VERSION.major < 1:
data = data.join(
wrapped, left_on="__prev", right_on="__next", how="inner"
).drop(["__prev", "__next"])
else:
data = data.join(
wrapped, left_on="__prev", right_on="__next", how="inner"
).drop(["__prev", "__next"], strict=False)
return expr._new(data)
Loading