Skip to content

Commit

Permalink
io: remove pandas based io for lp writing, use polars instead
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianHofmann committed Oct 21, 2024
1 parent 40a27f9 commit 83c43db
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 332 deletions.
1 change: 1 addition & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Upcoming Version
----------------

* When writing out an LP file, large variables and constraints are now chunked to avoid memory issues. This is especially useful for large models with constraints with many terms. The chunk size can be set with the `slice_size` argument in the `solve` function.
* To achieve better performance, the LP file writing is now using the `polars` package per default. Setting `io_api` to `lp-polars` is therefore deprecated, as the standard `io_api=lp` uses the `polars` package. The user should see no difference from this change but faster lp file writing. The previous `pandas` based implementation was removed.

Version 0.3.15
--------------
Expand Down
21 changes: 16 additions & 5 deletions linopy/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,24 @@ def check_has_nulls_polars(df: pl.DataFrame, name: str = "") -> None:
Raises:
------
ValueError: If the DataFrame contains null values,
a ValueError is raised with a message indicating the name of the constraint and the fields containing null values.
ValueError: If the DataFrame contains null values or NaN values,
a ValueError is raised with a message indicating the name of the constraint and the fields containing nulls.
"""
has_nulls = df.select(pl.col("*").is_null().any())
null_columns = [col for col in has_nulls.columns if has_nulls[col][0]]
null_check = df.select(
[
(
pl.col(col).is_null()
| (pl.col(col).is_nan() if dtype == pl.Float64 else False)
)
.any()
.alias(col)
for col, dtype in zip(df.columns, df.dtypes)
]
)

null_columns = [col for col in null_check.columns if null_check[col][0]]
if null_columns:
raise ValueError(f"{name} contains nan's in field(s) {null_columns}")
raise ValueError(f"{name} contains null/nan values in field(s) {null_columns}")


def filter_nulls_polars(df: pl.DataFrame) -> pl.DataFrame:
Expand Down
Loading

0 comments on commit 83c43db

Please sign in to comment.