test_reduction.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import numpy as np
  2. import pytest
  3. import pandas as pd
  4. @pytest.fixture
  5. def data():
  6. """Fixture returning boolean array, with valid and missing values."""
  7. return pd.array(
  8. [True, False] * 4 + [np.nan] + [True, False] * 44 + [np.nan] + [True, False],
  9. dtype="boolean",
  10. )
  11. @pytest.mark.parametrize(
  12. "values, exp_any, exp_all, exp_any_noskip, exp_all_noskip",
  13. [
  14. ([True, pd.NA], True, True, True, pd.NA),
  15. ([False, pd.NA], False, False, pd.NA, False),
  16. ([pd.NA], False, True, pd.NA, pd.NA),
  17. ([], False, True, False, True),
  18. # GH-33253: all True / all False values buggy with skipna=False
  19. ([True, True], True, True, True, True),
  20. ([False, False], False, False, False, False),
  21. ],
  22. )
  23. def test_any_all(values, exp_any, exp_all, exp_any_noskip, exp_all_noskip):
  24. # the methods return numpy scalars
  25. exp_any = pd.NA if exp_any is pd.NA else np.bool_(exp_any)
  26. exp_all = pd.NA if exp_all is pd.NA else np.bool_(exp_all)
  27. exp_any_noskip = pd.NA if exp_any_noskip is pd.NA else np.bool_(exp_any_noskip)
  28. exp_all_noskip = pd.NA if exp_all_noskip is pd.NA else np.bool_(exp_all_noskip)
  29. for con in [pd.array, pd.Series]:
  30. a = con(values, dtype="boolean")
  31. assert a.any() is exp_any
  32. assert a.all() is exp_all
  33. assert a.any(skipna=False) is exp_any_noskip
  34. assert a.all(skipna=False) is exp_all_noskip
  35. assert np.any(a.any()) is exp_any
  36. assert np.all(a.all()) is exp_all
  37. @pytest.mark.parametrize("dropna", [True, False])
  38. def test_reductions_return_types(dropna, data, all_numeric_reductions):
  39. op = all_numeric_reductions
  40. s = pd.Series(data)
  41. if dropna:
  42. s = s.dropna()
  43. if op in ("sum", "prod"):
  44. assert isinstance(getattr(s, op)(), np.int_)
  45. elif op == "count":
  46. # Oddly on the 32 bit build (but not Windows), this is intc (!= intp)
  47. assert isinstance(getattr(s, op)(), np.integer)
  48. elif op in ("min", "max"):
  49. assert isinstance(getattr(s, op)(), np.bool_)
  50. else:
  51. # "mean", "std", "var", "median", "kurt", "skew"
  52. assert isinstance(getattr(s, op)(), np.float64)