Skip to content

Commit

Permalink
translate_series
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Feb 24, 2024
1 parent 283f923 commit 3329bd8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 18 deletions.
4 changes: 2 additions & 2 deletions narwhals/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from narwhals import containers
from narwhals.translate import to_original_object
from narwhals.translate import translate_frame
from narwhals.translate import translate_series

__version__ = "0.1.13"

__all__ = ["translate_frame", "to_original_object", "containers"]
__all__ = ["translate_frame", "translate_series", "containers"]
25 changes: 19 additions & 6 deletions narwhals/pandas_like/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
from narwhals.pandas_like.dataframe import DataFrame
from narwhals.pandas_like.dataframe import LazyFrame
from narwhals.pandas_like.namespace import Namespace
from narwhals.pandas_like.series import Series


@overload
def translate(
def translate_frame(
df: Any,
implementation: str,
*,
Expand All @@ -23,7 +24,7 @@ def translate(


@overload
def translate(
def translate_frame(
df: Any,
implementation: str,
*,
Expand All @@ -34,7 +35,7 @@ def translate(


@overload
def translate(
def translate_frame(
df: Any,
implementation: str,
*,
Expand All @@ -45,7 +46,7 @@ def translate(


@overload
def translate(
def translate_frame(
df: Any,
implementation: str,
*,
Expand All @@ -55,7 +56,7 @@ def translate(
...


def translate(
def translate_frame(
df: Any,
implementation: str,
*,
Expand All @@ -76,4 +77,16 @@ def translate(
df,
implementation=implementation,
)
return df, Namespace(implementation=df._implementation)
return df, Namespace(implementation=implementation)


def translate_series(
series: Any,
implementation: str,
) -> tuple[Series, Namespace]:
from narwhals.pandas_like.namespace import Namespace
from narwhals.pandas_like.series import Series

return Series(series, implementation=implementation), Namespace(
implementation=implementation
)
49 changes: 39 additions & 10 deletions narwhals/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from narwhals.spec import DataFrame
from narwhals.spec import LazyFrame
from narwhals.spec import Namespace
from narwhals.spec import Series


@overload
Expand Down Expand Up @@ -86,34 +87,62 @@ def translate_frame(
return LazyFrame(df), Namespace()

if (pd := get_pandas()) is not None and isinstance(df, pd.DataFrame):
from narwhals.pandas_like.translate import translate
from narwhals.pandas_like.translate import translate_frame

return translate(
return translate_frame(
df,
implementation="pandas",
eager_only=eager_only,
lazy_only=lazy_only,
)

if (cudf := get_cudf()) is not None and isinstance(df, cudf.DataFrame):
from narwhals.pandas_like.translate import translate
from narwhals.pandas_like.translate import translate_frame

return translate(
return translate_frame(
df, implementation="cudf", eager_only=eager_only, lazy_only=lazy_only
)

if (mpd := get_modin()) is not None and isinstance(df, mpd.DataFrame):
from narwhals.pandas_like.translate import translate
from narwhals.pandas_like.translate import translate_frame

return translate(
return translate_frame(
df, implementation="modin", eager_only=eager_only, lazy_only=lazy_only
)

msg = f"Could not translate DataFrame {type(df)}, please open a feature request."
raise TypeError(msg)


def to_original_object(df: DataFrame | LazyFrame) -> Any:
if (pl := get_polars()) is not None and isinstance(df, (pl.DataFrame, pl.LazyFrame)):
return df
return df._dataframe # type: ignore[union-attr]
def translate_series(
series: Any,
) -> tuple[Series, Namespace]:
if hasattr(series, "__narwhals_series__"):
return series.__narwhals_series__() # type: ignore[no-any-return]

if (pl := get_polars()) is not None and isinstance(series, pl.Series):
from narwhals.polars import Namespace
from narwhals.polars import Series

return Series(series), Namespace()

if (pd := get_pandas()) is not None and isinstance(series, pd.DataFrame):
from narwhals.pandas_like.translate import translate_series

return translate_series(
series,
implementation="pandas",
)

if (cudf := get_cudf()) is not None and isinstance(series, cudf.DataFrame):
from narwhals.pandas_like.translate import translate_series

return translate_series(series, implementation="cudf")

if (mpd := get_modin()) is not None and isinstance(series, mpd.DataFrame):
from narwhals.pandas_like.translate import translate_series

return translate_series(series, implementation="modin")

msg = f"Could not translate DataFrame {type(series)}, please open a feature request."
raise TypeError(msg)

0 comments on commit 3329bd8

Please sign in to comment.