Skip to content

BUG: downcast = 'unsigend' on 0 would would not downcast to unsigned. #14472

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
wants to merge 10 commits into from
12 changes: 4 additions & 8 deletions doc/source/whatsnew/v0.19.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,8 @@ Bug Fixes
- Bug in ``MultiIndex.set_levels`` where illegal level values were still set after raising an error (:issue:`13754`)
- Bug in ``DataFrame.to_json`` where ``lines=True`` and a value contained a ``}`` character (:issue:`14391`)
- Bug in ``df.groupby`` causing an ``AttributeError`` when grouping a single index frame by a column and the index level (:issue`14327`)








- Bug in ``pd.pivot_table`` may raise ``TypeError`` or ``ValueError`` when ``index`` or ``columns`` is not scalar and ``values`` is not specified (:issue:`14380`)
- Bug in ``pd.pivot_table`` may raise ``TypeError`` or ``ValueError`` when ``index`` or ``columns``
- Bug in ``pd.to_numeric`` where a 0 was not unsigned on a downcast = 'unsigned' argument (:issue:`14401`)
- Bug in ``pd.pivot_table`` may raise ``TypeError`` or ``ValueError`` when ``index`` or ``columns``
is not scalar and ``values`` is not specified (:issue:`14380`)
27 changes: 27 additions & 0 deletions pandas/tools/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,33 @@ def test_downcast(self):
res = pd.to_numeric(data, downcast=downcast)
tm.assert_numpy_array_equal(res, expected)

def test_downcast_limits(self):
# Test the limits of each downcast. Bug: #14401.
dtype_downcast_min_max = [
('int8', 'integer', [np.iinfo(np.int8).min, np.iinfo(np.int8).max]),
('int16', 'integer', [np.iinfo(np.int16).min, np.iinfo(np.int16).max]),
('int32', 'integer', [np.iinfo(np.int32).min, np.iinfo(np.int32).max]),
('int64', 'integer', [np.iinfo(np.int64).min, np.iinfo(np.int64).max]),
('uint8', 'unsigned', [np.iinfo(np.uint8).min, np.iinfo(np.uint8).max]),
('uint16', 'unsigned', [np.iinfo(np.uint16).min, np.iinfo(np.uint16).max]),
('uint32', 'unsigned', [np.iinfo(np.uint32).min, np.iinfo(np.uint32).max]),
# ('uint64', 'unsigned', [np.iinfo(np.uint64).min, np.iinfo(np.uint64).max]),
('int16', 'integer', [np.iinfo(np.int8).min, np.iinfo(np.int8).max + 1]),
('int32', 'integer', [np.iinfo(np.int16).min, np.iinfo(np.int16).max + 1]),
('int64', 'integer', [np.iinfo(np.int32).min, np.iinfo(np.int32).max + 1]),
('int16', 'integer', [np.iinfo(np.int8).min - 1, np.iinfo(np.int16).max]),
('int32', 'integer', [np.iinfo(np.int16).min - 1, np.iinfo(np.int32).max]),
('int64', 'integer', [np.iinfo(np.int32).min - 1, np.iinfo(np.int64).max]),
('uint16', 'unsigned', [np.iinfo(np.uint8).min, np.iinfo(np.uint8).max + 1]),
('uint32', 'unsigned', [np.iinfo(np.uint16).min, np.iinfo(np.uint16).max + 1]),
# ('uint64', 'unsigned', [np.iinfo(np.uint32).min, np.iinfo(np.uint32).max + 1]),
]

for dtype, downcast, min_max in dtype_downcast_min_max:
series = pd.to_numeric(pd.Series(min_max), downcast=downcast)
tm.assert_equal(series.dtype, dtype)


if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
exit=False)
2 changes: 1 addition & 1 deletion pandas/tools/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def to_numeric(arg, errors='raise', downcast=None):

if downcast in ('integer', 'signed'):
typecodes = np.typecodes['Integer']
elif downcast == 'unsigned' and np.min(values) > 0:
elif downcast == 'unsigned' and np.min(values) >= 0:
typecodes = np.typecodes['UnsignedInteger']
elif downcast == 'float':
typecodes = np.typecodes['Float']
Expand Down