From 02d5e83592619e55fd297515e814cbdbfdf9c205 Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Wed, 21 Sep 2022 15:50:25 +0200 Subject: [PATCH] Add a `__dataframe__` method to the protocol dataframe object (#11692) This will align the implementation with those in other libraries, xref https://github.com/data-apis/dataframe-api/issues/80. Cc @shwina Authors: - Ralf Gommers (https://github.com/rgommers) Approvers: - Ashwin Srinath (https://github.com/shwina) URL: https://github.com/rapidsai/cudf/pull/11692 --- python/cudf/cudf/core/df_protocol.py | 10 ++++++++++ python/cudf/cudf/tests/test_df_protocol.py | 3 +++ 2 files changed, 13 insertions(+) diff --git a/python/cudf/cudf/core/df_protocol.py b/python/cudf/cudf/core/df_protocol.py index ad352dc6543..d770f4f6130 100644 --- a/python/cudf/cudf/core/df_protocol.py +++ b/python/cudf/cudf/core/df_protocol.py @@ -529,6 +529,16 @@ def __init__( self._nan_as_null = nan_as_null self._allow_copy = allow_copy + def __dataframe__( + self, nan_as_null: bool = False, allow_copy: bool = True + ) -> "_CuDFDataFrame": + """ + See the docstring of the `cudf.DataFrame.__dataframe__` for details + """ + return _CuDFDataFrame( + self._df, nan_as_null=nan_as_null, allow_copy=allow_copy + ) + @property def metadata(self): # `index` isn't a regular column, and the protocol doesn't support row diff --git a/python/cudf/cudf/tests/test_df_protocol.py b/python/cudf/cudf/tests/test_df_protocol.py index c88b6ac9228..7b83eec9b63 100644 --- a/python/cudf/cudf/tests/test_df_protocol.py +++ b/python/cudf/cudf/tests/test_df_protocol.py @@ -124,6 +124,9 @@ def test_from_dataframe(): df2 = cudf.from_dataframe(df1) assert_eq(df1, df2) + df3 = cudf.from_dataframe(df2) + assert_eq(df1, df3) + def test_int_dtype(): data_int = dict(a=[1, 2, 3], b=[9, 10, 11])