From faa7bedfc1bb2f9ec1bec672d2a5d5dc694bef2f Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Sun, 4 Jul 2021 21:21:34 -0500 Subject: [PATCH 1/2] CLN: remove np17 compat --- pandas/__init__.py | 5 +---- pandas/compat/__init__.py | 2 -- pandas/compat/numpy/__init__.py | 1 - pandas/core/common.py | 3 +-- pandas/tests/api/test_api.py | 1 - pandas/tests/arrays/test_datetimelike.py | 16 ++-------------- pandas/tests/frame/methods/test_sample.py | 14 ++------------ .../tests/indexes/period/test_searchsorted.py | 9 +-------- pandas/tests/indexes/test_numpy_compat.py | 19 ++----------------- pandas/tests/test_common.py | 19 ++++++++----------- 10 files changed, 17 insertions(+), 72 deletions(-) diff --git a/pandas/__init__.py b/pandas/__init__.py index db4043686bcbb..43f05617584cc 100644 --- a/pandas/__init__.py +++ b/pandas/__init__.py @@ -19,10 +19,7 @@ del hard_dependencies, dependency, missing_dependencies # numpy compat -from pandas.compat import ( - np_version_under1p18 as _np_version_under1p18, - is_numpy_dev as _is_numpy_dev, -) +from pandas.compat import is_numpy_dev as _is_numpy_dev try: from pandas._libs import hashtable as _hashtable, lib as _lib, tslib as _tslib diff --git a/pandas/compat/__init__.py b/pandas/compat/__init__.py index 369832e9bc05c..9c99488a57542 100644 --- a/pandas/compat/__init__.py +++ b/pandas/compat/__init__.py @@ -16,7 +16,6 @@ is_numpy_dev, np_array_datetime64_compat, np_datetime64_compat, - np_version_under1p18, np_version_under1p19, np_version_under1p20, ) @@ -151,7 +150,6 @@ def get_lzma_file(lzma): "is_numpy_dev", "np_array_datetime64_compat", "np_datetime64_compat", - "np_version_under1p18", "np_version_under1p19", "np_version_under1p20", "pa_version_under1p0", diff --git a/pandas/compat/numpy/__init__.py b/pandas/compat/numpy/__init__.py index 619713f28ee2d..9a3bb015d12df 100644 --- a/pandas/compat/numpy/__init__.py +++ b/pandas/compat/numpy/__init__.py @@ -9,7 +9,6 @@ # numpy versioning _np_version = np.__version__ _nlv = Version(_np_version) -np_version_under1p18 = _nlv < Version("1.18") np_version_under1p19 = _nlv < Version("1.19") np_version_under1p20 = _nlv < Version("1.20") is_numpy_dev = _nlv.dev is not None diff --git a/pandas/core/common.py b/pandas/core/common.py index dda3e39870ffb..c889f150a3de2 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -34,7 +34,6 @@ Scalar, T, ) -from pandas.compat import np_version_under1p18 from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike from pandas.core.dtypes.common import ( @@ -417,7 +416,7 @@ def random_state(state: RandomState | None = None) -> np.random.RandomState: if ( is_integer(state) or is_array_like(state) - or (not np_version_under1p18 and isinstance(state, np.random.BitGenerator)) + or isinstance(state, np.random.BitGenerator) ): # error: Argument 1 to "RandomState" has incompatible type "Optional[Union[int, # Union[ExtensionArray, ndarray[Any, Any]], Generator, RandomState]]"; expected diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py index 38984238ecf65..95dc1d82cb286 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -193,7 +193,6 @@ class TestPDApi(Base): "_hashtable", "_lib", "_libs", - "_np_version_under1p18", "_is_numpy_dev", "_testing", "_tslib", diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 3f3f3a5ee8d18..1e150f1b431c7 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -10,7 +10,6 @@ OutOfBoundsDatetime, Timestamp, ) -from pandas.compat import np_version_under1p18 import pandas.util._test_decorators as td import pandas as pd @@ -288,12 +287,7 @@ def test_searchsorted(self): # GH#29884 match numpy convention on whether NaT goes # at the end or the beginning result = arr.searchsorted(NaT) - if np_version_under1p18: - # Following numpy convention, NaT goes at the beginning - # (unlike NaN which goes at the end) - assert result == 0 - else: - assert result == 10 + assert result == 10 @pytest.mark.parametrize("box", [None, "index", "series"]) def test_searchsorted_castable_strings(self, arr1d, box, request, string_storage): @@ -1244,17 +1238,11 @@ def test_invalid_nat_setitem_array(arr, non_casting_nats): ], ) def test_to_numpy_extra(arr): - if np_version_under1p18: - # np.isnan(NaT) raises, so use pandas' - isnan = pd.isna - else: - isnan = np.isnan - arr[0] = NaT original = arr.copy() result = arr.to_numpy() - assert isnan(result[0]) + assert np.isnan(result[0]) result = arr.to_numpy(dtype="int64") assert result[0] == -9223372036854775808 diff --git a/pandas/tests/frame/methods/test_sample.py b/pandas/tests/frame/methods/test_sample.py index fc90bcbf5fbdc..e484f49829168 100644 --- a/pandas/tests/frame/methods/test_sample.py +++ b/pandas/tests/frame/methods/test_sample.py @@ -1,8 +1,6 @@ import numpy as np import pytest -from pandas.compat import np_version_under1p18 - from pandas import ( DataFrame, Index, @@ -161,16 +159,8 @@ def test_sample_none_weights(self, obj): "func_str,arg", [ ("np.array", [2, 3, 1, 0]), - pytest.param( - "np.random.MT19937", - 3, - marks=pytest.mark.skipif(np_version_under1p18, reason="NumPy<1.18"), - ), - pytest.param( - "np.random.PCG64", - 11, - marks=pytest.mark.skipif(np_version_under1p18, reason="NumPy<1.18"), - ), + ("np.random.MT19937", 3), + ("np.random.PCG64", 11), ], ) def test_sample_random_state(self, func_str, arg, frame_or_series): diff --git a/pandas/tests/indexes/period/test_searchsorted.py b/pandas/tests/indexes/period/test_searchsorted.py index af243eeccc7a4..27e998284c189 100644 --- a/pandas/tests/indexes/period/test_searchsorted.py +++ b/pandas/tests/indexes/period/test_searchsorted.py @@ -2,7 +2,6 @@ import pytest from pandas._libs.tslibs import IncompatibleFrequency -from pandas.compat import np_version_under1p18 from pandas import ( NaT, @@ -28,13 +27,7 @@ def test_searchsorted(self, freq): p2 = Period("2014-01-04", freq=freq) assert pidx.searchsorted(p2) == 3 - if np_version_under1p18: - # GH#36254 - # Following numpy convention, NaT goes at the beginning - # (unlike NaN which goes at the end) - assert pidx.searchsorted(NaT) == 0 - else: - assert pidx.searchsorted(NaT) == 5 + assert pidx.searchsorted(NaT) == 5 msg = "Input has different freq=H from PeriodArray" with pytest.raises(IncompatibleFrequency, match=msg): diff --git a/pandas/tests/indexes/test_numpy_compat.py b/pandas/tests/indexes/test_numpy_compat.py index f2ed96d0b65b8..d72e77e0f8512 100644 --- a/pandas/tests/indexes/test_numpy_compat.py +++ b/pandas/tests/indexes/test_numpy_compat.py @@ -1,8 +1,6 @@ import numpy as np import pytest -from pandas.compat import np_version_under1p18 - from pandas import ( DatetimeIndex, Float64Index, @@ -82,27 +80,14 @@ def test_numpy_ufuncs_other(index, func, request): isinstance(index, DatetimeIndex) and index.tz is not None and func in [np.isfinite, np.isnan, np.isinf] - and ( - not np_version_under1p18 - or (np_version_under1p18 and func is np.isfinite) - ) ): mark = pytest.mark.xfail(reason="__array_ufunc__ is not defined") request.node.add_marker(mark) - if not np_version_under1p18 and func in [np.isfinite, np.isinf, np.isnan]: - # numpy 1.18(dev) changed isinf and isnan to not raise on dt64/tfd64 - result = func(index) - assert isinstance(result, np.ndarray) - - elif func is np.isfinite: - # ok under numpy >= 1.17 - # Results in bool array + if func in [np.isfinite, np.isinf, np.isnan]: + # numpy 1.18 changed isinf and isnan to not raise on dt64/tfd64 result = func(index) assert isinstance(result, np.ndarray) - else: - with tm.external_error_raised(TypeError): - func(index) elif isinstance(index, PeriodIndex): with tm.external_error_raised(TypeError): diff --git a/pandas/tests/test_common.py b/pandas/tests/test_common.py index 93c95b3004876..8ee6ae40cd406 100644 --- a/pandas/tests/test_common.py +++ b/pandas/tests/test_common.py @@ -5,8 +5,6 @@ import numpy as np import pytest -from pandas.compat import np_version_under1p18 - import pandas as pd from pandas import Series import pandas._testing as tm @@ -72,15 +70,14 @@ def test_random_state(): # Check BitGenerators # GH32503 - if not np_version_under1p18: - assert ( - com.random_state(npr.MT19937(3)).uniform() - == npr.RandomState(npr.MT19937(3)).uniform() - ) - assert ( - com.random_state(npr.PCG64(11)).uniform() - == npr.RandomState(npr.PCG64(11)).uniform() - ) + assert ( + com.random_state(npr.MT19937(3)).uniform() + == npr.RandomState(npr.MT19937(3)).uniform() + ) + assert ( + com.random_state(npr.PCG64(11)).uniform() + == npr.RandomState(npr.PCG64(11)).uniform() + ) # Error for floats or strings msg = ( From a4c149b71c09a3f99e8289766acf45cd606325ee Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Mon, 5 Jul 2021 09:08:30 -0500 Subject: [PATCH 2/2] add else branch back --- pandas/tests/indexes/test_numpy_compat.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/tests/indexes/test_numpy_compat.py b/pandas/tests/indexes/test_numpy_compat.py index d72e77e0f8512..92adc0570dee1 100644 --- a/pandas/tests/indexes/test_numpy_compat.py +++ b/pandas/tests/indexes/test_numpy_compat.py @@ -84,10 +84,13 @@ def test_numpy_ufuncs_other(index, func, request): mark = pytest.mark.xfail(reason="__array_ufunc__ is not defined") request.node.add_marker(mark) - if func in [np.isfinite, np.isinf, np.isnan]: + if func in (np.isfinite, np.isinf, np.isnan): # numpy 1.18 changed isinf and isnan to not raise on dt64/tfd64 result = func(index) assert isinstance(result, np.ndarray) + else: + with tm.external_error_raised(TypeError): + func(index) elif isinstance(index, PeriodIndex): with tm.external_error_raised(TypeError):