itholic commented on code in PR #37801:
URL: https://github.com/apache/spark/pull/37801#discussion_r963416033
##########
python/pyspark/pandas/groupby.py:
##########
@@ -895,6 +895,95 @@ def sem(col: Column) -> Column:
bool_to_numeric=True,
)
+ # TODO: 1, 'n' accepts list and slice; 2, implement 'dropna' parameter
+ def nth(self, n: int) -> FrameLike:
+ """
+ Take the nth row from each group.
+
+ .. versionadded:: 3.4.0
+
+ Parameters
+ ----------
+ n : int
+ A single nth value for the row
+
+ Returns
+ -------
+ Series or DataFrame
+
+ Examples
+ --------
+ >>> df = ps.DataFrame({'A': [1, 1, 2, 1, 2],
+ ... 'B': [np.nan, 2, 3, 4, 5]}, columns=['A', 'B'])
+ >>> g = df.groupby('A')
+ >>> g.nth(0)
+ B
+ A
+ 1 NaN
+ 2 3.0
+ >>> g.nth(1)
+ B
+ A
+ 1 2.0
+ 2 5.0
+ >>> g.nth(-1)
+ B
+ A
+ 1 4.0
+ 2 5.0
+
+ See Also
+ --------
+ pyspark.pandas.Series.groupby
+ pyspark.pandas.DataFrame.groupby
+ """
+ if not isinstance(n, int):
+ raise TypeError("Unsupported type %s" % type(n).__name__)
Review Comment:
Got it.
Btw, seems like the latest pandas (1.4.3) raises `TypeError` as below:
```python
>>> g.nth("C")
Traceback (most recent call last):
...
TypeError: Invalid index <class 'str'>. Must be integer, list-like, slice or
a tuple of integers and slices
```
Can we follow the `TypeError` and its message from pandas, for more
information to users ?
##########
python/pyspark/pandas/groupby.py:
##########
@@ -895,6 +895,95 @@ def sem(col: Column) -> Column:
bool_to_numeric=True,
)
+ # TODO: 1, 'n' accepts list and slice; 2, implement 'dropna' parameter
+ def nth(self, n: int) -> FrameLike:
+ """
+ Take the nth row from each group.
+
+ .. versionadded:: 3.4.0
+
+ Parameters
+ ----------
+ n : int
+ A single nth value for the row
+
+ Returns
+ -------
+ Series or DataFrame
+
+ Examples
+ --------
+ >>> df = ps.DataFrame({'A': [1, 1, 2, 1, 2],
+ ... 'B': [np.nan, 2, 3, 4, 5]}, columns=['A', 'B'])
+ >>> g = df.groupby('A')
+ >>> g.nth(0)
+ B
+ A
+ 1 NaN
+ 2 3.0
+ >>> g.nth(1)
+ B
+ A
+ 1 2.0
+ 2 5.0
+ >>> g.nth(-1)
+ B
+ A
+ 1 4.0
+ 2 5.0
+
+ See Also
+ --------
+ pyspark.pandas.Series.groupby
+ pyspark.pandas.DataFrame.groupby
+ """
+ if not isinstance(n, int):
+ raise TypeError("Unsupported type %s" % type(n).__name__)
Review Comment:
Got it.
Btw, seems like the latest pandas (1.4.4) raises `TypeError` as below:
```python
>>> g.nth("C")
Traceback (most recent call last):
...
TypeError: Invalid index <class 'str'>. Must be integer, list-like, slice or
a tuple of integers and slices
```
Can we follow the `TypeError` and its message from pandas, for more
information to users ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]