common.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from __future__ import annotations
  2. from pandas._typing import AxisInt
  3. from pandas import (
  4. DataFrame,
  5. concat,
  6. )
  7. def _check_mixed_float(df, dtype=None):
  8. # float16 are most likely to be upcasted to float32
  9. dtypes = {"A": "float32", "B": "float32", "C": "float16", "D": "float64"}
  10. if isinstance(dtype, str):
  11. dtypes = {k: dtype for k, v in dtypes.items()}
  12. elif isinstance(dtype, dict):
  13. dtypes.update(dtype)
  14. if dtypes.get("A"):
  15. assert df.dtypes["A"] == dtypes["A"]
  16. if dtypes.get("B"):
  17. assert df.dtypes["B"] == dtypes["B"]
  18. if dtypes.get("C"):
  19. assert df.dtypes["C"] == dtypes["C"]
  20. if dtypes.get("D"):
  21. assert df.dtypes["D"] == dtypes["D"]
  22. def _check_mixed_int(df, dtype=None):
  23. dtypes = {"A": "int32", "B": "uint64", "C": "uint8", "D": "int64"}
  24. if isinstance(dtype, str):
  25. dtypes = {k: dtype for k, v in dtypes.items()}
  26. elif isinstance(dtype, dict):
  27. dtypes.update(dtype)
  28. if dtypes.get("A"):
  29. assert df.dtypes["A"] == dtypes["A"]
  30. if dtypes.get("B"):
  31. assert df.dtypes["B"] == dtypes["B"]
  32. if dtypes.get("C"):
  33. assert df.dtypes["C"] == dtypes["C"]
  34. if dtypes.get("D"):
  35. assert df.dtypes["D"] == dtypes["D"]
  36. def zip_frames(frames: list[DataFrame], axis: AxisInt = 1) -> DataFrame:
  37. """
  38. take a list of frames, zip them together under the
  39. assumption that these all have the first frames' index/columns.
  40. Returns
  41. -------
  42. new_frame : DataFrame
  43. """
  44. if axis == 1:
  45. columns = frames[0].columns
  46. zipped = [f.loc[:, c] for c in columns for f in frames]
  47. return concat(zipped, axis=1)
  48. else:
  49. index = frames[0].index
  50. zipped = [f.loc[i, :] for i in index for f in frames]
  51. return DataFrame(zipped)