-
Notifications
You must be signed in to change notification settings - Fork 119
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: descriptive #159
Merged
Merged
feat: descriptive #159
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1357,6 +1357,194 @@ def join( | |
""" | ||
return super().join(other, how=how, left_on=left_on, right_on=right_on) | ||
|
||
# --- descriptive --- | ||
def is_duplicated(self: Self) -> Series: | ||
r""" | ||
Get a mask of all duplicated rows in this DataFrame. | ||
|
||
Examples: | ||
>>> import narwhals as nw | ||
>>> import pandas as pd | ||
>>> import polars as pl | ||
>>> df_pd = pd.DataFrame( | ||
... { | ||
... "a": [1, 2, 3, 1], | ||
... "b": ["x", "y", "z", "x"], | ||
... } | ||
... ) | ||
>>> df_pl = pl.DataFrame( | ||
... { | ||
... "a": [1, 2, 3, 1], | ||
... "b": ["x", "y", "z", "x"], | ||
... } | ||
... ) | ||
|
||
Let's define a dataframe-agnostic function: | ||
|
||
>>> def func(df_any): | ||
... df = nw.from_native(df_any) | ||
... duplicated = df.is_duplicated() | ||
... return nw.to_native(duplicated) | ||
|
||
We can then pass either pandas or Polars to `func`: | ||
|
||
>>> func(df_pd) # doctest: +NORMALIZE_WHITESPACE | ||
0 True | ||
1 False | ||
2 False | ||
3 True | ||
dtype: bool | ||
|
||
>>> func(df_pl) # doctest: +NORMALIZE_WHITESPACE | ||
shape: (4,) | ||
Series: '' [bool] | ||
[ | ||
true | ||
false | ||
false | ||
true | ||
] | ||
""" | ||
from narwhals.series import Series | ||
|
||
return Series(self._dataframe.is_duplicated()) | ||
|
||
def is_empty(self: Self) -> bool: | ||
r""" | ||
Check if the dataframe is empty. | ||
|
||
Examples: | ||
>>> import narwhals as nw | ||
>>> import pandas as pd | ||
>>> import polars as pl | ||
|
||
Let's define a dataframe-agnostic function that filters rows in which "foo" | ||
values are greater than 10, and then checks if the result is empty or not: | ||
|
||
>>> def func(df_any): | ||
... df = nw.from_native(df_any) | ||
... return df.filter(nw.col("foo")>10).is_empty() | ||
|
||
We can then pass either pandas or Polars to `func`: | ||
|
||
>>> df_pd = pd.DataFrame({"foo": [1, 2, 3], "bar": [4, 5, 6]}) | ||
>>> df_pl = pl.DataFrame({"foo": [1, 2, 3], "bar": [4, 5, 6]}) | ||
>>> func(df_pd), func(df_pl) | ||
(True, True) | ||
|
||
>>> df_pd = pd.DataFrame({"foo": [100, 2, 3], "bar": [4, 5, 6]}) | ||
>>> df_pl = pl.DataFrame({"foo": [100, 2, 3], "bar": [4, 5, 6]}) | ||
>>> func(df_pd), func(df_pl) | ||
(False, False) | ||
""" | ||
|
||
return self._dataframe.is_empty() # type: ignore[no-any-return] | ||
|
||
def is_unique(self: Self) -> Series: | ||
r""" | ||
Get a mask of all unique rows in this DataFrame. | ||
|
||
Examples: | ||
>>> import narwhals as nw | ||
>>> import pandas as pd | ||
>>> import polars as pl | ||
>>> df_pd = pd.DataFrame( | ||
... { | ||
... "a": [1, 2, 3, 1], | ||
... "b": ["x", "y", "z", "x"], | ||
... } | ||
... ) | ||
>>> df_pl = pl.DataFrame( | ||
... { | ||
... "a": [1, 2, 3, 1], | ||
... "b": ["x", "y", "z", "x"], | ||
... } | ||
... ) | ||
|
||
Let's define a dataframe-agnostic function: | ||
|
||
>>> def func(df_any): | ||
... df = nw.from_native(df_any) | ||
... unique = df.is_unique() | ||
... return nw.to_native(unique) | ||
|
||
We can then pass either pandas or Polars to `func`: | ||
|
||
>>> func(df_pd) # doctest: +NORMALIZE_WHITESPACE | ||
0 False | ||
1 True | ||
2 True | ||
3 False | ||
dtype: bool | ||
|
||
>>> func(df_pl) # doctest: +NORMALIZE_WHITESPACE | ||
shape: (4,) | ||
Series: '' [bool] | ||
[ | ||
false | ||
true | ||
true | ||
false | ||
] | ||
""" | ||
from narwhals.series import Series | ||
|
||
return Series(self._dataframe.is_unique()) | ||
|
||
def null_count(self: Self) -> DataFrame: | ||
r""" | ||
Create a new DataFrame that shows the null counts per column. | ||
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. could we add a note here on how pandas and Polars treat null values differently? some other docstrings have it 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. |
||
|
||
Notes: | ||
pandas and Polars handle null values differently. Polars distinguishes | ||
between NaN and Null, whereas pandas doesn't. | ||
|
||
Examples: | ||
>>> import narwhals as nw | ||
>>> import pandas as pd | ||
>>> import polars as pl | ||
>>> df_pd = pd.DataFrame( | ||
... { | ||
... "foo": [1, None, 3], | ||
... "bar": [6, 7, None], | ||
... "ham": ["a", "b", "c"], | ||
... } | ||
... ) | ||
>>> df_pl = pl.DataFrame( | ||
... { | ||
... "foo": [1, None, 3], | ||
... "bar": [6, 7, None], | ||
... "ham": ["a", "b", "c"], | ||
... } | ||
... ) | ||
|
||
Let's define a dataframe-agnostic function that returns the null count of | ||
each columns: | ||
|
||
>>> def func(df_any): | ||
... df = nw.from_native(df_any) | ||
... null_counts = df.null_count() | ||
... return nw.to_native(null_counts) | ||
|
||
We can then pass either pandas or Polars to `func`: | ||
|
||
>>> func(df_pd) | ||
foo bar ham | ||
0 1 1 0 | ||
|
||
>>> func(df_pl) | ||
shape: (1, 3) | ||
┌─────┬─────┬─────┐ | ||
│ foo ┆ bar ┆ ham │ | ||
│ --- ┆ --- ┆ --- │ | ||
│ u32 ┆ u32 ┆ u32 │ | ||
╞═════╪═════╪═════╡ | ||
│ 1 ┆ 1 ┆ 0 │ | ||
└─────┴─────┴─────┘ | ||
""" | ||
|
||
return DataFrame(self._dataframe.null_count()) | ||
|
||
|
||
class LazyFrame(BaseFrame): | ||
r""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
All this to return a dataframe (instead of a series) as polars does.
I double checked modin and cudf docs, both support
to_frame
andtranspose
so it should be fine.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.
the fact that pandas reduces these operations to Series kind of annoys me
looks good, thanks