itholic opened a new pull request, #42994:
URL: https://github.com/apache/spark/pull/42994

   ### What changes were proposed in this pull request?
   
   This PR proposes to match `GroupBy.nth` behavior to the latest Pandas.
   
   ### Why are the changes needed?
   
   To match the behavior of Pandas 2.0.0 and above.
   
   ### Does this PR introduce _any_ user-facing change?
   **Test DataFrame**
   ```python
   >>> psdf = ps.DataFrame(
   ...     {
   ...         "A": [1, 2, 1, 2],
   ...         "B": [3.1, 4.1, 4.1, 3.1],
   ...         "C": ["a", "b", "b", "a"],
   ...         "D": [True, False, False, True],
   ...     }
   ... )
   >>> psdf
      A    B  C      D
   0  1  3.1  a   True
   1  2  4.1  b  False
   2  1  4.1  b  False
   3  2  3.1  a   True
   ```
   **Before fixing**
   ```python
   >>> psdf.groupby("A").nth(-1)
        B  C      D
   A
   1  4.1  b  False
   2  3.1  a   True
   >>> psdf.groupby("A")[["C"]].nth(-1)
      C
   A
   1  b
   2  a
   >>> psdf.groupby("A")["B"].nth(-1)
   A
   1    4.1
   2    3.1
   Name: B, dtype: float64
   ```
   **After fixing**
   ```python
   >>> psdf.groupby("A").nth(-1)
      A    B  C      D
   2  1  4.1  b  False
   3  2  3.1  a   True
   >>> psdf.groupby("A")[["C"]].nth(-1)
      C
   2  b
   3  a
   >>> psdf.groupby("A")["B"].nth(-1)
   2    4.1
   3    3.1
   Name: B, dtype: float64
   ```
   
   ### How was this patch tested?
   
   Enabling the existing tests & updating the doctests.
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   No.
   


-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to