diff --git a/pint_pandas/pint_array.py b/pint_pandas/pint_array.py index 486ce53..6568e14 100644 --- a/pint_pandas/pint_array.py +++ b/pint_pandas/pint_array.py @@ -308,6 +308,8 @@ def __init__(self, values, dtype=None, copy=False): # infer subdtype from values if not given in dtype if isinstance(dtype, str) and dtype.count("[") <= 1: _dtype = PintType(dtype) + if isinstance(values, _Quantity): + values = values.m_as(_dtype.units) values = pd.array(values, copy=copy) dtype = PintType(units=_dtype.units, subdtype=values.dtype) # infer units and subdtype from values if no dtype given diff --git a/pint_pandas/testsuite/test_pandas_interface.py b/pint_pandas/testsuite/test_pandas_interface.py index 2cddcbd..63d0597 100644 --- a/pint_pandas/testsuite/test_pandas_interface.py +++ b/pint_pandas/testsuite/test_pandas_interface.py @@ -92,7 +92,7 @@ def test_df_operations(self): def test_dequantify(self): df = pd.DataFrame( { - "no_unit_column": pd.Series([i for i in range(4)], dtype=float), + "no_unit_column": pd.Series([i for i in range(4)], dtype="Float64"), "pintarray_column": pd.Series( [1.0, 2.0, 2.0, 3.0], dtype="pint[lbf ft]" ), @@ -100,7 +100,7 @@ def test_dequantify(self): ) expected = pd.DataFrame( { - ("no_unit_column", "No Unit"): {0: 0.0, 1: 1.0, 2: 2.0, 3: 3.0}, + ("no_unit_column", "No Unit"): pd.Series({0: 0.0, 1: 1.0, 2: 2.0, 3: 3.0}, dtype="Float64"), ("pintarray_column", "foot * force_pound"): pd.Series( { 0: 1.0, @@ -127,12 +127,12 @@ def test_quantify(self): 2: 2.0, 3: 3.0, }, - } + }, dtype="Float64" ) df.columns.names = [None, "unit"] expected = pd.DataFrame( { - "no_unit_column": pd.Series([i for i in range(4)], dtype=float), + "no_unit_column": pd.Series([i for i in range(4)], dtype="Float64"), "pintarray_column": pd.Series( [1.0, 2.0, 2.0, 3.0], dtype="pint[lbf ft]" ), @@ -145,7 +145,7 @@ def test_quantify(self): def test_to_base_units(self): df = pd.DataFrame( { - "no_unit_column": pd.Series([i for i in range(4)], dtype=float), + "no_unit_column": pd.Series([i for i in range(4)], dtype="Float64"), "pintarray_column": pd.Series( [1.0, 2.0, 2.0, 3.0], dtype="pint[lbf ft]" ), @@ -155,7 +155,7 @@ def test_to_base_units(self): expected = pd.DataFrame( { - "no_unit_column": pd.Series([i for i in range(4)], dtype=float), + "no_unit_column": pd.Series([i for i in range(4)], dtype="Float64"), "pintarray_column": pd.Series( [ 1.3558179483314006,