Skip to content

Commit

Permalink
Pass kwargs and assume_sorted to xarray interp for both `DataAr…
Browse files Browse the repository at this point in the history
…ray` 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 <keewis@users.noreply.github.com>

* move kwargs test to separate parameter set

---------

Co-authored-by: Justus Magin <keewis@users.noreply.github.com>
  • Loading branch information
martijnvandermarel and keewis authored Jul 28, 2024
1 parent 20a9301 commit d3e9585
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
3 changes: 3 additions & 0 deletions docs/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ What's new
By `Justus Magin <https://github.com/keewis>`_.
- create a `PintIndex` to allow units on indexed coordinates (:pull:`163`, :issue:`162`)
By `Justus Magin <https://github.com/keewis>`_ and `Benoit Bovy <https://github.com/benbovy>`_.
- fix :py:meth:`Dataset.pint.interp` and :py:meth:`DataArray.pint.interp` bug
failing to pass through arguments (:pull:`270`, :issue:`267`)
By `Martijn van der Marel <https://github.com/martijnvandermarel>`_

0.4 (23 Jun 2024)
-----------------
Expand Down
20 changes: 12 additions & 8 deletions pint_xarray/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,8 @@ def interp(
converted to the units of the indexers first.
.. note::
``kwargs`` is passed unmodified to ``DataArray.interp``
``method``, ``assume_sorted`` and ``kwargs`` are passed unmodified to
``DataArray.interp``.
See Also
--------
Expand All @@ -742,8 +743,8 @@ def interp(
interpolated = stripped.interp(
stripped_indexers,
method=method,
assume_sorted=False,
kwargs=None,
assume_sorted=assume_sorted,
kwargs=kwargs,
)
return conversion.attach_units(interpolated, units)

Expand All @@ -754,7 +755,8 @@ def interp_like(self, other, method="linear", assume_sorted=False, kwargs=None):
to the units of the indexers first.
.. note::
``kwargs`` is passed unmodified to ``DataArray.interp``
``method``, ``assume_sorted`` and ``kwargs`` are passed unmodified to
``DataArray.interp``.
See Also
--------
Expand Down Expand Up @@ -1483,7 +1485,8 @@ def interp(
to the units of the indexers first.
.. note::
``kwargs`` is passed unmodified to ``Dataset.interp``
``method``, ``assume_sorted`` and ``kwargs`` are passed unmodified to
``DataArray.interp``.
See Also
--------
Expand All @@ -1509,8 +1512,8 @@ def interp(
interpolated = stripped.interp(
stripped_indexers,
method=method,
assume_sorted=False,
kwargs=None,
assume_sorted=assume_sorted,
kwargs=kwargs,
)
return conversion.attach_units(interpolated, units)

Expand All @@ -1521,7 +1524,8 @@ def interp_like(self, other, method="linear", assume_sorted=False, kwargs=None):
converted to the units of the indexers first.
.. note::
``kwargs`` is passed unmodified to ``Dataset.interp``
``method``, ``assume_sorted`` and ``kwargs`` are passed unmodified to
``DataArray.interp``.
See Also
--------
Expand Down
34 changes: 30 additions & 4 deletions pint_xarray/tests/test_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,

@requires_scipy
@pytest.mark.parametrize(
["obj", "units", "indexers", "expected", "expected_units", "error"],
["obj", "units", "indexers", "expected", "expected_units", "error", "kwargs"],
(
pytest.param(
xr.Dataset({"x": ("x", [10, 20, 30]), "y": ("y", [60, 120])}),
Expand All @@ -1446,6 +1446,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
xr.Dataset({"x": ("x", [10, 30, 50]), "y": ("y", [0, 120, 240])}),
{"x": "dm", "y": "s"},
None,
None,
id="Dataset-identical units",
),
pytest.param(
Expand All @@ -1455,6 +1456,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
xr.Dataset({"x": ("x", [0, 1, 3, 5]), "y": ("y", [0, 2, 4])}),
{"x": "m", "y": "min"},
None,
None,
id="Dataset-compatible units",
),
pytest.param(
Expand All @@ -1464,6 +1466,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
None,
{},
ValueError,
None,
id="Dataset-incompatible units",
),
pytest.param(
Expand All @@ -1488,6 +1491,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
),
{"a": "kg"},
None,
None,
id="Dataset-data units",
),
pytest.param(
Expand All @@ -1505,6 +1509,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
),
{"x": "dm", "y": "s"},
None,
None,
id="DataArray-identical units",
),
pytest.param(
Expand All @@ -1522,6 +1527,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
),
{"x": "m", "y": "min"},
None,
None,
id="DataArray-compatible units",
),
pytest.param(
Expand All @@ -1535,6 +1541,7 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
None,
{},
ValueError,
None,
id="DataArray-incompatible units",
),
pytest.param(
Expand All @@ -1552,20 +1559,39 @@ def test_reindex_like(obj, units, other, other_units, expected, expected_units,
),
{None: "kg"},
None,
None,
id="DataArray-data units",
),
pytest.param(
xr.DataArray(
[[0, 1], [2, 3], [4, 5]],
dims=("x", "y"),
coords={"x": ("x", [10, 20, 30]), "y": ("y", [60, 120])},
),
{"x": "dm", "y": "s"},
{"x": Quantity([1, 3, 5], "m"), "y": Quantity([0, 2], "min")},
xr.DataArray(
[[0, 1], [0, 5], [0, 0]],
dims=("x", "y"),
coords={"x": ("x", [1, 3, 5]), "y": ("y", [0, 2])},
),
{"x": "m", "y": "min"},
None,
{"bounds_error": False, "fill_value": 0},
id="DataArray-other parameters",
),
),
)
def test_interp(obj, units, indexers, expected, expected_units, error):
def test_interp(obj, units, indexers, expected, expected_units, error, kwargs):
obj_ = obj.pint.quantify(units)

if error is not None:
with pytest.raises(error):
obj.pint.interp(indexers)
obj_.pint.interp(indexers, kwargs=kwargs)
else:
expected_ = expected.pint.quantify(expected_units)

actual = obj_.pint.interp(indexers)
actual = obj_.pint.interp(indexers, kwargs=kwargs)
assert_units_equal(actual, expected_)
assert_identical(actual, expected_)

Expand Down

0 comments on commit d3e9585

Please sign in to comment.