Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
import numpy as np
import pint
from pint import Quantity as Q_
ureg = pint.get_application_registry()
data = pd.DataFrame({'m':[1, 2], 'unit':['m', 'kg']})
result = data['m'].combine(data['unit'], lambda m, u: Q_(m, u))
expected = np.array([Q_(1, 'm'), Q_(2, 'kg')], dtype=object)
assert all(result == expected)
print("ok")
Issue Description
Pint (version 0.22) treats a generic attempt to construct an array from list of Quantities as a request for just their magnitudes, and then it complains that it cannot convert a quantified type into a dimensionless quantity. One can argue that it could have better semantics in that case. Nevertheless, I think Pandas is to blame for trying to assume what might or might not happen when calling np.array
on some random type.
When I investigated what was going wrong, I found that the implementation of maybe_downcast_numeric
was not following the comments in the code. I slightly modified the code to better follow what the comments seemed to intend, and this also fixed the fatal problems with Pint.
The changes still elicit a warning about stripping units, specifically when isna(r[0])
digs into the Quantity, doesn't see it as a scalar, but does ultimately see an array method it can call on the Quantity. That call returns the magnitude of the quantity (stripped of its units), and isna properly reports whether the magnitude is NA or not. I think that Pint has this default array behavior specifically to make isna
work in this way.
Expected Behavior
We expect that the program will complete and print 'ok'
Installed Versions
INSTALLED VERSIONS
commit : d98e6f0
python : 3.11.5.final.0
python-bits : 64
OS : Darwin
OS-release : 22.6.0
Version : Darwin Kernel Version 22.6.0: Wed Jul 5 22:21:56 PDT 2023; root:xnu-8796.141.3~6/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.0.dev0+391.gd98e6f00ca.dirty
numpy : 1.24.4
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.1.2
pip : 23.2.1
Cython : 0.29.33
pytest : 7.4.2
hypothesis : 6.84.2
sphinx : 6.2.1
blosc : 1.11.1
feather : None
xlsxwriter : 3.1.2
lxml.etree : 4.9.3
html5lib : 1.1
pymysql : 1.4.6
psycopg2 : 2.9.7
jinja2 : 3.1.2
IPython : 8.15.0
pandas_datareader : None
bs4 : 4.12.2
bottleneck : 1.3.7
dataframe-api-compat: None
fastparquet : 2023.8.0
fsspec : 2023.9.0
gcsfs : 2023.9.0
matplotlib : 3.7.2
numba : 0.57.1
numexpr : 2.8.5
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 13.0.0
pyreadstat : 1.2.3
python-calamine : None
pyxlsb : 1.0.10
s3fs : 2023.9.0
scipy : 1.11.2
sqlalchemy : 2.0.20
tables : 3.8.0
tabulate : 0.9.0
xarray : 2023.8.0
xlrd : 2.0.1
zstandard : 0.21.0
tzdata : 2023.3
qtpy : None
pyqt5 : None