Skip to content

Commit

Permalink
REFACTOR-#7418: Rename internal interchange protocol methods. (#7422)
Browse files Browse the repository at this point in the history
Rename internal `to_dataframe` methods to `to_interchange_dataframe`, and internal `from_dataframe` methods to `from_interchange_dataframe`.

Signed-off-by: sfc-gh-mvashishtha <[email protected]>
  • Loading branch information
sfc-gh-mvashishtha authored Jan 16, 2025
1 parent caa6116 commit 4cae985
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 29 deletions.
6 changes: 4 additions & 2 deletions modin/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,15 @@ def from_arrow(cls, at, data_cls):
def free(self):
pass

def to_dataframe(self, nan_as_null: bool = False, allow_copy: bool = True):
def to_interchange_dataframe(
self, nan_as_null: bool = False, allow_copy: bool = True
):
raise NotImplementedError(
"The selected execution does not implement the DataFrame exchange protocol."
)

@classmethod
def from_dataframe(cls, df, data_cls):
def from_interchange_dataframe(cls, df, data_cls):
raise NotImplementedError(
"The selected execution does not implement the DataFrame exchange protocol."
)
Expand Down
2 changes: 1 addition & 1 deletion modin/core/dataframe/pandas/dataframe/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4807,7 +4807,7 @@ def __dataframe__(self, nan_as_null: bool = False, allow_copy: bool = True):
)

@classmethod
def from_dataframe(cls, df: ProtocolDataframe) -> PandasDataframe:
def from_interchange_dataframe(cls, df: ProtocolDataframe) -> PandasDataframe:
"""
Convert a DataFrame implementing the dataframe exchange protocol to a Core Modin Dataframe.
Expand Down
6 changes: 3 additions & 3 deletions modin/core/execution/dispatching/factories/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ def from_non_pandas(cls, *args, **kwargs):
return cls.get_factory()._from_non_pandas(*args, **kwargs)

@classmethod
@_inherit_docstrings(factories.BaseFactory._from_dataframe)
def from_dataframe(cls, *args, **kwargs):
return cls.get_factory()._from_dataframe(*args, **kwargs)
@_inherit_docstrings(factories.BaseFactory._from_interchange_dataframe)
def from_interchange_dataframe(cls, *args, **kwargs):
return cls.get_factory()._from_interchange_dataframe(*args, **kwargs)

@classmethod
@_inherit_docstrings(factories.BaseFactory._from_ray)
Expand Down
6 changes: 3 additions & 3 deletions modin/core/execution/dispatching/factories/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ def _from_non_pandas(cls, *args, **kwargs):
_doc_io_method_template,
source="a DataFrame object supporting exchange protocol `__dataframe__()`",
params=_doc_io_method_all_params,
method="io.from_dataframe",
method="io.from_interchange_dataframe",
)
def _from_dataframe(cls, *args, **kwargs):
return cls.io_cls.from_dataframe(*args, **kwargs)
def _from_interchange_dataframe(cls, *args, **kwargs):
return cls.io_cls.from_interchange_dataframe(*args, **kwargs)

@classmethod
@doc(
Expand Down
4 changes: 2 additions & 2 deletions modin/core/io/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def from_arrow(cls, at):
return cls.query_compiler_cls.from_arrow(at, cls.frame_cls)

@classmethod
def from_dataframe(cls, df):
def from_interchange_dataframe(cls, df):
"""
Create a Modin QueryCompiler from a DataFrame supporting the DataFrame exchange protocol `__dataframe__()`.
Expand All @@ -114,7 +114,7 @@ def from_dataframe(cls, df):
BaseQueryCompiler
QueryCompiler containing data from the DataFrame.
"""
return cls.query_compiler_cls.from_dataframe(df, cls.frame_cls)
return cls.query_compiler_cls.from_interchange_dataframe(df, cls.frame_cls)

@classmethod
def from_ray(cls, ray_obj):
Expand Down
11 changes: 8 additions & 3 deletions modin/core/storage_formats/base/query_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
StrDefault,
StructDefault,
)
from modin.core.dataframe.base.interchange.dataframe_protocol.dataframe import (
ProtocolDataframe,
)
from modin.error_message import ErrorMessage
from modin.logging import ClassLogger
from modin.logging.config import LogLevel
Expand Down Expand Up @@ -472,7 +475,9 @@ def to_numpy(self, **kwargs): # noqa: PR02
# Dataframe exchange protocol

@abc.abstractmethod
def to_dataframe(self, nan_as_null: bool = False, allow_copy: bool = True):
def to_interchange_dataframe(
self, nan_as_null: bool = False, allow_copy: bool = True
) -> ProtocolDataframe:
"""
Get a DataFrame exchange protocol object representing data of the Modin DataFrame.
Expand Down Expand Up @@ -501,13 +506,13 @@ def to_dataframe(self, nan_as_null: bool = False, allow_copy: bool = True):

@classmethod
@abc.abstractmethod
def from_dataframe(cls, df, data_cls):
def from_interchange_dataframe(cls, df: ProtocolDataframe, data_cls):
"""
Build QueryCompiler from a DataFrame object supporting the dataframe exchange protocol `__dataframe__()`.
Parameters
----------
df : DataFrame
df : ProtocolDataframe
The DataFrame object supporting the dataframe exchange protocol.
data_cls : type
:py:class:`~modin.core.dataframe.pandas.dataframe.dataframe.PandasDataframe` class
Expand Down
9 changes: 7 additions & 2 deletions modin/core/storage_formats/pandas/native_query_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import pandas
from pandas.core.dtypes.common import is_list_like, is_scalar

from modin.core.dataframe.base.interchange.dataframe_protocol.dataframe import (
ProtocolDataframe,
)
from modin.core.storage_formats.base.query_compiler import BaseQueryCompiler
from modin.core.storage_formats.pandas.query_compiler_caster import QueryCompilerCaster
from modin.utils import (
Expand Down Expand Up @@ -1236,13 +1239,15 @@ def finalize(self):

# Dataframe exchange protocol

def to_dataframe(self, nan_as_null: bool = False, allow_copy: bool = True):
def to_interchange_dataframe(
self, nan_as_null: bool = False, allow_copy: bool = True
):
return self._modin_frame.__dataframe__(
nan_as_null=nan_as_null, allow_copy=allow_copy
)

@classmethod
def from_dataframe(cls, df, data_cls):
def from_interchange_dataframe(cls, df: ProtocolDataframe, data_cls):
return cls(pandas.api.interchange.from_dataframe(df))

# END Dataframe exchange protocol
Expand Down
11 changes: 8 additions & 3 deletions modin/core/storage_formats/pandas/query_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
GroupByDefault,
SeriesGroupByDefault,
)
from modin.core.dataframe.base.interchange.dataframe_protocol.dataframe import (
ProtocolDataframe,
)
from modin.core.dataframe.pandas.metadata import (
DtypesDescriptor,
ModinDtypes,
Expand Down Expand Up @@ -381,14 +384,16 @@ def from_arrow(cls, at, data_cls):

# Dataframe exchange protocol

def to_dataframe(self, nan_as_null: bool = False, allow_copy: bool = True):
def to_interchange_dataframe(
self, nan_as_null: bool = False, allow_copy: bool = True
):
return self._modin_frame.__dataframe__(
nan_as_null=nan_as_null, allow_copy=allow_copy
)

@classmethod
def from_dataframe(cls, df, data_cls):
return cls(data_cls.from_dataframe(df))
def from_interchange_dataframe(cls, df: ProtocolDataframe, data_cls):
return cls(data_cls.from_interchange_dataframe(df))

# END Dataframe exchange protocol

Expand Down
2 changes: 1 addition & 1 deletion modin/pandas/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2917,7 +2917,7 @@ def __dataframe__(self, nan_as_null: bool = False, allow_copy: bool = True):
ProtocolDataframe
A dataframe object following the dataframe protocol specification.
"""
return self._query_compiler.to_dataframe(
return self._query_compiler.to_interchange_dataframe(
nan_as_null=nan_as_null, allow_copy=allow_copy
)

Expand Down
15 changes: 10 additions & 5 deletions modin/pandas/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
from pandas.io.parsers.readers import _c_parser_defaults

from modin.config import ModinNumpy
from modin.core.dataframe.base.interchange.dataframe_protocol.dataframe import (
ProtocolDataframe,
)
from modin.error_message import ErrorMessage
from modin.logging import ClassLogger, enable_logging
from modin.utils import (
Expand Down Expand Up @@ -1013,16 +1016,16 @@ def from_arrow(at) -> DataFrame:
return ModinObjects.DataFrame(query_compiler=FactoryDispatcher.from_arrow(at))


def from_dataframe(df) -> DataFrame:
def from_dataframe(df: ProtocolDataframe) -> DataFrame:
"""
Convert a DataFrame implementing the dataframe exchange protocol to a Modin DataFrame.
Convert a DataFrame implementing the dataframe interchange protocol to a Modin DataFrame.
See more about the protocol in https://data-apis.org/dataframe-protocol/latest/index.html.
Parameters
----------
df : DataFrame
The DataFrame object supporting the dataframe exchange protocol.
df : ProtocolDataframe
An object supporting the dataframe interchange protocol.
Returns
-------
Expand All @@ -1031,7 +1034,9 @@ def from_dataframe(df) -> DataFrame:
"""
from modin.core.execution.dispatching.factories.dispatcher import FactoryDispatcher

return ModinObjects.DataFrame(query_compiler=FactoryDispatcher.from_dataframe(df))
return ModinObjects.DataFrame(
query_compiler=FactoryDispatcher.from_interchange_dataframe(df)
)


def from_ray(ray_obj) -> DataFrame:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def dummy_io_method(*args, **kwargs):
raise TestPassed

query_compiler_cls = get_unique_base_execution
query_compiler_cls.from_dataframe = dummy_io_method
query_compiler_cls.to_dataframe = dummy_io_method
query_compiler_cls.from_interchange_dataframe = dummy_io_method
query_compiler_cls.to_interchange_dataframe = dummy_io_method

from modin.pandas.io import from_dataframe

Expand Down
4 changes: 2 additions & 2 deletions modin/tests/test_executions_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def test_base_abstract_methods():
"from_pandas",
"from_arrow",
"default_to_pandas",
"from_dataframe",
"to_dataframe",
"from_interchange_dataframe",
"to_interchange_dataframe",
]

not_implemented_methods = BASE_EXECUTION.__abstractmethods__.difference(
Expand Down

0 comments on commit 4cae985

Please sign in to comment.