reduce.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import warnings
  2. import pytest
  3. import pandas as pd
  4. import pandas.util.testing as tm
  5. from .base import BaseExtensionTests
  6. class BaseReduceTests(BaseExtensionTests):
  7. """
  8. Reduction specific tests. Generally these only
  9. make sense for numeric/boolean operations.
  10. """
  11. def check_reduce(self, s, op_name, skipna):
  12. result = getattr(s, op_name)(skipna=skipna)
  13. expected = getattr(s.astype('float64'), op_name)(skipna=skipna)
  14. tm.assert_almost_equal(result, expected)
  15. class BaseNoReduceTests(BaseReduceTests):
  16. """ we don't define any reductions """
  17. @pytest.mark.parametrize('skipna', [True, False])
  18. def test_reduce_series_numeric(self, data, all_numeric_reductions, skipna):
  19. op_name = all_numeric_reductions
  20. s = pd.Series(data)
  21. with pytest.raises(TypeError):
  22. getattr(s, op_name)(skipna=skipna)
  23. @pytest.mark.parametrize('skipna', [True, False])
  24. def test_reduce_series_boolean(self, data, all_boolean_reductions, skipna):
  25. op_name = all_boolean_reductions
  26. s = pd.Series(data)
  27. with pytest.raises(TypeError):
  28. getattr(s, op_name)(skipna=skipna)
  29. class BaseNumericReduceTests(BaseReduceTests):
  30. @pytest.mark.parametrize('skipna', [True, False])
  31. def test_reduce_series(self, data, all_numeric_reductions, skipna):
  32. op_name = all_numeric_reductions
  33. s = pd.Series(data)
  34. # min/max with empty produce numpy warnings
  35. with warnings.catch_warnings():
  36. warnings.simplefilter("ignore", RuntimeWarning)
  37. self.check_reduce(s, op_name, skipna)
  38. class BaseBooleanReduceTests(BaseReduceTests):
  39. @pytest.mark.parametrize('skipna', [True, False])
  40. def test_reduce_series(self, data, all_boolean_reductions, skipna):
  41. op_name = all_boolean_reductions
  42. s = pd.Series(data)
  43. self.check_reduce(s, op_name, skipna)