test_deprecations.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """Test deprecation and future warnings.
  2. """
  3. import pytest
  4. import numpy as np
  5. from numpy.ma.core import MaskedArrayFutureWarning
  6. from numpy.ma.testutils import assert_equal
  7. class TestArgsort:
  8. """ gh-8701 """
  9. def _test_base(self, argsort, cls):
  10. arr_0d = np.array(1).view(cls)
  11. argsort(arr_0d)
  12. arr_1d = np.array([1, 2, 3]).view(cls)
  13. argsort(arr_1d)
  14. # argsort has a bad default for >1d arrays
  15. arr_2d = np.array([[1, 2], [3, 4]]).view(cls)
  16. result = pytest.warns(
  17. np.ma.core.MaskedArrayFutureWarning, argsort, arr_2d)
  18. assert_equal(result, argsort(arr_2d, axis=None))
  19. # should be no warnings for explicitly specifying it
  20. argsort(arr_2d, axis=None)
  21. argsort(arr_2d, axis=-1)
  22. def test_function_ndarray(self):
  23. return self._test_base(np.ma.argsort, np.ndarray)
  24. def test_function_maskedarray(self):
  25. return self._test_base(np.ma.argsort, np.ma.MaskedArray)
  26. def test_method(self):
  27. return self._test_base(np.ma.MaskedArray.argsort, np.ma.MaskedArray)
  28. class TestMinimumMaximum:
  29. def test_axis_default(self):
  30. # NumPy 1.13, 2017-05-06
  31. data1d = np.ma.arange(6)
  32. data2d = data1d.reshape(2, 3)
  33. ma_min = np.ma.minimum.reduce
  34. ma_max = np.ma.maximum.reduce
  35. # check that the default axis is still None, but warns on 2d arrays
  36. result = pytest.warns(MaskedArrayFutureWarning, ma_max, data2d)
  37. assert_equal(result, ma_max(data2d, axis=None))
  38. result = pytest.warns(MaskedArrayFutureWarning, ma_min, data2d)
  39. assert_equal(result, ma_min(data2d, axis=None))
  40. # no warnings on 1d, as both new and old defaults are equivalent
  41. result = ma_min(data1d)
  42. assert_equal(result, ma_min(data1d, axis=None))
  43. assert_equal(result, ma_min(data1d, axis=0))
  44. result = ma_max(data1d)
  45. assert_equal(result, ma_max(data1d, axis=None))
  46. assert_equal(result, ma_max(data1d, axis=0))