Skip to content

Commit

Permalink
feat: Add 'IntoSeries' (#991)
Browse files Browse the repository at this point in the history

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 17, 2024
1 parent 82b1d6e commit 9b628ee
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/extremes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
nightlies:
strategy:
matrix:
python-version: ["3.11"]
python-version: ["3.12"]
os: [ubuntu-latest]
if: github.event.pull_request.head.repo.full_name == github.repository
runs-on: ${{ matrix.os }}
Expand Down
21 changes: 11 additions & 10 deletions narwhals/stable/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
from narwhals.translate import to_native
from narwhals.typing import IntoDataFrameT
from narwhals.typing import IntoFrameT
from narwhals.typing import IntoSeriesT
from narwhals.utils import is_ordered_categorical as nw_is_ordered_categorical
from narwhals.utils import maybe_align_index as nw_maybe_align_index
from narwhals.utils import maybe_convert_dtypes as nw_maybe_convert_dtypes
Expand Down Expand Up @@ -571,26 +572,26 @@ def _stableify(

@overload
def from_native(
native_dataframe: Any,
native_dataframe: IntoDataFrameT | IntoSeriesT,
*,
strict: Literal[False],
eager_only: None = ...,
eager_or_interchange_only: Literal[True],
series_only: None = ...,
allow_series: Literal[True],
) -> Any: ...
) -> DataFrame[IntoFrameT] | Series: ...


@overload
def from_native(
native_dataframe: Any,
native_dataframe: IntoDataFrameT | IntoSeriesT,
*,
strict: Literal[False],
eager_only: Literal[True],
eager_or_interchange_only: None = ...,
series_only: None = ...,
allow_series: Literal[True],
) -> Any: ...
) -> DataFrame[IntoDataFrameT] | Series: ...


@overload
Expand Down Expand Up @@ -643,26 +644,26 @@ def from_native(

@overload
def from_native(
native_dataframe: Any,
native_dataframe: IntoFrameT | IntoSeriesT,
*,
strict: Literal[False],
eager_only: None = ...,
eager_or_interchange_only: None = ...,
series_only: None = ...,
allow_series: Literal[True],
) -> Any: ...
) -> DataFrame[IntoFrameT] | LazyFrame[IntoFrameT] | Series: ...


@overload
def from_native(
native_dataframe: Any,
native_dataframe: IntoSeriesT,
*,
strict: Literal[False],
eager_only: None = ...,
eager_or_interchange_only: None = ...,
series_only: Literal[True],
allow_series: None = ...,
) -> Any: ...
) -> Series: ...


@overload
Expand Down Expand Up @@ -723,7 +724,7 @@ def from_native(

@overload
def from_native(
native_dataframe: Any,
native_dataframe: IntoFrameT | IntoSeriesT,
*,
strict: Literal[True] = ...,
eager_only: None = ...,
Expand All @@ -739,7 +740,7 @@ def from_native(

@overload
def from_native(
native_dataframe: Any,
native_dataframe: IntoSeriesT | Any, # remain `Any` for downstream compatibility
*,
strict: Literal[True] = ...,
eager_only: None = ...,
Expand Down
9 changes: 9 additions & 0 deletions narwhals/stable/v1/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def columns(self) -> Any: ...

def join(self, *args: Any, **kwargs: Any) -> Any: ...

class NativeSeries(Protocol):
def __len__(self) -> int: ...

class DataFrameLike(Protocol):
def __dataframe__(self, *args: Any, **kwargs: Any) -> Any: ...

Expand All @@ -47,11 +50,15 @@ def __dataframe__(self, *args: Any, **kwargs: Any) -> Any: ...
Frame: TypeAlias = Union["DataFrame[Any]", "LazyFrame[Any]"]
"""Narwhals DataFrame or Narwhals LazyFrame"""

IntoSeries: TypeAlias = Union["Series", "NativeSeries"]
"""Anything which can be converted to a Narwhals Series."""

# TypeVars for some of the above
IntoFrameT = TypeVar("IntoFrameT", bound="IntoFrame")
IntoDataFrameT = TypeVar("IntoDataFrameT", bound="IntoDataFrame")
FrameT = TypeVar("FrameT", bound="Frame")
DataFrameT = TypeVar("DataFrameT", bound="DataFrame[Any]")
IntoSeriesT = TypeVar("IntoSeriesT", bound="IntoSeries")


class DTypes:
Expand Down Expand Up @@ -89,4 +96,6 @@ class DTypes:
"Frame",
"FrameT",
"DataFrameT",
"IntoSeries",
"IntoSeriesT",
]
21 changes: 11 additions & 10 deletions narwhals/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from narwhals.typing import DTypes
from narwhals.typing import IntoDataFrameT
from narwhals.typing import IntoFrameT
from narwhals.typing import IntoSeriesT

T = TypeVar("T")

Expand Down Expand Up @@ -86,26 +87,26 @@ def to_native(

@overload
def from_native(
native_object: Any,
native_object: IntoDataFrameT | IntoSeriesT,
*,
strict: Literal[False],
eager_only: None = ...,
eager_or_interchange_only: Literal[True],
series_only: None = ...,
allow_series: Literal[True],
) -> Any: ...
) -> DataFrame[IntoDataFrameT]: ...


@overload
def from_native(
native_object: Any,
native_object: IntoDataFrameT | IntoSeriesT,
*,
strict: Literal[False],
eager_only: Literal[True],
eager_or_interchange_only: None = ...,
series_only: None = ...,
allow_series: Literal[True],
) -> Any: ...
) -> DataFrame[IntoDataFrameT] | Series: ...


@overload
Expand Down Expand Up @@ -158,26 +159,26 @@ def from_native(

@overload
def from_native(
native_object: Any,
native_object: IntoFrameT | IntoSeriesT,
*,
strict: Literal[False],
eager_only: None = ...,
eager_or_interchange_only: None = ...,
series_only: None = ...,
allow_series: Literal[True],
) -> Any: ...
) -> DataFrame[IntoFrameT] | LazyFrame[IntoFrameT] | Series: ...


@overload
def from_native(
native_object: Any,
native_object: IntoSeriesT,
*,
strict: Literal[False],
eager_only: None = ...,
eager_or_interchange_only: None = ...,
series_only: Literal[True],
allow_series: None = ...,
) -> Any: ...
) -> Series: ...


@overload
Expand Down Expand Up @@ -238,7 +239,7 @@ def from_native(

@overload
def from_native(
native_object: Any,
native_object: IntoFrameT | IntoSeriesT,
*,
strict: Literal[True] = ...,
eager_only: None = ...,
Expand All @@ -254,7 +255,7 @@ def from_native(

@overload
def from_native(
native_object: Any,
native_object: IntoSeriesT,
*,
strict: Literal[True] = ...,
eager_only: None = ...,
Expand Down
9 changes: 9 additions & 0 deletions narwhals/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def columns(self) -> Any: ...

def join(self, *args: Any, **kwargs: Any) -> Any: ...

class NativeSeries(Protocol):
def __len__(self) -> int: ...

class DataFrameLike(Protocol):
def __dataframe__(self, *args: Any, **kwargs: Any) -> Any: ...

Expand All @@ -47,11 +50,15 @@ def __dataframe__(self, *args: Any, **kwargs: Any) -> Any: ...
Frame: TypeAlias = Union["DataFrame[Any]", "LazyFrame[Any]"]
"""Narwhals DataFrame or Narwhals LazyFrame"""

IntoSeries: TypeAlias = Union["Series", "NativeSeries"]
"""Anything which can be converted to a Narwhals Series."""

# TypeVars for some of the above
IntoFrameT = TypeVar("IntoFrameT", bound="IntoFrame")
IntoDataFrameT = TypeVar("IntoDataFrameT", bound="IntoDataFrame")
FrameT = TypeVar("FrameT", bound="Frame")
DataFrameT = TypeVar("DataFrameT", bound="DataFrame[Any]")
IntoSeriesT = TypeVar("IntoSeriesT", bound="IntoSeries")


class DTypes:
Expand Down Expand Up @@ -89,4 +96,6 @@ class DTypes:
"Frame",
"FrameT",
"DataFrameT",
"IntoSeries",
"IntoSeriesT",
]

0 comments on commit 9b628ee

Please sign in to comment.