Skip to content

Commit

Permalink
Setup project to support multiple versions of polars
Browse files Browse the repository at this point in the history
  • Loading branch information
Santiago Roig committed Oct 31, 2024
1 parent 522f48d commit f76fbf8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
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,<1.13", "numpy", "pyarrow", "pandas"]

[project.optional-dependencies]
dev = [
Expand Down
13 changes: 11 additions & 2 deletions src/pyoframe/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@
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

import polars as pl
from packaging import version

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

COEF_KEY = "__coeff"
VAR_KEY = "__variable_id"
Expand Down
32 changes: 27 additions & 5 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 < version.parse("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 @@ -1258,7 +1271,10 @@ def __repr__(self):
)

def to_expr(self) -> Expression:
return self._new(self.data.drop(SOLUTION_KEY))
if POLARS_VERSION < version.parse("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 < version.parse("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)

0 comments on commit f76fbf8

Please sign in to comment.