Open tbenthompson opened 1 year ago
This seems to be because https://github.com/pandas-dev/pandas/blob/dba96f97abc96712946067efb63a587e47786caf/pandas/core/groupby/groupby.py#L1437 is catching all TypeError exceptions.
Thanks for the report! In the code you highlighted, apply is first trying to operation with the groupings, and then when this fails trying it without them. In my mind this is undesirable behavior but it will need to go through deprecation. I don't recall which issues / PRs this was in, but in the past there was also concern that users might find including the groupings useful (most of groupby does not and I think we should be consistent). So there might not be a clear way forward here. Tangentially related: #49543
In the meantime, I agree the traceback is undesirable. Perhaps it would be better to do something like
raised_typeerror = False
try:
...
except TypeError:
raised_typeerror = True
if raised_typerror:
...
I think this could be done without any deprecation. Further suggestions and PRs to fix are welcome!
Results in three exceptions stacked on top of each other. See below. This is particularly unpleasant for less sophisticated users because the bottom-most error is
KeyError: 'A'
which is very confusing. The actually relevant exception is the first of the three.