-
Notifications
You must be signed in to change notification settings - Fork 170
feat: Adds DataFrame.iter_columns
#2104
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
Changes from all commits
9e381de
fdf3920
61c72db
6cf1b1e
d2127ef
e76d05f
0aa730a
68f2e35
0ca6554
fbe4b64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| - is_empty | ||
| - is_unique | ||
| - item | ||
| - iter_columns | ||
| - iter_rows | ||
| - join | ||
| - join_asof | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1235,6 +1235,37 @@ def rows( | |||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| return self._compliant_frame.rows(named=named) # type: ignore[no-any-return] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def iter_columns(self: Self) -> Iterator[Series[Any]]: | ||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Lines 72 to 86 in 1abc05a
On the public side, it would mean adding: # narwhals.typing.py
SeriesT_co = TypeVar("SeriesT_co", bound="Series[Any]", covariant=True)And making changes like this: # narwhals.dataframe.py
from narwhals.typing import SeriesT_co
class DataFrame(BaseFrame[DataFrameT], Generic[SeriesT_co]):
def get_column(self: Self, name: str) -> SeriesT_co: ...
def iter_columns(self: Self) -> Iterator[SeriesT_co]: ...
...So then you'd have things like: DataFrame[pd.DataFrame, pd.Series]
DataFrame[pl.DataFrame, pl.Series]
DataFrame[pa.Table, pa.ChunkedArray]The link between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would β€οΈ love β€οΈ to have this |
||||||||||||||||||||||||||||||||
| """Returns an iterator over the columns of this DataFrame. | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| Yields: | ||||||||||||||||||||||||||||||||
| A Narwhals Series, backed by a native series. | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| Examples: | ||||||||||||||||||||||||||||||||
| >>> import pandas as pd | ||||||||||||||||||||||||||||||||
| >>> import narwhals as nw | ||||||||||||||||||||||||||||||||
| >>> df_native = pd.DataFrame({"foo": [1, 2], "bar": [6.0, 7.0]}) | ||||||||||||||||||||||||||||||||
| >>> iter_columns = nw.from_native(df_native).iter_columns() | ||||||||||||||||||||||||||||||||
| >>> next(iter_columns) | ||||||||||||||||||||||||||||||||
| βββββββββββββββββββββββββ | ||||||||||||||||||||||||||||||||
| | Narwhals Series | | ||||||||||||||||||||||||||||||||
| |-----------------------| | ||||||||||||||||||||||||||||||||
| |0 1 | | ||||||||||||||||||||||||||||||||
| |1 2 | | ||||||||||||||||||||||||||||||||
| |Name: foo, dtype: int64| | ||||||||||||||||||||||||||||||||
| βββββββββββββββββββββββββ | ||||||||||||||||||||||||||||||||
| >>> next(iter_columns) | ||||||||||||||||||||||||||||||||
| βββββββββββββββββββββββββββ | ||||||||||||||||||||||||||||||||
| | Narwhals Series | | ||||||||||||||||||||||||||||||||
| |-------------------------| | ||||||||||||||||||||||||||||||||
| |0 6.0 | | ||||||||||||||||||||||||||||||||
| |1 7.0 | | ||||||||||||||||||||||||||||||||
| |Name: bar, dtype: float64| | ||||||||||||||||||||||||||||||||
| βββββββββββββββββββββββββββ | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| for series in self._compliant_frame.iter_columns(): | ||||||||||||||||||||||||||||||||
| yield self._series(series, level=self._level) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @overload | ||||||||||||||||||||||||||||||||
| def iter_rows( | ||||||||||||||||||||||||||||||||
| self: Self, *, named: Literal[False], buffer_size: int = ... | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,9 @@ | |||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if TYPE_CHECKING: | ||||||||||||||||||||||||||||
| from tests.utils import Constructor | ||||||||||||||||||||||||||||
| from tests.utils import ConstructorEager | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8.0, 9.0]} | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @pytest.mark.filterwarnings("ignore:Determining|Resolving.*") | ||||||||||||||||||||||||||||
|
|
@@ -17,3 +20,10 @@ def test_columns(constructor: Constructor) -> None: | |||||||||||||||||||||||||||
| result = df.columns | ||||||||||||||||||||||||||||
| expected = ["a", "b", "z"] | ||||||||||||||||||||||||||||
| assert result == expected | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_iter_columns(constructor_eager: ConstructorEager) -> None: | ||||||||||||||||||||||||||||
| df = nw.from_native(constructor_eager(data), eager_only=True) | ||||||||||||||||||||||||||||
| expected = df.to_dict(as_series=True) | ||||||||||||||||||||||||||||
| result = {series.name: series for series in df.iter_columns()} | ||||||||||||||||||||||||||||
| assert result == expected | ||||||||||||||||||||||||||||
|
Comment on lines
+25
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I put the test here, following the lead of narwhals/tests/frame/rows_test.py Lines 32 to 44 in b362e46
Not sure if we want anything more complex? |
||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See (#2064 (comment)) regarding false-positive (
PERF102)