Skip to content

Commit

Permalink
Add support for PandasArray for pandas<2.1.0 (#15786)
Browse files Browse the repository at this point in the history
Only `pandas-2.1.0+` has support for `NumpyExtensionArray` and any version below that only have support for `PandasArray`. This PR make `cudf.pandas` back-ward compatible in that aspect.

Authors:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

Approvers:
  - https://github.com/brandon-b-miller
  - Matthew Roeschke (https://github.com/mroeschke)

URL: #15786
  • Loading branch information
galipremsagar authored May 21, 2024
1 parent 6b1248e commit 1dd1910
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
40 changes: 29 additions & 11 deletions python/cudf/cudf/pandas/_wrappers/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,17 +386,35 @@ def Index__new__(cls, *args, **kwargs):
},
)

NumpyExtensionArray = make_final_proxy_type(
"NumpyExtensionArray",
_Unusable,
pd.arrays.NumpyExtensionArray,
fast_to_slow=_Unusable(),
slow_to_fast=_Unusable(),
additional_attributes={
"_ndarray": _FastSlowAttribute("_ndarray"),
"_dtype": _FastSlowAttribute("_dtype"),
},
)
try:
from pandas.arrays import NumpyExtensionArray as pd_NumpyExtensionArray

NumpyExtensionArray = make_final_proxy_type(
"NumpyExtensionArray",
_Unusable,
pd_NumpyExtensionArray,
fast_to_slow=_Unusable(),
slow_to_fast=_Unusable(),
additional_attributes={
"_ndarray": _FastSlowAttribute("_ndarray"),
"_dtype": _FastSlowAttribute("_dtype"),
},
)

except ImportError:
from pandas.arrays import PandasArray as pd_PandasArray

PandasArray = make_final_proxy_type(
"PandasArray",
_Unusable,
pd_PandasArray,
fast_to_slow=_Unusable(),
slow_to_fast=_Unusable(),
additional_attributes={
"_ndarray": _FastSlowAttribute("_ndarray"),
"_dtype": _FastSlowAttribute("_dtype"),
},
)

TimedeltaArray = make_final_proxy_type(
"TimedeltaArray",
Expand Down
8 changes: 6 additions & 2 deletions python/cudf/cudf_pandas_tests/test_cudf_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1241,8 +1241,12 @@ def test_pickle_groupby(dataframe):

def test_numpy_extension_array():
np_array = np.array([0, 1, 2, 3])
xarray = xpd.arrays.NumpyExtensionArray(np_array)
array = pd.arrays.NumpyExtensionArray(np_array)
try:
xarray = xpd.arrays.NumpyExtensionArray(np_array)
array = pd.arrays.NumpyExtensionArray(np_array)
except AttributeError:
xarray = xpd.arrays.PandasArray(np_array)
array = pd.arrays.PandasArray(np_array)

tm.assert_equal(xarray, array)

Expand Down

0 comments on commit 1dd1910

Please sign in to comment.