Skip to content

Commit de2257b

Browse files
authored
keep data units in interp / interp_like (#76)
* add tests to make sure interp properly keeps units on data * make sure we don't try to attach None as units * also attach back the data units * remove pytest-azurepipelines from the CI * update whats-new.rst [skip-ci] * only assume a units attribute if the units are not None
1 parent de4a4a2 commit de2257b

File tree

6 files changed

+113
-8
lines changed

6 files changed

+113
-8
lines changed

ci/requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ black
77
flake8
88
pytest
99
pytest-cov
10-
pytest-azurepipelines

docs/whats-new.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ What's new
2929
:py:meth:`DataArray.pint.reindex` and :py:meth:`DataArray.pint.reindex_like` (:pull:`69`).
3030
By `Justus Magin <https://github.com/keewis>`_.
3131
- implement :py:meth:`Dataset.pint.interp`, :py:meth:`Dataset.pint.interp_like`,
32-
:py:meth:`DataArray.pint.interp` and :py:meth:`DataArray.pint.interp_like` (:pull:`72`).
32+
:py:meth:`DataArray.pint.interp` and :py:meth:`DataArray.pint.interp_like` (:pull:`72`, :pull:`76`).
3333
By `Justus Magin <https://github.com/keewis>`_.
3434

3535
v0.1 (October 26 2020)

pint_xarray/accessors.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ def interp(
595595

596596
# convert the indexes to the indexer's units
597597
converted = conversion.convert_units(self.da, indexer_units)
598+
units = conversion.extract_units(converted)
598599
stripped = conversion.strip_units(converted)
599600

600601
# index
@@ -608,7 +609,7 @@ def interp(
608609
assume_sorted=False,
609610
kwargs=None,
610611
)
611-
return conversion.attach_units(interpolated, indexer_units)
612+
return conversion.attach_units(interpolated, units)
612613

613614
def interp_like(self, other, method="linear", assume_sorted=False, kwargs=None):
614615
"""unit-aware version of interp_like
@@ -652,14 +653,15 @@ def interp_like(self, other, method="linear", assume_sorted=False, kwargs=None):
652653
raise DimensionalityError(units1, units2)
653654

654655
converted = conversion.convert_units(self.da, indexer_units)
656+
units = conversion.extract_units(converted)
655657
stripped = conversion.strip_units(converted)
656658
interpolated = stripped.interp_like(
657659
other,
658660
method=method,
659661
assume_sorted=assume_sorted,
660662
kwargs=kwargs,
661663
)
662-
return conversion.attach_units(interpolated, indexer_units)
664+
return conversion.attach_units(interpolated, units)
663665

664666
def sel(
665667
self, indexers=None, method=None, tolerance=None, drop=False, **indexers_kwargs
@@ -1185,6 +1187,7 @@ def interp(
11851187

11861188
# convert the indexes to the indexer's units
11871189
converted = conversion.convert_units(self.ds, indexer_units)
1190+
units = conversion.extract_units(converted)
11881191
stripped = conversion.strip_units(converted)
11891192

11901193
# index
@@ -1198,7 +1201,7 @@ def interp(
11981201
assume_sorted=False,
11991202
kwargs=None,
12001203
)
1201-
return conversion.attach_units(interpolated, indexer_units)
1204+
return conversion.attach_units(interpolated, units)
12021205

12031206
def interp_like(self, other, method="linear", assume_sorted=False, kwargs=None):
12041207
"""unit-aware version of interp_like
@@ -1242,14 +1245,15 @@ def interp_like(self, other, method="linear", assume_sorted=False, kwargs=None):
12421245
raise DimensionalityError(units1, units2)
12431246

12441247
converted = conversion.convert_units(self.ds, indexer_units)
1248+
units = conversion.extract_units(converted)
12451249
stripped = conversion.strip_units(converted)
12461250
interpolated = stripped.interp_like(
12471251
other,
12481252
method=method,
12491253
assume_sorted=assume_sorted,
12501254
kwargs=kwargs,
12511255
)
1252-
return conversion.attach_units(interpolated, indexer_units)
1256+
return conversion.attach_units(interpolated, units)
12531257

12541258
def sel(
12551259
self, indexers=None, method=None, tolerance=None, drop=False, **indexers_kwargs

pint_xarray/conversion.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ def array_strip_units(data):
9393
def attach_units_variable(variable, units):
9494
if isinstance(variable, IndexVariable):
9595
new_obj = variable.copy()
96-
new_obj.attrs[unit_attribute_name] = units
96+
if units is not None:
97+
new_obj.attrs[unit_attribute_name] = units
9798
elif isinstance(variable, Variable):
9899
new_data = array_attach_units(variable.data, units)
99100
new_obj = variable.copy(data=new_data)

pint_xarray/tests/test_accessors.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,28 @@ def test_reindex_like(obj, other, expected, error):
731731
DimensionalityError,
732732
id="Dataset-incompatible units",
733733
),
734+
pytest.param(
735+
xr.Dataset(
736+
{
737+
"a": (("x", "y"), Quantity([[0, 1], [2, 3], [4, 5]], "kg")),
738+
"x": [10, 20, 30],
739+
"y": [60, 120],
740+
}
741+
),
742+
{
743+
"x": [15, 25],
744+
"y": [75, 105],
745+
},
746+
xr.Dataset(
747+
{
748+
"a": (("x", "y"), Quantity([[1.25, 1.75], [3.25, 3.75]], "kg")),
749+
"x": [15, 25],
750+
"y": [75, 105],
751+
}
752+
),
753+
None,
754+
id="Dataset-data units",
755+
),
734756
pytest.param(
735757
xr.DataArray(
736758
[[0, 1], [2, 3], [4, 5]],
@@ -787,6 +809,30 @@ def test_reindex_like(obj, other, expected, error):
787809
DimensionalityError,
788810
id="DataArray-incompatible units",
789811
),
812+
pytest.param(
813+
xr.DataArray(
814+
Quantity([[0, 1], [2, 3], [4, 5]], "kg"),
815+
dims=("x", "y"),
816+
coords={
817+
"x": [10, 20, 30],
818+
"y": [60, 120],
819+
},
820+
),
821+
{
822+
"x": [15, 25],
823+
"y": [75, 105],
824+
},
825+
xr.DataArray(
826+
Quantity([[1.25, 1.75], [3.25, 3.75]], "kg"),
827+
dims=("x", "y"),
828+
coords={
829+
"x": [15, 25],
830+
"y": [75, 105],
831+
},
832+
),
833+
None,
834+
id="DataArray-data units",
835+
),
790836
),
791837
)
792838
def test_interp(obj, indexers, expected, error):
@@ -889,6 +935,30 @@ def test_interp(obj, indexers, expected, error):
889935
None,
890936
id="DataArray-identical units",
891937
),
938+
pytest.param(
939+
xr.Dataset(
940+
{
941+
"a": (("x", "y"), Quantity([[0, 1], [2, 3], [4, 5]], "kg")),
942+
"x": [10, 20, 30],
943+
"y": [60, 120],
944+
}
945+
),
946+
xr.Dataset(
947+
{
948+
"x": [15, 25],
949+
"y": [75, 105],
950+
}
951+
),
952+
xr.Dataset(
953+
{
954+
"a": (("x", "y"), Quantity([[1.25, 1.75], [3.25, 3.75]], "kg")),
955+
"x": [15, 25],
956+
"y": [75, 105],
957+
}
958+
),
959+
None,
960+
id="Dataset-data units",
961+
),
892962
pytest.param(
893963
xr.DataArray(
894964
[[0, 1], [2, 3], [4, 5]],
@@ -934,6 +1004,32 @@ def test_interp(obj, indexers, expected, error):
9341004
DimensionalityError,
9351005
id="DataArray-incompatible units",
9361006
),
1007+
pytest.param(
1008+
xr.DataArray(
1009+
Quantity([[0, 1], [2, 3], [4, 5]], "kg"),
1010+
dims=("x", "y"),
1011+
coords={
1012+
"x": [10, 20, 30],
1013+
"y": [60, 120],
1014+
},
1015+
),
1016+
xr.Dataset(
1017+
{
1018+
"x": [15, 25],
1019+
"y": [75, 105],
1020+
}
1021+
),
1022+
xr.DataArray(
1023+
Quantity([[1.25, 1.75], [3.25, 3.75]], "kg"),
1024+
dims=("x", "y"),
1025+
coords={
1026+
"x": [15, 25],
1027+
"y": [75, 105],
1028+
},
1029+
),
1030+
None,
1031+
id="DataArray-data units",
1032+
),
9371033
),
9381034
)
9391035
def test_interp_like(obj, other, expected, error):

pint_xarray/tests/test_conversion.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,16 @@ def test_attach_units(self, type, units):
226226
q_b = to_quantity(b, units.get("b"))
227227
q_u = to_quantity(u, units.get("u"))
228228

229+
units_x = units.get("x")
230+
229231
obj = Dataset({"a": ("x", a), "b": ("x", b)}, coords={"u": ("x", u), "x": x})
230232
expected = Dataset(
231233
{"a": ("x", q_a), "b": ("x", q_b)},
232-
coords={"u": ("x", q_u), "x": ("x", x, {"units": units.get("x")})},
234+
coords={"u": ("x", q_u), "x": x},
233235
)
236+
if units_x is not None:
237+
expected.x.attrs["units"] = units_x
238+
234239
if type == "DataArray":
235240
obj = obj["a"]
236241
expected = expected["a"]

0 commit comments

Comments
 (0)