@@ -41,7 +41,7 @@ def _cached_int_clippers(flt_type, int_type):
41
41
return FLT_INT_CLIPS [(flt_type , int_type )]
42
42
43
43
44
- class RoundingError (Exception ):
44
+ class CastingError (Exception ):
45
45
pass
46
46
47
47
@@ -56,7 +56,7 @@ def nice_round(arr, int_type, nan2zero=True, infmax=False):
56
56
Numpy integer type
57
57
nan2zero : {True, False}
58
58
Whether to convert NaN value to zero. Default is True. If False, and
59
- NaNs are present, raise RoundingError
59
+ NaNs are present, raise CastingError
60
60
infmax : {False, True}
61
61
If True, set np.inf values in `arr` to be `int_type` integer maximum
62
62
value, -np.inf as `int_type` integer minimum. If False, merely set infs
@@ -77,23 +77,21 @@ def nice_round(arr, int_type, nan2zero=True, infmax=False):
77
77
arr = np .asarray (arr )
78
78
flt_type = arr .dtype .type
79
79
int_type = np .dtype (int_type ).type
80
+ # Deal with scalar as input; fancy indexing needs 1D
81
+ shape = arr .shape
82
+ arr = np .atleast_1d (arr )
80
83
mn , mx = _cached_int_clippers (flt_type , int_type )
81
84
nans = np .isnan (arr )
82
85
have_nans = np .any (nans )
83
86
if not nan2zero and have_nans :
84
- raise RoundingError ('NaNs in array, nan2zero not True' )
87
+ raise CastingError ('NaNs in array, nan2zero not True' )
85
88
iarr = np .clip (np .rint (arr ), mn , mx ).astype (int_type )
86
89
if have_nans :
87
90
iarr [nans ] = 0
88
91
if not infmax :
89
- return iarr
90
- # Deal with scalar as input
91
- shape = iarr .shape
92
- iarr = np .atleast_1d (iarr )
93
- arr = np .atleast_1d (arr )
92
+ return iarr .reshape (shape )
94
93
ii = np .iinfo (int_type )
95
94
iarr [arr == np .inf ] = ii .max
96
95
if ii .min != int (mn ):
97
96
iarr [arr == - np .inf ] = ii .min
98
- iarr .shape = shape
99
- return iarr
97
+ return iarr .reshape (shape )
0 commit comments