1818# with this module https://github.com/rapidsai/cudf/issues/14521#issue-2015198786
1919import pyarrow .dataset as ds # noqa: F401
2020from pandas ._testing import at , getitem , iat , iloc , loc , setitem
21+ from pandas .compat ._optional import import_optional_dependency
2122from pandas .tseries .holiday import (
2223 AbstractHolidayCalendar as pd_AbstractHolidayCalendar ,
2324 EasterMonday as pd_EasterMonday ,
@@ -314,6 +315,20 @@ def _DataFrame_columns(self):
314315 return result
315316
316317
318+ def _to_xarray (self ):
319+ # Call xarray conversion functions directly with self (the proxy object).
320+ # We must pass the proxy (self), not the slow pandas object, because xarray
321+ # does isinstance checks against pd.MultiIndex and pd.api.extensions.ExtensionArray.
322+ # After cudf.pandas.install(), these refer to proxy classes. The slow object
323+ # contains real pandas types that don't pass isinstance checks against the proxy
324+ # classes.
325+ xr = import_optional_dependency ("xarray" )
326+ if self .ndim == 1 :
327+ return xr .DataArray .from_series (self )
328+ else :
329+ return xr .Dataset .from_dataframe (self )
330+
331+
317332DataFrame = make_final_proxy_type (
318333 "DataFrame" ,
319334 cudf .DataFrame ,
@@ -346,6 +361,7 @@ def _DataFrame_columns(self):
346361 "flags" : _FastSlowAttribute ("flags" , private = True ),
347362 "memory_usage" : _FastSlowAttribute ("memory_usage" ),
348363 "__sizeof__" : _FastSlowAttribute ("__sizeof__" ),
364+ "to_xarray" : _to_xarray ,
349365 },
350366)
351367
@@ -419,6 +435,7 @@ def _argsort(self, *args, **kwargs):
419435 "_accessors" : set (),
420436 "dtype" : property (_Series_dtype ),
421437 "argsort" : _argsort ,
438+ "to_xarray" : _to_xarray ,
422439 "attrs" : _FastSlowAttribute ("attrs" ),
423440 "_mgr" : _FastSlowAttribute ("_mgr" , private = True ),
424441 "array" : _FastSlowAttribute ("array" , private = True ),
@@ -537,6 +554,7 @@ def Index__setattr__(self, name, value):
537554 fast_to_slow = lambda fast : fast ,
538555 slow_to_fast = lambda slow : slow ,
539556 additional_attributes = {
557+ "__array_ufunc__" : _FastSlowAttribute ("__array_ufunc__" ),
540558 "__from_arrow__" : _FastSlowAttribute ("__from_arrow__" ),
541559 "__hash__" : _FastSlowAttribute ("__hash__" ),
542560 "pyarrow_dtype" : _FastSlowAttribute ("pyarrow_dtype" ),
@@ -575,6 +593,10 @@ def Index__setattr__(self, name, value):
575593 pd .Categorical ,
576594 fast_to_slow = _Unusable (),
577595 slow_to_fast = _Unusable (),
596+ bases = (pd .api .extensions .ExtensionArray ,),
597+ additional_attributes = {
598+ "__array_ufunc__" : _FastSlowAttribute ("__array_ufunc__" ),
599+ },
578600)
579601
580602CategoricalDtype = make_final_proxy_type (
@@ -614,7 +636,9 @@ def Index__setattr__(self, name, value):
614636 pd .arrays .DatetimeArray ,
615637 fast_to_slow = _Unusable (),
616638 slow_to_fast = _Unusable (),
639+ bases = (pd .api .extensions .ExtensionArray ,),
617640 additional_attributes = {
641+ "__array_ufunc__" : _FastSlowAttribute ("__array_ufunc__" ),
618642 "_data" : _FastSlowAttribute ("_data" , private = True ),
619643 "_mask" : _FastSlowAttribute ("_mask" , private = True ),
620644 },
@@ -687,7 +711,9 @@ def Index__setattr__(self, name, value):
687711 pd .arrays .TimedeltaArray ,
688712 fast_to_slow = _Unusable (),
689713 slow_to_fast = _Unusable (),
714+ bases = (pd .api .extensions .ExtensionArray ,),
690715 additional_attributes = {
716+ "__array_ufunc__" : _FastSlowAttribute ("__array_ufunc__" ),
691717 "_data" : _FastSlowAttribute ("_data" , private = True ),
692718 "_mask" : _FastSlowAttribute ("_mask" , private = True ),
693719 },
@@ -717,6 +743,7 @@ def Index__setattr__(self, name, value):
717743 pd .arrays .PeriodArray ,
718744 fast_to_slow = _Unusable (),
719745 slow_to_fast = _Unusable (),
746+ bases = (pd .api .extensions .ExtensionArray ,),
720747 additional_attributes = {
721748 "_data" : _FastSlowAttribute ("_data" , private = True ),
722749 "_mask" : _FastSlowAttribute ("_mask" , private = True ),
@@ -799,11 +826,13 @@ def Index__setattr__(self, name, value):
799826 pd .arrays .StringArray ,
800827 fast_to_slow = _Unusable (),
801828 slow_to_fast = _Unusable (),
829+ bases = (pd .api .extensions .ExtensionArray ,),
802830 additional_attributes = {
803831 "_data" : _FastSlowAttribute ("_data" , private = True ),
804832 "_mask" : _FastSlowAttribute ("_mask" , private = True ),
805833 "__array__" : _FastSlowAttribute ("__array__" ),
806834 "__array_ufunc__" : _FastSlowAttribute ("__array_ufunc__" ),
835+ "__arrow_array__" : _FastSlowAttribute ("__arrow_array__" ),
807836 },
808837)
809838
@@ -835,6 +864,7 @@ def Index__setattr__(self, name, value):
835864 pd .core .arrays .string_arrow .ArrowStringArray ,
836865 fast_to_slow = _Unusable (),
837866 slow_to_fast = _Unusable (),
867+ bases = (pd .api .extensions .ExtensionArray ,),
838868 additional_attributes = {
839869 "_pa_array" : _FastSlowAttribute ("_pa_array" , private = True ),
840870 "__array__" : _FastSlowAttribute ("__array__" , private = True ),
@@ -844,6 +874,7 @@ def Index__setattr__(self, name, value):
844874 "__abs__" : _FastSlowAttribute ("__abs__" ),
845875 "__contains__" : _FastSlowAttribute ("__contains__" ),
846876 "__array_ufunc__" : _FastSlowAttribute ("__array_ufunc__" ),
877+ "__arrow_array__" : _FastSlowAttribute ("__arrow_array__" ),
847878 },
848879)
849880
@@ -874,6 +905,7 @@ def Index__setattr__(self, name, value):
874905 pd .arrays .BooleanArray ,
875906 fast_to_slow = _Unusable (),
876907 slow_to_fast = _Unusable (),
908+ bases = (pd .api .extensions .ExtensionArray ,),
877909 additional_attributes = {
878910 "_data" : _FastSlowAttribute ("_data" , private = True ),
879911 "_mask" : _FastSlowAttribute ("_mask" , private = True ),
@@ -898,6 +930,7 @@ def Index__setattr__(self, name, value):
898930 pd .arrays .IntegerArray ,
899931 fast_to_slow = _Unusable (),
900932 slow_to_fast = _Unusable (),
933+ bases = (pd .api .extensions .ExtensionArray ,),
901934 additional_attributes = {
902935 "__array_ufunc__" : _FastSlowAttribute ("__array_ufunc__" ),
903936 "_data" : _FastSlowAttribute ("_data" , private = True ),
@@ -1018,7 +1051,9 @@ def Index__setattr__(self, name, value):
10181051 pd .arrays .IntervalArray ,
10191052 fast_to_slow = _Unusable (),
10201053 slow_to_fast = _Unusable (),
1054+ bases = (pd .api .extensions .ExtensionArray ,),
10211055 additional_attributes = {
1056+ "__array_ufunc__" : _FastSlowAttribute ("__array_ufunc__" ),
10221057 "_data" : _FastSlowAttribute ("_data" , private = True ),
10231058 "_mask" : _FastSlowAttribute ("_mask" , private = True ),
10241059 },
@@ -1052,6 +1087,7 @@ def Index__setattr__(self, name, value):
10521087 pd .arrays .FloatingArray ,
10531088 fast_to_slow = _Unusable (),
10541089 slow_to_fast = _Unusable (),
1090+ bases = (pd .api .extensions .ExtensionArray ,),
10551091 additional_attributes = {
10561092 "__array_ufunc__" : _FastSlowAttribute ("__array_ufunc__" ),
10571093 "_data" : _FastSlowAttribute ("_data" , private = True ),
@@ -1081,6 +1117,7 @@ def Index__setattr__(self, name, value):
10811117 },
10821118)
10831119
1120+
10841121SeriesGroupBy = make_intermediate_proxy_type (
10851122 "SeriesGroupBy" ,
10861123 cudf .core .groupby .groupby .SeriesGroupBy ,
0 commit comments