@@ -3231,63 +3231,80 @@ def max(
32313231 def first (
32323232 self , numeric_only : bool = False , min_count : int = - 1 , skipna : bool = True
32333233 ) -> NDFrameT :
3234- """
3235- Compute the first entry of each column within each group.
3234+ """
3235+ Compute the first non-null entry of each column within each group.
32363236
3237- Defaults to skipping NA elements.
3237+ This method operates column-wise, returning the first non-null value
3238+ in each column for every group. Unlike `nth(0)`, which returns the
3239+ first row (even if it contains nulls), `first()` skips over NA/null
3240+ values in each column independently.
32383241
3239- Parameters
3240- ----------
3241- numeric_only : bool, default False
3242- Include only float, int, boolean columns.
3243- min_count : int, default -1
3244- The required number of valid values to perform the operation. If fewer
3245- than ``min_count`` valid values are present the result will be NA.
3246- skipna : bool, default True
3247- Exclude NA/null values. If an entire group is NA, the result will be NA.
3242+ Parameters
3243+ ----------
3244+ numeric_only : bool, default False
3245+ Include only float, int, boolean columns.
3246+ min_count : int, default -1
3247+ The required number of valid values to perform the operation. If fewer
3248+ than ``min_count`` valid values are present the result will be NA.
3249+ skipna : bool, default True
3250+ Exclude NA/null values. If an entire group is NA, the result will be NA.
32483251
3249- .. versionadded:: 2.2.1
3252+ .. versionadded:: 2.2.1
32503253
3251- Returns
3252- -------
3253- Series or DataFrame
3254- First values within each group.
3254+ Returns
3255+ -------
3256+ Series or DataFrame
3257+ First non-null values within each group, selected independently per column .
32553258
3256- See Also
3257- --------
3258- DataFrame.groupby : Apply a function groupby to each row or column of a
3259- DataFrame .
3260- core.groupby.DataFrameGroupBy.last : Compute the last non-null entry
3261- of each column .
3262- core.groupby.DataFrameGroupBy.nth : Take the nth row from each group .
3259+ See Also
3260+ --------
3261+ DataFrame.groupby : Group DataFrame using a mapper or by a Series of columns.
3262+ Series.groupby : Group Series using a mapper or by a Series of values .
3263+ GroupBy.nth : Take the nth row from each group.
3264+ GroupBy.head : Return the first `n` rows from each group .
3265+ GroupBy.last : Compute the last non-null entry of each column .
32633266
3264- Examples
3265- --------
3266- >>> df = pd.DataFrame(
3267- ... dict(
3268- ... A=[1, 1, 3],
3269- ... B=[None, 5, 6],
3270- ... C=[1, 2, 3],
3271- ... D=["3/11/2000", "3/12/2000", "3/13/2000"],
3272- ... )
3273- ... )
3274- >>> df["D"] = pd.to_datetime(df["D"])
3275- >>> df.groupby("A").first()
3276- B C D
3277- A
3278- 1 5.0 1 2000-03-11
3279- 3 6.0 3 2000-03-13
3280- >>> df.groupby("A").first(min_count=2)
3281- B C D
3282- A
3283- 1 NaN 1.0 2000-03-11
3284- 3 NaN NaN NaT
3285- >>> df.groupby("A").first(numeric_only=True)
3286- B C
3287- A
3288- 1 5.0 1
3289- 3 6.0 3
3290- """
3267+ Examples
3268+ --------
3269+ >>> df = pd.DataFrame(
3270+ ... dict(
3271+ ... A=[1, 1, 3],
3272+ ... B=[None, 5, 6],
3273+ ... C=[1, 2, 3],
3274+ ... D=["3/11/2000", "3/12/2000", "3/13/2000"],
3275+ ... )
3276+ ... )
3277+ >>> df["D"] = pd.to_datetime(df["D"])
3278+
3279+ >>> df.groupby("A").first()
3280+ B C D
3281+ A
3282+ 1 5.0 1 2000-03-11
3283+ 3 6.0 3 2000-03-13
3284+
3285+ >>> df.groupby("A").nth(0)
3286+ B C D
3287+ A
3288+ 1 NaN 1 2000-03-11
3289+ 3 6.0 3 2000-03-13
3290+
3291+ >>> df.groupby("A").head(1)
3292+ A B C D
3293+ 0 1 NaN 1 2000-03-11
3294+ 2 3 6.0 3 2000-03-13
3295+
3296+ >>> df.groupby("A").first(min_count=2)
3297+ B C D
3298+ A
3299+ 1 NaN 1.0 2000-03-11
3300+ 3 NaN NaN NaT
3301+
3302+ >>> df.groupby("A").first(numeric_only=True)
3303+ B C
3304+ A
3305+ 1 5.0 1
3306+ 3 6.0 3
3307+ """
32913308
32923309 def first_compat (obj : NDFrameT ):
32933310 def first (x : Series ):
0 commit comments