|
4 | 4 | import nose
|
5 | 5 |
|
6 | 6 | import numpy as np
|
| 7 | +from numpy import iinfo |
7 | 8 |
|
8 | 9 | import pandas as pd
|
9 |
| -from pandas import date_range, Index |
| 10 | +from pandas import (date_range, Index, _np_version_under1p9) |
10 | 11 | import pandas.util.testing as tm
|
11 | 12 | from pandas.tools.util import cartesian_product, to_numeric
|
12 | 13 |
|
@@ -401,6 +402,41 @@ def test_downcast(self):
|
401 | 402 | res = pd.to_numeric(data, downcast=downcast)
|
402 | 403 | tm.assert_numpy_array_equal(res, expected)
|
403 | 404 |
|
| 405 | + def test_downcast_limits(self): |
| 406 | + # Test the limits of each downcast. Bug: #14401. |
| 407 | + # Check to make sure numpy is new enough to run this test. |
| 408 | + if _np_version_under1p9: |
| 409 | + raise nose.SkipTest("Numpy version is under 1.9") |
| 410 | + |
| 411 | + i = 'integer' |
| 412 | + u = 'unsigned' |
| 413 | + dtype_downcast_min_max = [ |
| 414 | + ('int8', i, [iinfo(np.int8).min, iinfo(np.int8).max]), |
| 415 | + ('int16', i, [iinfo(np.int16).min, iinfo(np.int16).max]), |
| 416 | + ('int32', i, [iinfo(np.int32).min, iinfo(np.int32).max]), |
| 417 | + ('int64', i, [iinfo(np.int64).min, iinfo(np.int64).max]), |
| 418 | + ('uint8', u, [iinfo(np.uint8).min, iinfo(np.uint8).max]), |
| 419 | + ('uint16', u, [iinfo(np.uint16).min, iinfo(np.uint16).max]), |
| 420 | + ('uint32', u, [iinfo(np.uint32).min, iinfo(np.uint32).max]), |
| 421 | + # Test will be skipped until there is more uint64 support. |
| 422 | + # ('uint64', u, [iinfo(uint64).min, iinfo(uint64).max]), |
| 423 | + ('int16', i, [iinfo(np.int8).min, iinfo(np.int8).max + 1]), |
| 424 | + ('int32', i, [iinfo(np.int16).min, iinfo(np.int16).max + 1]), |
| 425 | + ('int64', i, [iinfo(np.int32).min, iinfo(np.int32).max + 1]), |
| 426 | + ('int16', i, [iinfo(np.int8).min - 1, iinfo(np.int16).max]), |
| 427 | + ('int32', i, [iinfo(np.int16).min - 1, iinfo(np.int32).max]), |
| 428 | + ('int64', i, [iinfo(np.int32).min - 1, iinfo(np.int64).max]), |
| 429 | + ('uint16', u, [iinfo(np.uint8).min, iinfo(np.uint8).max + 1]), |
| 430 | + ('uint32', u, [iinfo(np.uint16).min, iinfo(np.uint16).max + 1]), |
| 431 | + # Test will be skipped until there is more uint64 support. |
| 432 | + # ('uint64', u, [iinfo(np.uint32).min, iinfo(np.uint32).max + 1]), |
| 433 | + ] |
| 434 | + |
| 435 | + for dtype, downcast, min_max in dtype_downcast_min_max: |
| 436 | + series = pd.to_numeric(pd.Series(min_max), downcast=downcast) |
| 437 | + tm.assert_equal(series.dtype, dtype) |
| 438 | + |
| 439 | + |
404 | 440 | if __name__ == '__main__':
|
405 | 441 | nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
|
406 | 442 | exit=False)
|
0 commit comments