common.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from __future__ import annotations
  2. from functools import reduce
  3. import numpy as np
  4. from pandas._config import get_option
  5. def ensure_decoded(s) -> str:
  6. """
  7. If we have bytes, decode them to unicode.
  8. """
  9. if isinstance(s, (np.bytes_, bytes)):
  10. s = s.decode(get_option("display.encoding"))
  11. return s
  12. def result_type_many(*arrays_and_dtypes):
  13. """
  14. Wrapper around numpy.result_type which overcomes the NPY_MAXARGS (32)
  15. argument limit.
  16. """
  17. try:
  18. return np.result_type(*arrays_and_dtypes)
  19. except ValueError:
  20. # we have > NPY_MAXARGS terms in our expression
  21. return reduce(np.result_type, arrays_and_dtypes)
  22. except TypeError:
  23. from pandas.core.dtypes.cast import find_common_type
  24. from pandas.core.dtypes.common import is_extension_array_dtype
  25. arr_and_dtypes = list(arrays_and_dtypes)
  26. ea_dtypes, non_ea_dtypes = [], []
  27. for arr_or_dtype in arr_and_dtypes:
  28. if is_extension_array_dtype(arr_or_dtype):
  29. ea_dtypes.append(arr_or_dtype)
  30. else:
  31. non_ea_dtypes.append(arr_or_dtype)
  32. if non_ea_dtypes:
  33. try:
  34. np_dtype = np.result_type(*non_ea_dtypes)
  35. except ValueError:
  36. np_dtype = reduce(np.result_type, arrays_and_dtypes)
  37. return find_common_type(ea_dtypes + [np_dtype])
  38. return find_common_type(ea_dtypes)