Skip to content

Commit 6ff53c2

Browse files
Nicholas Ver Halenjreback
Nicholas Ver Halen
authored andcommitted
BUG: downcast='unsigned' on 0 would would not downcast to unsigned.
closes #14401 Author: Nicholas Ver Halen <[email protected]> Closes #14472 from verhalenn/issue14401 and squashes the following commits: 21e1a97 [Nicholas Ver Halen] Made the downcast limits test its own function. a62a3f8 [Nicholas Ver Halen] Revert "Added release note to issue 14401 resolve." 7413a9c [Nicholas Ver Halen] Did some editing for some lint problems. 26fe3a1 [Nicholas Ver Halen] Made the dictionaries into one list of sets. 5023fc7 [Nicholas Ver Halen] Changed the test to work with python 3.x 9ccc991 [Nicholas Ver Halen] Changed the tests so that it iterated through a dictionary. ef35e19 [Nicholas Ver Halen] Added tests for the max and min values of all dtypes to to_numeric cc278ff [Nicholas Ver Halen] Added release note to issue 14401 resolve. 11a421d [Nicholas Ver Halen] Added a test to check uint8 with 0 11038f8 [Nicholas Ver Halen] Made it so that 0 was included in uint8
1 parent 2e77536 commit 6ff53c2

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

doc/source/whatsnew/v0.19.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Bug Fixes
5757

5858
- Bug in ``Timestamp`` where dates very near the minimum (1677-09) could underflow on creation (:issue:`14415`)
5959

60+
- Bug in ``pd.to_numeric`` where 0 was not included when ``downcast='unsigned'`` is passed (:issue:`14401`)
6061
- Bug in ``pd.concat`` where names of the ``keys`` were not propagated to the resulting ``MultiIndex`` (:issue:`14252`)
6162
- Bug in ``pd.concat`` where ``axis`` cannot take string parameters ``'rows'`` or ``'columns'`` (:issue:`14369`)
6263
- Bug in ``pd.concat`` with dataframes heterogeneous in length and tuple ``keys`` (:issue:`14438`)

pandas/tools/tests/test_util.py

+46
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,52 @@ def test_downcast(self):
401401
res = pd.to_numeric(data, downcast=downcast)
402402
tm.assert_numpy_array_equal(res, expected)
403403

404+
def test_downcast_limits(self):
405+
# Test the limits of each downcast. #14401
406+
# uint64 is not fully supported ATM
407+
dtype_downcast_min_max = [
408+
('int8', 'integer',
409+
[np.iinfo(np.int8).min, np.iinfo(np.int8).max]),
410+
('int16', 'integer',
411+
[np.iinfo(np.int16).min, np.iinfo(np.int16).max]),
412+
('int32', 'integer',
413+
[np.iinfo(np.int32).min, np.iinfo(np.int32).max]),
414+
('int64', 'integer',
415+
[np.iinfo(np.int64).min, np.iinfo(np.int64).max]),
416+
('uint8', 'unsigned',
417+
[np.iinfo(np.uint8).min, np.iinfo(np.uint8).max]),
418+
('uint16', 'unsigned',
419+
[np.iinfo(np.uint16).min, np.iinfo(np.uint16).max]),
420+
('uint32', 'unsigned',
421+
[np.iinfo(np.uint32).min, np.iinfo(np.uint32).max]),
422+
# ('uint64', 'unsigned',
423+
# [np.iinfo(np.uint64).min, np.iinfo(np.uint64).max]),
424+
425+
('int16', 'integer',
426+
[np.iinfo(np.int8).min, np.iinfo(np.int8).max + 1]),
427+
('int32', 'integer',
428+
[np.iinfo(np.int16).min, np.iinfo(np.int16).max + 1]),
429+
('int64', 'integer',
430+
[np.iinfo(np.int32).min, np.iinfo(np.int32).max + 1]),
431+
('int16', 'integer',
432+
[np.iinfo(np.int8).min - 1, np.iinfo(np.int16).max]),
433+
('int32', 'integer',
434+
[np.iinfo(np.int16).min - 1, np.iinfo(np.int32).max]),
435+
('int64', 'integer',
436+
[np.iinfo(np.int32).min - 1, np.iinfo(np.int64).max]),
437+
('uint16', 'unsigned',
438+
[np.iinfo(np.uint8).min, np.iinfo(np.uint8).max + 1]),
439+
('uint32', 'unsigned',
440+
[np.iinfo(np.uint16).min, np.iinfo(np.uint16).max + 1]),
441+
# ('uint64', 'unsigned',
442+
# [np.iinfo(np.uint32).min, np.iinfo(np.uint32).max + 1]),
443+
]
444+
445+
for dtype, downcast, min_max in dtype_downcast_min_max:
446+
series = pd.to_numeric(pd.Series(min_max), downcast=downcast)
447+
tm.assert_equal(series.dtype, dtype)
448+
449+
404450
if __name__ == '__main__':
405451
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
406452
exit=False)

pandas/tools/util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def to_numeric(arg, errors='raise', downcast=None):
205205

206206
if downcast in ('integer', 'signed'):
207207
typecodes = np.typecodes['Integer']
208-
elif downcast == 'unsigned' and np.min(values) > 0:
208+
elif downcast == 'unsigned' and np.min(values) >= 0:
209209
typecodes = np.typecodes['UnsignedInteger']
210210
elif downcast == 'float':
211211
typecodes = np.typecodes['Float']

0 commit comments

Comments
 (0)