Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid destroying MultiIndex objects in dequantify #168

Merged
merged 4 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ What's new
By `Justus Magin <https://github.com/keewis>`_.
- add support for python 3.10 (:pull:`155`)
By `Justus Magin <https://github.com/keewis>`_.
- preserve :py:class:`pandas.MultiIndex` objects (:issue:`164`, :pull:`168`).
By `Justus Magin <https://github.com/keewis>`_.

0.2.1 (26 Jul 2021)
-------------------
Expand Down
3 changes: 3 additions & 0 deletions pint_xarray/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ def extract_unit_attributes(obj, attr="units"):


def strip_units_variable(var):
if not isinstance(var.data, pint.Quantity):
return var

data = array_strip_units(var.data)
return var.copy(data=data)

Expand Down
12 changes: 12 additions & 0 deletions pint_xarray/tests/test_accessors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pandas as pd
import pint
import pytest
import xarray as xr
Expand Down Expand Up @@ -174,6 +175,17 @@ def test_roundtrip_data(self, example_unitless_da):
result = quantified.pint.dequantify()
assert_equal(result, orig)

def test_multiindex(self):
mindex = pd.MultiIndex.from_product([["a", "b"], [1, 2]], names=("lat", "lon"))

da = xr.DataArray(
np.arange(len(mindex)), dims="multi", coords={"multi": mindex}
)
result = da.pint.dequantify()

xr.testing.assert_identical(da, result)
assert isinstance(result.indexes["multi"], pd.MultiIndex)


class TestPropertiesDataArray:
def test_magnitude_getattr(self, example_quantity_da):
Expand Down