test_deprecations.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """Test deprecation and future warnings.
  2. """
  3. import numpy as np
  4. from numpy.testing import assert_warns
  5. from numpy.ma.testutils import assert_equal
  6. from numpy.ma.core import MaskedArrayFutureWarning
  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 = assert_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_minimum(self):
  30. assert_warns(DeprecationWarning, np.ma.minimum, np.ma.array([1, 2]))
  31. def test_maximum(self):
  32. assert_warns(DeprecationWarning, np.ma.maximum, np.ma.array([1, 2]))
  33. def test_axis_default(self):
  34. # NumPy 1.13, 2017-05-06
  35. data1d = np.ma.arange(6)
  36. data2d = data1d.reshape(2, 3)
  37. ma_min = np.ma.minimum.reduce
  38. ma_max = np.ma.maximum.reduce
  39. # check that the default axis is still None, but warns on 2d arrays
  40. result = assert_warns(MaskedArrayFutureWarning, ma_max, data2d)
  41. assert_equal(result, ma_max(data2d, axis=None))
  42. result = assert_warns(MaskedArrayFutureWarning, ma_min, data2d)
  43. assert_equal(result, ma_min(data2d, axis=None))
  44. # no warnings on 1d, as both new and old defaults are equivalent
  45. result = ma_min(data1d)
  46. assert_equal(result, ma_min(data1d, axis=None))
  47. assert_equal(result, ma_min(data1d, axis=0))
  48. result = ma_max(data1d)
  49. assert_equal(result, ma_max(data1d, axis=None))
  50. assert_equal(result, ma_max(data1d, axis=0))