Skip to content

Commit e139469

Browse files
authored
DOC: Clarify groupby.first does not use nulls (#46195)
1 parent 471319b commit e139469

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

pandas/core/groupby/groupby.py

+72-2
Original file line numberDiff line numberDiff line change
@@ -2235,8 +2235,43 @@ def max(
22352235
)
22362236

22372237
@final
2238-
@doc(_groupby_agg_method_template, fname="first", no=False, mc=-1)
2238+
@Substitution(name="groupby")
22392239
def first(self, numeric_only: bool = False, min_count: int = -1):
2240+
"""
2241+
Compute the first non-null entry of each column.
2242+
2243+
Parameters
2244+
----------
2245+
numeric_only : bool, default False
2246+
Include only float, int, boolean columns. If None, will attempt to use
2247+
everything, then use only numeric data.
2248+
min_count : int, default -1
2249+
The required number of valid values to perform the operation. If fewer
2250+
than ``min_count`` non-NA values are present the result will be NA.
2251+
2252+
Returns
2253+
-------
2254+
Series or DataFrame
2255+
First non-null of values within each group.
2256+
2257+
See Also
2258+
--------
2259+
DataFrame.groupby : Apply a function groupby to each row or column of a
2260+
DataFrame.
2261+
DataFrame.core.groupby.GroupBy.last : Compute the last non-null entry of each
2262+
column.
2263+
DataFrame.core.groupby.GroupBy.nth : Take the nth row from each group.
2264+
2265+
Examples
2266+
--------
2267+
>>> df = pd.DataFrame(dict(A=[1, 1, 3], B=[None, 5, 6], C=[1, 2, 3]))
2268+
>>> df.groupby("A").first()
2269+
B C
2270+
A
2271+
1 5.0 1
2272+
3 6.0 3
2273+
"""
2274+
22402275
def first_compat(obj: NDFrameT, axis: int = 0):
22412276
def first(x: Series):
22422277
"""Helper function for first item that isn't NA."""
@@ -2260,8 +2295,43 @@ def first(x: Series):
22602295
)
22612296

22622297
@final
2263-
@doc(_groupby_agg_method_template, fname="last", no=False, mc=-1)
2298+
@Substitution(name="groupby")
22642299
def last(self, numeric_only: bool = False, min_count: int = -1):
2300+
"""
2301+
Compute the last non-null entry of each column.
2302+
2303+
Parameters
2304+
----------
2305+
numeric_only : bool, default False
2306+
Include only float, int, boolean columns. If None, will attempt to use
2307+
everything, then use only numeric data.
2308+
min_count : int, default -1
2309+
The required number of valid values to perform the operation. If fewer
2310+
than ``min_count`` non-NA values are present the result will be NA.
2311+
2312+
Returns
2313+
-------
2314+
Series or DataFrame
2315+
Last non-null of values within each group.
2316+
2317+
See Also
2318+
--------
2319+
DataFrame.groupby : Apply a function groupby to each row or column of a
2320+
DataFrame.
2321+
DataFrame.core.groupby.GroupBy.first : Compute the first non-null entry of each
2322+
column.
2323+
DataFrame.core.groupby.GroupBy.nth : Take the nth row from each group.
2324+
2325+
Examples
2326+
--------
2327+
>>> df = pd.DataFrame(dict(A=[1, 1, 3], B=[5, None, 6], C=[1, 2, 3]))
2328+
>>> df.groupby("A").last()
2329+
B C
2330+
A
2331+
1 5.0 2
2332+
3 6.0 3
2333+
"""
2334+
22652335
def last_compat(obj: NDFrameT, axis: int = 0):
22662336
def last(x: Series):
22672337
"""Helper function for last item that isn't NA."""

0 commit comments

Comments
 (0)