@@ -3231,80 +3231,81 @@ def max(
3231
3231
def first (
3232
3232
self , numeric_only : bool = False , min_count : int = - 1 , skipna : bool = True
3233
3233
) -> NDFrameT :
3234
- """
3235
- Compute the first non-null entry of each column within each group.
3234
+ """
3235
+ Compute the first non-null entry of each column within each group.
3236
3236
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.
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.
3241
3241
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.
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.
3251
3251
3252
- .. versionadded:: 2.2.1
3252
+ .. versionadded:: 2.2.1
3253
3253
3254
- Returns
3255
- -------
3256
- Series or DataFrame
3257
- First non-null values within each group, selected independently per column.
3254
+ Returns
3255
+ -------
3256
+ Series or DataFrame
3257
+ First non-null values within each group, selected independently per column.
3258
3258
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.
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.
3266
+
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
+ """
3266
3308
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
- """
3308
3309
3309
3310
def first_compat (obj : NDFrameT ):
3310
3311
def first (x : Series ):
0 commit comments