util.py 899 B

123456789101112131415161718192021222324252627282930
  1. from pandas import (
  2. Categorical,
  3. Index,
  4. Series,
  5. )
  6. from pandas.core.arrays import BaseMaskedArray
  7. def get_array(obj, col=None):
  8. """
  9. Helper method to get array for a DataFrame column or a Series.
  10. Equivalent of df[col].values, but without going through normal getitem,
  11. which triggers tracking references / CoW (and we might be testing that
  12. this is done by some other operation).
  13. """
  14. if isinstance(obj, Index):
  15. arr = obj._values
  16. elif isinstance(obj, Series) and (col is None or obj.name == col):
  17. arr = obj._values
  18. else:
  19. assert col is not None
  20. icol = obj.columns.get_loc(col)
  21. assert isinstance(icol, int)
  22. arr = obj._get_column_array(icol)
  23. if isinstance(arr, BaseMaskedArray):
  24. return arr._data
  25. elif isinstance(arr, Categorical):
  26. return arr
  27. return getattr(arr, "_ndarray", arr)