Skip to content

Commit d70472c

Browse files
add __pandas_priority__ to linopy objects (#389)
1 parent aeba9d3 commit d70472c

File tree

5 files changed

+9
-1
lines changed

5 files changed

+9
-1
lines changed

doc/release_notes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ Release Notes
44
Upcoming Version
55
----------------
66

7+
* Added a pandas priority attribute. With this change, the operation with pandas objects is now prioritizing linopy objects over pandas objects. This is useful when the using linopy objects in arithmetic operations with pandas objects, e.g. `a * x` where `a` is a pandas Series/DataFrame and `x` is a linopy variable.
78
* The method :meth:`model.to_file <linopy.model.Model.to_file>` now includes a progress argument to enable or disable the progress bar while writing.
89

9-
1010
Version 0.4.2
1111
--------------
1212

linopy/expressions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ class LinearExpression:
344344
__slots__ = ("_data", "_model")
345345
__array_ufunc__ = None
346346
__array_priority__ = 10000
347+
__pandas_priority__ = 10000
347348

348349
_fill_value = FILL_VALUE
349350

@@ -1485,6 +1486,7 @@ class QuadraticExpression(LinearExpression):
14851486
__slots__ = ("_data", "_model")
14861487
__array_ufunc__ = None
14871488
__array_priority__ = 10000
1489+
__pandas_priority__ = 10000
14881490

14891491
_fill_value = {"vars": -1, "coeffs": np.nan, "const": np.nan}
14901492

linopy/objective.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class Objective:
5353
__slots__ = ("_expression", "_model", "_sense", "_value")
5454
__array_ufunc__ = None
5555
__array_priority__ = 10000
56+
__pandas_priority__ = 10000
5657

5758
_fill_value = {"vars": -1, "coeffs": np.nan, "const": 0}
5859

linopy/variables.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ class Variable:
143143

144144
__slots__ = ("_data", "_model")
145145
__array_ufunc__ = None
146+
__array_priority__ = 10000
147+
__pandas_priority__ = 10000
146148

147149
_fill_value = FILL_VALUE
148150

test/test_linear_expression.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ def test_linear_expression_with_multiplication(x):
199199
expr = xr.DataArray(np.array([[1, 2], [2, 3]])) * x
200200
assert isinstance(expr, LinearExpression)
201201

202+
expr = pd.Series([1, 2], index=pd.RangeIndex(2, name="dim_0")) * x
203+
assert isinstance(expr, LinearExpression)
204+
202205

203206
def test_linear_expression_with_addition(m, x, y):
204207
expr = 10 * x + y

0 commit comments

Comments
 (0)