Skip to content

Commit d3e9585

Browse files
Pass kwargs and assume_sorted to xarray interp for both DataArray and Dataset (#270)
* pass kwargs and assume_sorted to xarray interp for both DataArray and Dataset * update docstring, whats-new and adapt interp test to test for kwarg passing * Explicit dict for quantify Co-authored-by: Justus Magin <[email protected]> * move kwargs test to separate parameter set --------- Co-authored-by: Justus Magin <[email protected]>
1 parent 20a9301 commit d3e9585

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

docs/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ What's new
88
By `Justus Magin <https://github.com/keewis>`_.
99
- create a `PintIndex` to allow units on indexed coordinates (:pull:`163`, :issue:`162`)
1010
By `Justus Magin <https://github.com/keewis>`_ and `Benoit Bovy <https://github.com/benbovy>`_.
11+
- fix :py:meth:`Dataset.pint.interp` and :py:meth:`DataArray.pint.interp` bug
12+
failing to pass through arguments (:pull:`270`, :issue:`267`)
13+
By `Martijn van der Marel <https://github.com/martijnvandermarel>`_
1114

1215
0.4 (23 Jun 2024)
1316
-----------------

pint_xarray/accessors.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,8 @@ def interp(
716716
converted to the units of the indexers first.
717717
718718
.. note::
719-
``kwargs`` is passed unmodified to ``DataArray.interp``
719+
``method``, ``assume_sorted`` and ``kwargs`` are passed unmodified to
720+
``DataArray.interp``.
720721
721722
See Also
722723
--------
@@ -742,8 +743,8 @@ def interp(
742743
interpolated = stripped.interp(
743744
stripped_indexers,
744745
method=method,
745-
assume_sorted=False,
746-
kwargs=None,
746+
assume_sorted=assume_sorted,
747+
kwargs=kwargs,
747748
)
748749
return conversion.attach_units(interpolated, units)
749750

@@ -754,7 +755,8 @@ def interp_like(self, other, method="linear", assume_sorted=False, kwargs=None):
754755
to the units of the indexers first.
755756
756757
.. note::
757-
``kwargs`` is passed unmodified to ``DataArray.interp``
758+
``method``, ``assume_sorted`` and ``kwargs`` are passed unmodified to
759+
``DataArray.interp``.
758760
759761
See Also
760762
--------
@@ -1483,7 +1485,8 @@ def interp(
14831485
to the units of the indexers first.
14841486
14851487
.. note::
1486-
``kwargs`` is passed unmodified to ``Dataset.interp``
1488+
``method``, ``assume_sorted`` and ``kwargs`` are passed unmodified to
1489+
``DataArray.interp``.
14871490
14881491
See Also
14891492
--------
@@ -1509,8 +1512,8 @@ def interp(
15091512
interpolated = stripped.interp(
15101513
stripped_indexers,
15111514
method=method,
1512-
assume_sorted=False,
1513-
kwargs=None,
1515+
assume_sorted=assume_sorted,
1516+
kwargs=kwargs,
15141517
)
15151518
return conversion.attach_units(interpolated, units)
15161519

@@ -1521,7 +1524,8 @@ def interp_like(self, other, method="linear", assume_sorted=False, kwargs=None):
15211524
converted to the units of the indexers first.
15221525
15231526
.. note::
1524-
``kwargs`` is passed unmodified to ``Dataset.interp``
1527+
``method``, ``assume_sorted`` and ``kwargs`` are passed unmodified to
1528+
``DataArray.interp``.
15251529
15261530
See Also
15271531
--------

pint_xarray/tests/test_accessors.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
14371437

14381438
@requires_scipy
14391439
@pytest.mark.parametrize(
1440-
["obj", "units", "indexers", "expected", "expected_units", "error"],
1440+
["obj", "units", "indexers", "expected", "expected_units", "error", "kwargs"],
14411441
(
14421442
pytest.param(
14431443
xr.Dataset({"x": ("x", [10, 20, 30]), "y": ("y", [60, 120])}),
@@ -1446,6 +1446,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
14461446
xr.Dataset({"x": ("x", [10, 30, 50]), "y": ("y", [0, 120, 240])}),
14471447
{"x": "dm", "y": "s"},
14481448
None,
1449+
None,
14491450
id="Dataset-identical units",
14501451
),
14511452
pytest.param(
@@ -1455,6 +1456,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
14551456
xr.Dataset({"x": ("x", [0, 1, 3, 5]), "y": ("y", [0, 2, 4])}),
14561457
{"x": "m", "y": "min"},
14571458
None,
1459+
None,
14581460
id="Dataset-compatible units",
14591461
),
14601462
pytest.param(
@@ -1464,6 +1466,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
14641466
None,
14651467
{},
14661468
ValueError,
1469+
None,
14671470
id="Dataset-incompatible units",
14681471
),
14691472
pytest.param(
@@ -1488,6 +1491,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
14881491
),
14891492
{"a": "kg"},
14901493
None,
1494+
None,
14911495
id="Dataset-data units",
14921496
),
14931497
pytest.param(
@@ -1505,6 +1509,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
15051509
),
15061510
{"x": "dm", "y": "s"},
15071511
None,
1512+
None,
15081513
id="DataArray-identical units",
15091514
),
15101515
pytest.param(
@@ -1522,6 +1527,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
15221527
),
15231528
{"x": "m", "y": "min"},
15241529
None,
1530+
None,
15251531
id="DataArray-compatible units",
15261532
),
15271533
pytest.param(
@@ -1535,6 +1541,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
15351541
None,
15361542
{},
15371543
ValueError,
1544+
None,
15381545
id="DataArray-incompatible units",
15391546
),
15401547
pytest.param(
@@ -1552,20 +1559,39 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
15521559
),
15531560
{None: "kg"},
15541561
None,
1562+
None,
15551563
id="DataArray-data units",
15561564
),
1565+
pytest.param(
1566+
xr.DataArray(
1567+
[[0, 1], [2, 3], [4, 5]],
1568+
dims=("x", "y"),
1569+
coords={"x": ("x", [10, 20, 30]), "y": ("y", [60, 120])},
1570+
),
1571+
{"x": "dm", "y": "s"},
1572+
{"x": Quantity([1, 3, 5], "m"), "y": Quantity([0, 2], "min")},
1573+
xr.DataArray(
1574+
[[0, 1], [0, 5], [0, 0]],
1575+
dims=("x", "y"),
1576+
coords={"x": ("x", [1, 3, 5]), "y": ("y", [0, 2])},
1577+
),
1578+
{"x": "m", "y": "min"},
1579+
None,
1580+
{"bounds_error": False, "fill_value": 0},
1581+
id="DataArray-other parameters",
1582+
),
15571583
),
15581584
)
1559-
def test_interp(obj, units, indexers, expected, expected_units, error):
1585+
def test_interp(obj, units, indexers, expected, expected_units, error, kwargs):
15601586
obj_ = obj.pint.quantify(units)
15611587

15621588
if error is not None:
15631589
with pytest.raises(error):
1564-
obj.pint.interp(indexers)
1590+
obj_.pint.interp(indexers, kwargs=kwargs)
15651591
else:
15661592
expected_ = expected.pint.quantify(expected_units)
15671593

1568-
actual = obj_.pint.interp(indexers)
1594+
actual = obj_.pint.interp(indexers, kwargs=kwargs)
15691595
assert_units_equal(actual, expected_)
15701596
assert_identical(actual, expected_)
15711597

0 commit comments

Comments
 (0)