reduction.pyx 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import numpy as np
  2. cimport numpy as cnp
  3. cnp.import_array()
  4. from pandas._libs.util cimport is_array
  5. cdef cnp.dtype _dtype_obj = np.dtype("object")
  6. cpdef check_result_array(object obj, object dtype):
  7. # Our operation is supposed to be an aggregation/reduction. If
  8. # it returns an ndarray, this likely means an invalid operation has
  9. # been passed. See test_apply_without_aggregation, test_agg_must_agg
  10. if is_array(obj):
  11. if dtype != _dtype_obj:
  12. # If it is object dtype, the function can be a reduction/aggregation
  13. # and still return an ndarray e.g. test_agg_over_numpy_arrays
  14. raise ValueError("Must produce aggregated value")
  15. cpdef inline extract_result(object res):
  16. """ extract the result object, it might be a 0-dim ndarray
  17. or a len-1 0-dim, or a scalar """
  18. if hasattr(res, "_values"):
  19. # Preserve EA
  20. res = res._values
  21. if res.ndim == 1 and len(res) == 1:
  22. # see test_agg_lambda_with_timezone, test_resampler_grouper.py::test_apply
  23. res = res[0]
  24. return res