Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[ ] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
keys = [1,1,1,2,2]
vals = [2,4,6,0,1]
df = pd.DataFrame({"keys":keys, "vals":vals})
df.groupby('keys').nth(n=3, dropna="all")
Issue Description
Doing a groupby nth with dropna when n is greater than the group size throws an error.
Expected Behavior
I think the expected behavior should be to just return an empty DataFrame.
Installed Versions
INSTALLED VERSIONS
------------------
commit : ca60aab7340d9989d9428e11a51467658190bb6b
python : 3.9.12.final.0
python-bits : 64
OS : Darwin
OS-release : 21.3.0
Version : Darwin Kernel Version 21.3.0: Wed Jan 5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_ARM64_T6000
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.4.4
numpy : 1.22.4
pytz : 2022.1
dateutil : 2.8.2
setuptools : 61.2.0
pip : 22.1.2
Cython : None
pytest : 7.2.0
hypothesis : None
sphinx : 5.0.1
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli :
fastparquet : 0.8.1
fsspec : 2022.7.1
gcsfs : 2022.7.1
markupsafe : 2.1.1
matplotlib : 3.5.2
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : 3.0.10
pandas_gbq : 0.17.6
pyarrow : 8.0.0
pyreadstat : None
pyxlsb : None
s3fs : 2022.7.1
scipy : 1.8.1
snappy : None
sqlalchemy : 1.4.37
tables : 3.7.0
tabulate : 0.8.10
xarray : 2022.3.0
xlrd : 2.0.1
xlwt : None
zstandard : None
Comment From: rhshadrach
Thanks for the report - on main I get:
Empty DataFrame
Columns: [keys, vals]
Index: []
This will be fixed in the release of 2.0.