buffer.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from __future__ import annotations
  2. import numpy as np
  3. from pandas.core.interchange.dataframe_protocol import (
  4. Buffer,
  5. DlpackDeviceType,
  6. )
  7. from pandas.util.version import Version
  8. _NUMPY_HAS_DLPACK = Version(np.__version__) >= Version("1.22.0")
  9. class PandasBuffer(Buffer):
  10. """
  11. Data in the buffer is guaranteed to be contiguous in memory.
  12. """
  13. def __init__(self, x: np.ndarray, allow_copy: bool = True) -> None:
  14. """
  15. Handle only regular columns (= numpy arrays) for now.
  16. """
  17. if not x.strides == (x.dtype.itemsize,):
  18. # The protocol does not support strided buffers, so a copy is
  19. # necessary. If that's not allowed, we need to raise an exception.
  20. if allow_copy:
  21. x = x.copy()
  22. else:
  23. raise RuntimeError(
  24. "Exports cannot be zero-copy in the case "
  25. "of a non-contiguous buffer"
  26. )
  27. # Store the numpy array in which the data resides as a private
  28. # attribute, so we can use it to retrieve the public attributes
  29. self._x = x
  30. @property
  31. def bufsize(self) -> int:
  32. """
  33. Buffer size in bytes.
  34. """
  35. return self._x.size * self._x.dtype.itemsize
  36. @property
  37. def ptr(self) -> int:
  38. """
  39. Pointer to start of the buffer as an integer.
  40. """
  41. return self._x.__array_interface__["data"][0]
  42. def __dlpack__(self):
  43. """
  44. Represent this structure as DLPack interface.
  45. """
  46. if _NUMPY_HAS_DLPACK:
  47. return self._x.__dlpack__()
  48. raise NotImplementedError("__dlpack__")
  49. def __dlpack_device__(self) -> tuple[DlpackDeviceType, int | None]:
  50. """
  51. Device type and device ID for where the data in the buffer resides.
  52. """
  53. return (DlpackDeviceType.CPU, None)
  54. def __repr__(self) -> str:
  55. return (
  56. "PandasBuffer("
  57. + str(
  58. {
  59. "bufsize": self.bufsize,
  60. "ptr": self.ptr,
  61. "device": self.__dlpack_device__()[0].name,
  62. }
  63. )
  64. + ")"
  65. )