test_deprecations.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """Test deprecation and future warnings.
  2. """
  3. import pytest
  4. import numpy as np
  5. from numpy.testing import assert_warns
  6. from numpy.ma.testutils import assert_equal
  7. from numpy.ma.core import MaskedArrayFutureWarning
  8. import io
  9. import textwrap
  10. class TestArgsort:
  11. """ gh-8701 """
  12. def _test_base(self, argsort, cls):
  13. arr_0d = np.array(1).view(cls)
  14. argsort(arr_0d)
  15. arr_1d = np.array([1, 2, 3]).view(cls)
  16. argsort(arr_1d)
  17. # argsort has a bad default for >1d arrays
  18. arr_2d = np.array([[1, 2], [3, 4]]).view(cls)
  19. result = assert_warns(
  20. np.ma.core.MaskedArrayFutureWarning, argsort, arr_2d)
  21. assert_equal(result, argsort(arr_2d, axis=None))
  22. # should be no warnings for explicitly specifying it
  23. argsort(arr_2d, axis=None)
  24. argsort(arr_2d, axis=-1)
  25. def test_function_ndarray(self):
  26. return self._test_base(np.ma.argsort, np.ndarray)
  27. def test_function_maskedarray(self):
  28. return self._test_base(np.ma.argsort, np.ma.MaskedArray)
  29. def test_method(self):
  30. return self._test_base(np.ma.MaskedArray.argsort, np.ma.MaskedArray)
  31. class TestMinimumMaximum:
  32. def test_axis_default(self):
  33. # NumPy 1.13, 2017-05-06
  34. data1d = np.ma.arange(6)
  35. data2d = data1d.reshape(2, 3)
  36. ma_min = np.ma.minimum.reduce
  37. ma_max = np.ma.maximum.reduce
  38. # check that the default axis is still None, but warns on 2d arrays
  39. result = assert_warns(MaskedArrayFutureWarning, ma_max, data2d)
  40. assert_equal(result, ma_max(data2d, axis=None))
  41. result = assert_warns(MaskedArrayFutureWarning, ma_min, data2d)
  42. assert_equal(result, ma_min(data2d, axis=None))
  43. # no warnings on 1d, as both new and old defaults are equivalent
  44. result = ma_min(data1d)
  45. assert_equal(result, ma_min(data1d, axis=None))
  46. assert_equal(result, ma_min(data1d, axis=0))
  47. result = ma_max(data1d)
  48. assert_equal(result, ma_max(data1d, axis=None))
  49. assert_equal(result, ma_max(data1d, axis=0))
  50. class TestFromtextfile:
  51. def test_fromtextfile_delimitor(self):
  52. # NumPy 1.22.0, 2021-09-23
  53. textfile = io.StringIO(textwrap.dedent(
  54. """
  55. A,B,C,D
  56. 'string 1';1;1.0;'mixed column'
  57. 'string 2';2;2.0;
  58. 'string 3';3;3.0;123
  59. 'string 4';4;4.0;3.14
  60. """
  61. ))
  62. with pytest.warns(DeprecationWarning):
  63. result = np.ma.mrecords.fromtextfile(textfile, delimitor=';')