Repository: hgrecco/pint-pandas

Issue:
series.astype(float) not returning series of floats
# Start: series with dimensionless values

```python
import pandas as pd
import pint
import pint_pandas

s0 = pd.Series([1.0, 2.5])
s1 = s0.astype('pint[dimensionless]')

s1
# 0    1.0
# 1    2.5
# dtype: pint[]
```

# Issue: conversion back to floats

```python
s2 = s1.astype(float)
```

Previously, the resulting Series had a `float` data type. Now, the datatype is slightly different, which causes the comparison with `s0` to fail.
```
s2.dtype
# PandasDtype('float64')

pd.testing.assert_series_equal(s0, s2)  #"dtypes are different"
```

Another issue is, that `s2` cannot be converted to a string:

```python
s2
#AttributeError: 'numpy.ndarray' object has no attribute '_formatter'
```

Excuse me as I currently cannot test this with the latest `pandas` version; I've used `2.1` instead. `pint-pandas` `0.5` , and `pint` `0.22`.

Discussion:

Patch:
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 @@ class PintArray(ExtensionArray, ExtensionOpsMixin):
             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):


Test patch:
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 @@ class TestIssue174(BaseExtensionTests):
         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)