-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: reset_index after a group_by raise a ValueError for empty dataframe #43767
Comments
Thanks for the report! Similar to #44350 since resample is using apply, however we should be able to predict the result shape and correct it in |
I don't think this is a bug but the normal/expected behavior.
When we apply
What we notice here is that
The result of calling the function will be
Thus, the The function apply will assume that all of the columns can be numeric when calling the We can edit the method |
Ok, so you can close the issue then. It’s just that users must be aware of handling empty dataframe before trying to do this kind of call 👍 |
Thanks for the assessment here @ahmedibrhm! In addition, as part of #46560, the silent dropping of column "b" when it is non-empty will raise in 2.0. So in pandas 2.0, both the empty and non-empty versions of the OP example will raise. This is an improvement because it removes the value-dependent behavior. |
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the master branch of pandas.
Reproducible Example
Issue Description
When you run the code above, df won’t be empty and the code will run correctly. But if you uncomment the line that makes the dataframe empty, the reset index will raise a ValueError(f"cannot insert b, already exists").
This is a regression as it was working in pandas 1.2.x
Expected Behavior
It’s expected for the reset_index to apply correctly on an empty dataframe too.
Installed Versions
INSTALLED VERSIONS
commit : 73c6825
python : 3.9.7.final.0
python-bits : 64
OS : Darwin
OS-release : 20.6.0
Version : Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8
pandas : 1.3.3
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 21.1.2
setuptools : 57.0.0
Cython : None
pytest : 6.2.5
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.1 (dt dec pq3 ext lo64)
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : 1.4.25
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: