Skip to content

Commit

Permalink
DOC Fixing EX01 - added examples (pandas-dev#53181)
Browse files Browse the repository at this point in the history
* DOC Fixing EX01 - added examples

* Removed Index.item from code_checks

* Removed #GH13

* Added case for return value=False

* Added False case for is_unique

* Correct docstring

* really minor clarification (there may be multiple groups, so "the group" isnt defined)

---------

Co-authored-by: MarcoGorelli <33491632+MarcoGorelli@users.noreply.github.com>
  • Loading branch information
DeaMariaLeon and MarcoGorelli committed May 15, 2023
1 parent 935244a commit f676c5f
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 13 deletions.
9 changes: 0 additions & 9 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then

MSG='Partially validate docstrings (EX01)' ; echo $MSG
$BASE_DIR/scripts/validate_docstrings.py --format=actions --errors=EX01 --ignore_functions \
pandas.Series.item \
pandas.Series.pipe \
pandas.Series.mode \
pandas.Series.is_unique \
pandas.Series.is_monotonic_increasing \
pandas.Series.is_monotonic_decreasing \
pandas.Series.backfill \
pandas.Series.bfill \
pandas.Series.ffill \
Expand Down Expand Up @@ -319,7 +313,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.Index.fillna \
pandas.Index.dropna \
pandas.Index.astype \
pandas.Index.item \
pandas.Index.map \
pandas.Index.ravel \
pandas.Index.to_list \
Expand Down Expand Up @@ -462,8 +455,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.core.groupby.SeriesGroupBy.cumsum \
pandas.core.groupby.SeriesGroupBy.diff \
pandas.core.groupby.SeriesGroupBy.ffill \
pandas.core.groupby.SeriesGroupBy.is_monotonic_increasing \
pandas.core.groupby.SeriesGroupBy.is_monotonic_decreasing \
pandas.core.groupby.SeriesGroupBy.max \
pandas.core.groupby.SeriesGroupBy.median \
pandas.core.groupby.SeriesGroupBy.min \
Expand Down
46 changes: 44 additions & 2 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,24 @@ def item(self):
Returns
-------
scalar
The first element of Series.
The first element of Series or Index.
Raises
------
ValueError
If the data is not length-1.
If the data is not length = 1.
Examples
--------
>>> s = pd.Series([1])
>>> s.item()
1
For an index:
>>> s = pd.Series([1], index=['a'])
>>> s.index.item()
'a'
"""
if len(self) == 1:
return next(iter(self))
Expand Down Expand Up @@ -965,6 +977,16 @@ def is_unique(self) -> bool:
Returns
-------
bool
Examples
--------
>>> s = pd.Series([1, 2, 3])
>>> s.is_unique
True
>>> s = pd.Series([1, 2, 3, 1])
>>> s.is_unique
False
"""
return self.nunique(dropna=False) == len(self)

Expand All @@ -976,6 +998,16 @@ def is_monotonic_increasing(self) -> bool:
Returns
-------
bool
Examples
--------
>>> s = pd.Series([1, 2, 2])
>>> s.is_monotonic_increasing
True
>>> s = pd.Series([3, 2, 1])
>>> s.is_monotonic_increasing
False
"""
from pandas import Index

Expand All @@ -989,6 +1021,16 @@ def is_monotonic_decreasing(self) -> bool:
Returns
-------
bool
Examples
--------
>>> s = pd.Series([3, 2, 2, 1])
>>> s.is_monotonic_decreasing
True
>>> s = pd.Series([1, 2, 3])
>>> s.is_monotonic_decreasing
False
"""
from pandas import Index

Expand Down
32 changes: 30 additions & 2 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,13 +1170,41 @@ def cov(
return result

@property
@doc(Series.is_monotonic_increasing.__doc__)
def is_monotonic_increasing(self) -> Series:
"""
Return whether each group's values are monotonically increasing.
Returns
-------
Series
Examples
--------
>>> s = pd.Series([2, 1, 3, 4], index=['Falcon', 'Falcon', 'Parrot', 'Parrot'])
>>> s.groupby(level=0).is_monotonic_increasing
Falcon False
Parrot True
dtype: bool
"""
return self.apply(lambda ser: ser.is_monotonic_increasing)

@property
@doc(Series.is_monotonic_decreasing.__doc__)
def is_monotonic_decreasing(self) -> Series:
"""
Return whether each group's values are monotonically decreasing.
Returns
-------
Series
Examples
--------
>>> s = pd.Series([2, 1, 3, 4], index=['Falcon', 'Falcon', 'Parrot', 'Parrot'])
>>> s.groupby(level=0).is_monotonic_decreasing
Falcon True
Parrot False
dtype: bool
"""
return self.apply(lambda ser: ser.is_monotonic_decreasing)

@doc(Series.hist.__doc__)
Expand Down
26 changes: 26 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,32 @@ def mode(self, dropna: bool = True) -> Series:
-------
Series
Modes of the Series in sorted order.
Examples
--------
>>> s = pd.Series([2, 4, 2, 2, 4, None])
>>> s.mode()
0 2.0
dtype: float64
More than one mode:
>>> s = pd.Series([2, 4, 8, 2, 4, None])
>>> s.mode()
0 2.0
1 4.0
dtype: float64
With and without considering null value:
>>> s = pd.Series([2, 4, None, None, 4, None])
>>> s.mode(dropna=False)
0 NaN
dtype: float64
>>> s = pd.Series([2, 4, None, None, 4, None])
>>> s.mode()
0 4.0
dtype: float64
"""
# TODO: Add option for bins like value_counts()
values = self._values
Expand Down

0 comments on commit f676c5f

Please sign in to comment.