Skip to content

PintArray.astype does not use the same conventions as pandas for float and int dtypes #183

Closed
@remicome

Description

@remicome

Description

Casting a pandas Series from float to pint quantities, then to float again using PintArray.astype, does not give back the original pandas dtype. The last series has type PandasDtype('float64') instead of float64. This is a source of unexpected test errors involving dtypes comparison.

Expected behavior

If my_pint_array is a PintArray instance, then my_pint_array.astype(float) should have the same dtype as pandas' default float implementation (i.e. float64). The same applies for int

Minimal reproducible example

>>> pint_pandas.show_versions()
{'numpy': '1.24.3', 'pandas': '2.0.2', 'pint': '0.19.2', 'pint_pandas': '0.4'}
>>> import pandas as pd
>>> import pint_pandas
>>> dimensionless_series = pd.Series(
    [1, 1.5, 2],
)
>>> quantities = dimensionless_series.astype("pint[m]")
>>> new_dimensionless_series = quantities.astype(float)
>>> pd.testing.assert_series_equal(dimensionless_series, new_dimensionless_series)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".venv/lib/python3.9/site-packages/pandas/_testing/asserters.py", 
line 931, in assert_series_equal
    assert_attr_equal("dtype", left, right, obj=f"Attributes of {obj}")
  File ".venv/lib/python3.9/site-packages/pandas/_testing/asserters.py", 
line 415, in assert_attr_equal
    raise_assert_detail(obj, msg, left_attr, right_attr)
  File ".venv/lib/python3.9/site-packages/pandas/_testing/asserters.py", 
line 599, in raise_assert_detail
    raise AssertionError(msg)
AssertionError: Attributes of Series are different

Attribute "dtype" are different
[left]:  float64
[right]: PandasDtype('float64')

Notes

  • The same code does not raise any error with pint-pandas==0.2.
  • Calling astype twice solves the problem:
    >>> new_dimensionless_series = quantities.astype(float).astype(float)
    >>> new_dimensionless_series.dtype
    dtype('float64')
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions