Skip to content

Commit

Permalink
BUG: fix converting a dimensionless array with unyt_array.to_astropy
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Aug 6, 2023
1 parent b0ff846 commit e0ff4dd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion unyt/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,11 @@ def to_astropy(self, **kwargs):
>>> data.to_astropy()
<Quantity [3., 4., 5.] g / cm3>
"""
return self.value * _astropy.units.Unit(str(self.units), **kwargs)
if self.units.is_dimensionless:
s_units = ""
else:
s_units = str(self.units)
return self.value * _astropy.units.Unit(s_units, **kwargs)

@classmethod
def from_pint(cls, arr, unit_registry=None):
Expand Down
11 changes: 11 additions & 0 deletions unyt/tests/test_unyt_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,17 @@ def test_astropy():
assert_equal(yt_quan, unyt_quantity.from_astropy(yt_quan.to_astropy()))


def test_astropy_dimensionless():
# see https://github.com/yt-project/unyt/issues/436
pytest.importorskip("astropy")

arr = unyt_array([1, 2, 3], "")
ap_arr = np.array([1, 2, 3]) * _astropy.units.Unit("")

assert_array_equal(ap_arr, arr.to_astropy())
assert_array_equal(arr, unyt_array.from_astropy(ap_arr))


def test_pint():
pytest.importorskip("pint")

Expand Down

0 comments on commit e0ff4dd

Please sign in to comment.