|
4 | 4 | import pandas as pd |
5 | 5 | from dask.backends import CreationDispatch |
6 | 6 | from dask.dataframe.backends import DataFrameBackendEntrypoint |
| 7 | +from dask.dataframe.dispatch import to_pandas_dispatch |
7 | 8 |
|
8 | 9 | from dask_expr._dispatch import get_collection_type |
| 10 | +from dask_expr._expr import ToBackend |
9 | 11 |
|
10 | 12 | try: |
11 | 13 | import sparse |
|
32 | 34 | ) |
33 | 35 |
|
34 | 36 |
|
| 37 | +class ToPandasBackend(ToBackend): |
| 38 | + @staticmethod |
| 39 | + def operation(df, options): |
| 40 | + return to_pandas_dispatch(df, **options) |
| 41 | + |
| 42 | + def _simplify_down(self): |
| 43 | + if isinstance(self.frame._meta, (pd.DataFrame, pd.Series, pd.Index)): |
| 44 | + # We already have pandas data |
| 45 | + return self.frame |
| 46 | + |
| 47 | + |
35 | 48 | class PandasBackendEntrypoint(DataFrameBackendEntrypoint): |
36 | 49 | """Pandas-Backend Entrypoint Class for Dask-Expressions |
37 | 50 |
|
38 | 51 | Note that all DataFrame-creation functions are defined |
39 | 52 | and registered 'in-place'. |
40 | 53 | """ |
41 | 54 |
|
42 | | - @classmethod |
43 | | - def to_backend_dispatch(cls): |
44 | | - from dask.dataframe.dispatch import to_pandas_dispatch |
45 | | - |
46 | | - return to_pandas_dispatch |
47 | | - |
48 | 55 | @classmethod |
49 | 56 | def to_backend(cls, data, **kwargs): |
50 | | - if isinstance(data._meta, (pd.DataFrame, pd.Series, pd.Index)): |
51 | | - # Already a pandas-backed collection |
52 | | - return data |
53 | | - return data.map_partitions(cls.to_backend_dispatch(), **kwargs) |
| 57 | + from dask_expr._collection import new_collection |
| 58 | + |
| 59 | + return new_collection(ToPandasBackend(data, kwargs)) |
54 | 60 |
|
55 | 61 |
|
56 | 62 | dataframe_creation_dispatch.register_backend("pandas", PandasBackendEntrypoint()) |
|
0 commit comments