Skip to content

Commit 2995737

Browse files
committed
DOC: update groupby/v0.14.0 docs for nth
1 parent 809d9d1 commit 2995737

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

doc/source/groupby.rst

+21-7
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ index are the group names and whose values are the sizes of each group.
400400
for example: ``mean, sum, size, count, std, var, describe, first, last, nth, min, max``. This is
401401
what happens when you do for example ``DataFrame.sum()`` and get back a ``Series``.
402402

403+
``nth`` can act as a reducer *or* a filter, see :ref:`here <groupby.nth>`
404+
403405
.. _groupby.aggregate.multifunc:
404406

405407
Applying multiple functions at once
@@ -855,19 +857,25 @@ This shows the first or last n rows from each group.
855857
Taking the nth row of each group
856858
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
857859

858-
To select from a DataFrame or Series the nth item, use the nth method:
860+
To select from a DataFrame or Series the nth item, use the nth method. This is a reduction method, and will return a single row (or no row) per group:
859861

860862
.. ipython:: python
861863
862-
DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B'])
864+
df = DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B'])
863865
g = df.groupby('A')
864-
g.nth(0)
865866
866-
g.nth(1)
867+
# nth(0) is the same as g.first()
868+
g.nth(0)
869+
g.first()
867870
871+
# nth(-1) is the same as g.last()
868872
g.nth(-1)
873+
g.last()
874+
875+
# return the nth item
876+
g.nth(1)
869877
870-
If you want to select the nth not-null method, use the dropna kwarg. For a DataFrame this should be either 'any' or 'all' just like you would pass to dropna, for a Series this just needs to be truthy.
878+
If you want to select the nth not-null method, use the ``dropna`` kwarg. For a DataFrame this should be either ``'any'`` or ``'all'`` just like you would pass to dropna, for a Series this just needs to be truthy.
871879

872880
.. ipython:: python
873881
@@ -877,9 +885,15 @@ If you want to select the nth not-null method, use the dropna kwarg. For a DataF
877885
878886
g.B.nth(0, dropna=True)
879887
880-
.. warning::
888+
As with other methods, passing ``as_index=False``, will achieve a filtration, which returns the grouped row.
889+
890+
.. ipython:: python
881891
882-
Before 0.14.0 this method existed but did not work correctly on DataFrames. The API has changed so that it filters by default, but the old behaviour (for Series) can be achieved by passing dropna. An alternative is to dropna before doing the groupby.
892+
df = DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B'])
893+
g = df.groupby('A',as_index=False)
894+
895+
g.nth(0)
896+
g.nth(-1)
883897
884898
Enumerate group items
885899
~~~~~~~~~~~~~~~~~~~~~

doc/source/v0.14.0.txt

+16-2
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,31 @@ More consistent behaviour for some groupby methods:
221221

222222
g[['B']].head(1)
223223

224-
- groupby ``nth`` now filters by default, with optional dropna argument to ignore
225-
NaN (to replicate the previous behaviour.), See :ref:`the docs <groupby.nth>`.
224+
- groupby ``nth`` now reduces by default; filtering can be achieved by passing ``as_index=False``. With an optional ``dropna`` argument to ignore
225+
NaN. See :ref:`the docs <groupby.nth>`.
226+
227+
Reducing
226228

227229
.. ipython:: python
228230

229231
df = DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B'])
230232
g = df.groupby('A')
231233
g.nth(0) # can also use negative ints
232234

235+
# this is equivalent to g.first()
233236
g.nth(0, dropna='any') # similar to old behaviour
234237

238+
# this is equivalent to g.last()
239+
g.nth(-1, dropna='any')
240+
241+
Filtering
242+
243+
.. ipython:: python
244+
245+
gf = df.groupby('A',as_index=False)
246+
gf.nth(0)
247+
gf.nth(0, dropna='any')
248+
235249
- groupby will now not return the grouped column for non-cython functions (:issue:`5610`, :issue:`5614`, :issue:`6732`),
236250
as its already the index
237251

0 commit comments

Comments
 (0)