Skip to content

BUG: Resample.aggregate raising TypeError instead of SpecificationError with missing keys dtypes #39028

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

Merged
merged 11 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ Groupby/resample/rolling
- Bug in :meth:`.GroupBy.indices` would contain non-existent indices when null values were present in the groupby keys (:issue:`9304`)
- Fixed bug in :meth:`DataFrameGroupBy.sum` and :meth:`SeriesGroupBy.sum` causing loss of precision through using Kahan summation (:issue:`38778`)
- Fixed bug in :meth:`DataFrameGroupBy.cumsum`, :meth:`SeriesGroupBy.cumsum`, :meth:`DataFrameGroupBy.mean` and :meth:`SeriesGroupBy.mean` causing loss of precision through using Kahan summation (:issue:`38934`)
- Bug in :meth:`.Resampler.aggregate` raising ``TypeError`` instead of ``SpecificationError`` when missing keys having mixed dtypes (:issue:`39025`)

Reshaping
^^^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,10 @@ def agg_dict_like(
if isinstance(selected_obj, ABCDataFrame) and len(
selected_obj.columns.intersection(keys)
) != len(keys):
cols = sorted(set(keys) - set(selected_obj.columns.intersection(keys)))
cols = sorted(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you just use safe_sort?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, of course. Thx. Forgot about safe sort...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm had to add list calls

set(keys) - set(selected_obj.columns.intersection(keys)),
key=lambda col: (isinstance(col, str), col),
)
raise SpecificationError(f"Column(s) {cols} do not exist")

from pandas.core.reshape.concat import concat
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/resample/test_resample_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,21 @@ def test_agg_consistency():
r.agg({"r1": "mean", "r2": "sum"})


def test_agg_consistency_int_str_column_mix():
# GH#39025
df = DataFrame(
np.random.randn(1000, 2),
index=pd.date_range("1/1/2012", freq="S", periods=1000),
columns=[1, "a"],
)

r = df.resample("3T")

msg = r"Column\(s\) \[2, 'b'\] do not exist"
with pytest.raises(pd.core.base.SpecificationError, match=msg):
r.agg({2: "mean", "b": "sum"})


# TODO: once GH 14008 is fixed, move these tests into
# `Base` test class

Expand Down