Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add 'IntoSeries' #991

Merged
merged 24 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
73f46e1
Add IntoSeries, IntoSeriesT
luke396 Sep 18, 2024
0b15168
Merge remote-tracking branch 'upstream/main' into add-IntoSeries
luke396 Sep 20, 2024
1cbc80c
Merge remote-tracking branch 'upstream/main' into add-IntoSeries
luke396 Sep 21, 2024
7f86ca7
Merge remote-tracking branch 'upstream/main' into add-IntoSeries
luke396 Sep 24, 2024
12c4510
Merge remote-tracking branch 'upstream/main' into add-IntoSeries
luke396 Sep 25, 2024
054bbda
Merge remote-tracking branch 'upstream/main' into add-IntoSeries
luke396 Sep 26, 2024
9a89301
Merge remote-tracking branch 'upstream/main' into add-IntoSeries
luke396 Sep 28, 2024
39aeaa7
Merge remote-tracking branch 'upstream/main' into add-IntoSeries
luke396 Sep 29, 2024
bd830f2
Merge remote-tracking branch 'upstream/main' into add-IntoSeries
luke396 Oct 1, 2024
568f055
Merge remote-tracking branch 'upstream/main' into add-IntoSeries
luke396 Oct 5, 2024
94a1bd1
WIP
luke396 Oct 5, 2024
6190b51
Revert "WIP"
luke396 Oct 6, 2024
3a2f361
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
5411635
maybe fine
luke396 Oct 6, 2024
8dae16b
Merge remote-tracking branch 'upstream/main' into add-IntoSeries
luke396 Oct 10, 2024
fd02cd9
improve more
luke396 Oct 10, 2024
ea802ac
fix
luke396 Oct 10, 2024
5496920
fix downstream
luke396 Oct 10, 2024
a676d83
fix downstream
luke396 Oct 10, 2024
c0a9f19
fix all
luke396 Oct 10, 2024
7163525
try remove frame
luke396 Oct 16, 2024
ab8dbe5
Merge remote-tracking branch 'upstream/main' into add-IntoSeries
MarcoGorelli Oct 17, 2024
6cca207
fixup annotation, run nightly on 3.12
MarcoGorelli Oct 17, 2024
263c0c5
include in v1
MarcoGorelli Oct 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for https://github.com/vega/altair/blob/796649efd751a7b928ac4f81e192d0ae93f4a3d0/altair/utils/schemapi.py#L502C1-L507C25

def _from_array_like(obj: Iterable[Any], /) -> list[Any]:
    try:
        ser = nw.from_native(obj, strict=True, series_only=True)
        return ser.to_list()
    except TypeError:
        return list(obj)

*,
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",
]
Loading