|
13 | 13 | from pandas import (Series, Categorical, DataFrame, isna, notna,
|
14 | 14 | bdate_range, date_range, _np_version_under1p10,
|
15 | 15 | CategoricalIndex)
|
| 16 | +from pandas.core.dtypes.common import ( |
| 17 | + is_float_dtype, is_integer_dtype, is_datetimelike) |
16 | 18 | from pandas.core.index import MultiIndex
|
17 | 19 | from pandas.core.indexes.datetimes import Timestamp
|
18 | 20 | from pandas.core.indexes.timedeltas import Timedelta
|
@@ -2028,6 +2030,33 @@ def test_n(self, n):
|
2028 | 2030 | expected = s.sort_values().head(n)
|
2029 | 2031 | assert_series_equal(result, expected)
|
2030 | 2032 |
|
| 2033 | + @pytest.mark.parametrize('dtype', [ |
| 2034 | + 'int8', 'int16', 'int32', 'int64', |
| 2035 | + 'uint8', 'uint16', 'uint32', 'uint64', |
| 2036 | + 'float16', 'float32', 'float64', |
| 2037 | + 'datetime64[ns]', 'timedelta64[ns]']) |
| 2038 | + @pytest.mark.parametrize('method', ['nsmallest', 'nlargest']) |
| 2039 | + def test_boundary(self, method, dtype): |
| 2040 | + # GH 21426 |
| 2041 | + if is_float_dtype(dtype): |
| 2042 | + min_val, max_val = np.finfo(dtype).min, np.finfo(dtype).max |
| 2043 | + min_2nd, max_2nd = np.nextafter([min_val, max_val], 0, dtype=dtype) |
| 2044 | + vals = [min_val, min_2nd, max_2nd, max_val] |
| 2045 | + elif is_integer_dtype(dtype): |
| 2046 | + min_val, max_val = np.iinfo(dtype).min, np.iinfo(dtype).max |
| 2047 | + vals = [min_val, min_val + 1, max_val - 1, max_val] |
| 2048 | + elif is_datetimelike(dtype): |
| 2049 | + # use int64 bounds and +1 to min_val since true minimum is NaT |
| 2050 | + # (include min_val/NaT at end to maintain same expected_idxr) |
| 2051 | + min_val, max_val = np.iinfo('int64').min, np.iinfo('int64').max |
| 2052 | + vals = [min_val + 1, min_val + 2, max_val - 1, max_val, min_val] |
| 2053 | + |
| 2054 | + s = Series(vals, dtype=dtype) |
| 2055 | + result = getattr(s, method)(3) |
| 2056 | + expected_idxr = [0, 1, 2] if method == 'nsmallest' else [3, 2, 1] |
| 2057 | + expected = s.loc[expected_idxr] |
| 2058 | + tm.assert_series_equal(result, expected) |
| 2059 | + |
2031 | 2060 |
|
2032 | 2061 | class TestCategoricalSeriesAnalytics(object):
|
2033 | 2062 |
|
|
0 commit comments