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

change .pint.magnitude to return a DataArray #172

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 @@ -10,6 +10,8 @@ What's new
By `Justus Magin <https://github.com/keewis>`_.
- preserve :py:class:`pandas.MultiIndex` objects (:issue:`164`, :pull:`168`).
By `Justus Magin <https://github.com/keewis>`_.
- return a :py:class:`DataArray` from :py:attr:`DataArray.pint.magnitude` (:issue:`151`, :pull:`172`)
By `Justus Magin <https://github.com/keewis>`_.

0.2.1 (26 Jul 2021)
-------------------
Expand Down
3 changes: 1 addition & 2 deletions pint_xarray/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,7 @@ def dequantify(self, format=None):
@property
def magnitude(self):
"""the magnitude of the data or the data itself if not a quantity."""
data = self.da.data
return getattr(data, "magnitude", data)
return self.dequantify()

@property
def units(self):
Expand Down
5 changes: 3 additions & 2 deletions pint_xarray/tests/test_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,12 @@ class TestPropertiesDataArray:
def test_magnitude_getattr(self, example_quantity_da):
da = example_quantity_da
actual = da.pint.magnitude
assert not isinstance(actual, Quantity)
assert isinstance(actual, xr.DataArray)
assert not isinstance(actual.data, Quantity)

def test_magnitude_getattr_unitless(self, example_unitless_da):
da = example_unitless_da
xr.testing.assert_duckarray_equal(da.pint.magnitude, da.data)
xr.testing.assert_identical(da.pint.magnitude, da)

def test_units_getattr(self, example_quantity_da):
da = example_quantity_da
Expand Down