-
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 1 commit
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,155 @@ 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 polars as pl | ||
>>> import pandas as pd | ||
|
||
>>> df_pl = pl.DataFrame( | ||
... { | ||
... "a": [1, 2, 3, 1], | ||
... "b": ["x", "y", "z", "x"], | ||
... } | ||
... ) | ||
>>> nw.to_native(nw.from_native(df_pl).is_duplicated()) # doctest: +NORMALIZE_WHITESPACE | ||
shape: (4,) | ||
Series: '' [bool] | ||
[ | ||
true | ||
false | ||
false | ||
true | ||
] | ||
|
||
>>> df_pd = pd.DataFrame( | ||
... { | ||
... "a": [1, 2, 3, 1], | ||
... "b": ["x", "y", "z", "x"], | ||
... } | ||
... ) | ||
>>> nw.to_native(nw.from_native(df_pd).is_duplicated()) | ||
0 True | ||
1 False | ||
2 False | ||
3 True | ||
dtype: bool | ||
""" | ||
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 polars as pl | ||
>>> import pandas as pd | ||
|
||
>>> df_pl = nw.from_native(pl.DataFrame({"foo": [1, 2, 3], "bar": [4, 5, 6]})) | ||
>>> df_pl.is_empty() | ||
False | ||
>>> df_pl.filter(nw.col("foo") > 99).is_empty() | ||
True | ||
|
||
>>> df_pd = nw.from_native(pd.DataFrame({"foo": [1, 2, 3], "bar": [4, 5, 6]})) | ||
>>> df_pd.is_empty() | ||
False | ||
>>> df_pd.filter(nw.col("foo") > 99).is_empty() | ||
True | ||
""" | ||
|
||
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 polars as pl | ||
>>> import pandas as pd | ||
|
||
>>> df_pl = pl.DataFrame( | ||
... { | ||
... "a": [1, 2, 3, 1], | ||
... "b": ["x", "y", "z", "x"], | ||
... } | ||
... ) | ||
>>> nw.to_native(nw.from_native(df_pl).is_unique()) # doctest: +NORMALIZE_WHITESPACE | ||
shape: (4,) | ||
Series: '' [bool] | ||
[ | ||
false | ||
true | ||
true | ||
false | ||
] | ||
|
||
>>> df_pd = pd.DataFrame( | ||
... { | ||
... "a": [1, 2, 3, 1], | ||
... "b": ["x", "y", "z", "x"], | ||
... } | ||
... ) | ||
>>> nw.to_native(nw.from_native(df_pd).is_unique()) | ||
0 False | ||
1 True | ||
2 True | ||
3 False | ||
dtype: bool | ||
""" | ||
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. |
||
|
||
Examples: | ||
>>> import narwhals as nw | ||
>>> import polars as pl | ||
>>> import pandas as pd | ||
|
||
>>> df_pl = pl.DataFrame( | ||
... { | ||
... "foo": [1, None, 3], | ||
... "bar": [6, 7, None], | ||
... "ham": ["a", "b", "c"], | ||
... } | ||
... ) | ||
>>> nw.to_native(nw.from_native(df_pl).null_count()) | ||
shape: (1, 3) | ||
┌─────┬─────┬─────┐ | ||
│ foo ┆ bar ┆ ham │ | ||
│ --- ┆ --- ┆ --- │ | ||
│ u32 ┆ u32 ┆ u32 │ | ||
╞═════╪═════╪═════╡ | ||
│ 1 ┆ 1 ┆ 0 │ | ||
└─────┴─────┴─────┘ | ||
|
||
>>> df_pd = pd.DataFrame( | ||
... { | ||
... "foo": [1, None, 3], | ||
... "bar": [6, 7, None], | ||
... "ham": ["a", "b", "c"], | ||
... } | ||
... ) | ||
>>> nw.to_native(nw.from_native(df_pd).null_count()) | ||
foo bar ham | ||
0 1 1 0 | ||
""" | ||
|
||
return DataFrame(self._dataframe.null_count()) | ||
|
||
|
||
class LazyFrame(BaseFrame): | ||
r""" | ||
|
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