diff --git a/CHANGES b/CHANGES index 4ce6b16..f896b8e 100644 --- a/CHANGES +++ b/CHANGES @@ -4,7 +4,7 @@ pint-pandas Changelog 0.6 (unreleased) ---------------- -- Nothing changed yet. +- Fix astype issue #196 0.5 (2023-09-07) diff --git a/pint_pandas/pint_array.py b/pint_pandas/pint_array.py index 7154be5..fcbfe61 100644 --- a/pint_pandas/pint_array.py +++ b/pint_pandas/pint_array.py @@ -417,7 +417,9 @@ def astype(self, dtype, copy=True): return self._to_array_of_quantity(copy=copy) if is_string_dtype(dtype): return pd.array([str(x) for x in self.quantity], dtype=dtype) - return pd.array(self.quantity, dtype, copy) + if isinstance(self._data, ExtensionArray): + return self._data.astype(dtype, copy=copy) + return pd.array(self.quantity.m, dtype, copy) @property def units(self): diff --git a/pint_pandas/testsuite/test_issues.py b/pint_pandas/testsuite/test_issues.py index 95d85b2..d8d6ce0 100644 --- a/pint_pandas/testsuite/test_issues.py +++ b/pint_pandas/testsuite/test_issues.py @@ -185,3 +185,12 @@ def test_sum(self): expected_2 = pd.Series([3, 12], dtype="pint[m]") tm.assert_series_equal(col_sum, expected_2) + + +@pytest.mark.parametrize("dtype", [pd.Float64Dtype(), "float"]) +def test_issue_194(dtype): + s0 = pd.Series([1.0, 2.5], dtype=dtype) + s1 = s0.astype("pint[dimensionless]") + s2 = s1.astype(dtype) + + tm.assert_series_equal(s0, s2)