accumulate.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import pytest
  2. import pandas as pd
  3. import pandas._testing as tm
  4. class BaseAccumulateTests:
  5. """
  6. Accumulation specific tests. Generally these only
  7. make sense for numeric/boolean operations.
  8. """
  9. def _supports_accumulation(self, ser: pd.Series, op_name: str) -> bool:
  10. # Do we expect this accumulation to be supported for this dtype?
  11. # We default to assuming "no"; subclass authors should override here.
  12. return False
  13. def check_accumulate(self, ser: pd.Series, op_name: str, skipna: bool):
  14. try:
  15. alt = ser.astype("float64")
  16. except (TypeError, ValueError):
  17. # e.g. Period can't be cast to float64 (TypeError)
  18. # String can't be cast to float64 (ValueError)
  19. alt = ser.astype(object)
  20. result = getattr(ser, op_name)(skipna=skipna)
  21. expected = getattr(alt, op_name)(skipna=skipna)
  22. tm.assert_series_equal(result, expected, check_dtype=False)
  23. @pytest.mark.parametrize("skipna", [True, False])
  24. def test_accumulate_series(self, data, all_numeric_accumulations, skipna):
  25. op_name = all_numeric_accumulations
  26. ser = pd.Series(data)
  27. if self._supports_accumulation(ser, op_name):
  28. self.check_accumulate(ser, op_name, skipna)
  29. else:
  30. with pytest.raises((NotImplementedError, TypeError)):
  31. # TODO: require TypeError for things that will _never_ work?
  32. getattr(ser, op_name)(skipna=skipna)