Skip to content

Commit

Permalink
BUG: downcast='unsigned' on 0 would would not downcast to unsigned.
Browse files Browse the repository at this point in the history
closes #14401

Author: Nicholas Ver Halen <verhalenn@yahoo.com>

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
  • Loading branch information
Nicholas Ver Halen authored and jreback committed Oct 25, 2016
1 parent 2e77536 commit 6ff53c2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.1.txt
Expand Up @@ -57,6 +57,7 @@ Bug Fixes

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

- Bug in ``pd.to_numeric`` where 0 was not included when ``downcast='unsigned'`` is passed (:issue:`14401`)
- Bug in ``pd.concat`` where names of the ``keys`` were not propagated to the resulting ``MultiIndex`` (:issue:`14252`)
- Bug in ``pd.concat`` where ``axis`` cannot take string parameters ``'rows'`` or ``'columns'`` (:issue:`14369`)
- Bug in ``pd.concat`` with dataframes heterogeneous in length and tuple ``keys`` (:issue:`14438`)
Expand Down
46 changes: 46 additions & 0 deletions pandas/tools/tests/test_util.py
Expand Up @@ -401,6 +401,52 @@ 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. #14401
# uint64 is not fully supported ATM
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
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

0 comments on commit 6ff53c2

Please sign in to comment.