|
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) |
18 | 16 | from pandas.core.index import MultiIndex
|
19 | 17 | from pandas.core.indexes.datetimes import Timestamp
|
20 | 18 | from pandas.core.indexes.timedeltas import Timedelta
|
@@ -1948,6 +1946,10 @@ def test_mode_sortwarning(self):
|
1948 | 1946 |
|
1949 | 1947 | class TestNLargestNSmallest(object):
|
1950 | 1948 |
|
| 1949 | + @pytest.fixture(params=['nlargest', 'nsmallest']) |
| 1950 | + def method(self, request): |
| 1951 | + return request.param |
| 1952 | + |
1951 | 1953 | @pytest.mark.parametrize(
|
1952 | 1954 | "r", [Series([3., 2, 1, 2, '5'], dtype='object'),
|
1953 | 1955 | Series([3., 2, 1, 2, 5], dtype='object'),
|
@@ -2030,33 +2032,40 @@ def test_n(self, n):
|
2030 | 2032 | expected = s.sort_values().head(n)
|
2031 | 2033 | assert_series_equal(result, expected)
|
2032 | 2034 |
|
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 |
| - |
| 2035 | + def _check_nselect_boundary(self, vals, dtype, method): |
| 2036 | + # helper function for 'test_boundary_dtype' tests |
2054 | 2037 | s = Series(vals, dtype=dtype)
|
2055 | 2038 | result = getattr(s, method)(3)
|
2056 | 2039 | expected_idxr = [0, 1, 2] if method == 'nsmallest' else [3, 2, 1]
|
2057 | 2040 | expected = s.loc[expected_idxr]
|
2058 | 2041 | tm.assert_series_equal(result, expected)
|
2059 | 2042 |
|
| 2043 | + @pytest.mark.parametrize('dtype', [ |
| 2044 | + 'int8', 'int16', 'int32', 'int64', |
| 2045 | + 'uint8', 'uint16', 'uint32', 'uint64']) |
| 2046 | + def test_boundary_integer(self, method, dtype): |
| 2047 | + # GH 21426 |
| 2048 | + min_val, max_val = np.iinfo(dtype).min, np.iinfo(dtype).max |
| 2049 | + vals = [min_val, min_val + 1, max_val - 1, max_val] |
| 2050 | + self._check_nselect_boundary(vals, dtype, method) |
| 2051 | + |
| 2052 | + @pytest.mark.parametrize('dtype', ['float16', 'float32', 'float64']) |
| 2053 | + def test_boundary_float(self, method, dtype): |
| 2054 | + # GH 21426 |
| 2055 | + min_val, max_val = np.finfo(dtype).min, np.finfo(dtype).max |
| 2056 | + min_2nd, max_2nd = np.nextafter([min_val, max_val], 0, dtype=dtype) |
| 2057 | + vals = [min_val, min_2nd, max_2nd, max_val] |
| 2058 | + self._check_nselect_boundary(vals, dtype, method) |
| 2059 | + |
| 2060 | + @pytest.mark.parametrize('dtype', ['datetime64[ns]', 'timedelta64[ns]']) |
| 2061 | + def test_boundary_datetimelike(self, method, dtype): |
| 2062 | + # GH 21426 |
| 2063 | + # use int64 bounds and +1 to min_val since true minimum is NaT |
| 2064 | + # (include min_val/NaT at end to maintain same expected_idxr) |
| 2065 | + min_val, max_val = np.iinfo('int64').min, np.iinfo('int64').max |
| 2066 | + vals = [min_val + 1, min_val + 2, max_val - 1, max_val, min_val] |
| 2067 | + self._check_nselect_boundary(vals, dtype, method) |
| 2068 | + |
2060 | 2069 |
|
2061 | 2070 | class TestCategoricalSeriesAnalytics(object):
|
2062 | 2071 |
|
|
0 commit comments