Skip to content

to_numeric fails with empty data and downcast="unsigned" #32493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pspeter opened this issue Mar 6, 2020 · 4 comments · Fixed by #32512
Closed

to_numeric fails with empty data and downcast="unsigned" #32493

pspeter opened this issue Mar 6, 2020 · 4 comments · Fixed by #32512
Labels
Dtype Conversions Unexpected or buggy dtype conversions good first issue
Milestone

Comments

@pspeter
Copy link

pspeter commented Mar 6, 2020

Code Sample, a copy-pastable example if possible

pd.to_numeric([], downcast="unsigned")

Problem description

The code currently calls np.min() on the data which fails when the data is empty. This does not happen for any other downcast.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../lib/python3.7/site-packages/pandas/core/tools/numeric.py", line 163, in to_numeric
    elif downcast == "unsigned" and np.min(values) >= 0:
  File "<__array_function__ internals>", line 6, in amin
  File "/.../lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 2793, in amin
    keepdims=keepdims, initial=initial, where=where)
  File "/.../lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 90, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError: zero-size array to reduction operation minimum which has no identity

Expected Output

array([], dtype=uint8)

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.6.final.0
python-bits : 64
OS : Darwin
OS-release : 19.3.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 41.2.0
Cython : None
pytest : 5.3.5
hypothesis : None
sphinx : None
blosc : None
feather : 0.4.0
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.13.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : 0.6.0
lxml.etree : None
matplotlib : 3.1.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.16.0
pytables : None
pytest : 5.3.5
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : 0.48.0

@TomAugspurger
Copy link
Contributor

Thanks for the report. I think it's safe to include an initial=0 in that call to np.min in

elif downcast == "unsigned" and np.min(values) >= 0:
typecodes = np.typecodes["UnsignedInteger"]
.

@TomAugspurger TomAugspurger added this to the Contributions Welcome milestone Mar 6, 2020
@TomAugspurger TomAugspurger added Dtype Conversions Unexpected or buggy dtype conversions good first issue labels Mar 6, 2020
@jreback jreback modified the milestones: Contributions Welcome, 1.1 Mar 11, 2020
@MillanSharma
Copy link

take

@pspeter
Copy link
Author

pspeter commented Mar 14, 2020

There's already a PR for this bug!

@MillanSharma MillanSharma removed their assignment Mar 15, 2020
@pspeter
Copy link
Author

pspeter commented Mar 16, 2020

Thanks @mikekutzma! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions good first issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants