Skip to content

Commit 8fe3dc6

Browse files
authored
BUG: Resample.aggregate raising TypeError instead of SpecificationError with missing keys dtypes (pandas-dev#39028)
1 parent 20e414a commit 8fe3dc6

File tree

5 files changed

+43
-13
lines changed

5 files changed

+43
-13
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ Groupby/resample/rolling
306306
- Bug in :meth:`.GroupBy.indices` would contain non-existent indices when null values were present in the groupby keys (:issue:`9304`)
307307
- Fixed bug in :meth:`DataFrameGroupBy.sum` and :meth:`SeriesGroupBy.sum` causing loss of precision through using Kahan summation (:issue:`38778`)
308308
- 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`)
309+
- Bug in :meth:`.Resampler.aggregate` and :meth:`DataFrame.transform` raising ``TypeError`` instead of ``SpecificationError`` when missing keys having mixed dtypes (:issue:`39025`)
309310

310311
Reshaping
311312
^^^^^^^^^

pandas/core/aggregation.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from pandas.core.dtypes.common import is_dict_like, is_list_like
3636
from pandas.core.dtypes.generic import ABCDataFrame, ABCNDFrame, ABCSeries
3737

38+
from pandas.core.algorithms import safe_sort
3839
from pandas.core.base import DataError, SpecificationError
3940
import pandas.core.common as com
4041
from pandas.core.indexes.api import Index
@@ -482,9 +483,10 @@ def transform_dict_like(
482483

483484
if obj.ndim != 1:
484485
# Check for missing columns on a frame
485-
cols = sorted(set(func.keys()) - set(obj.columns))
486+
cols = set(func.keys()) - set(obj.columns)
486487
if len(cols) > 0:
487-
raise SpecificationError(f"Column(s) {cols} do not exist")
488+
cols_sorted = list(safe_sort(list(cols)))
489+
raise SpecificationError(f"Column(s) {cols_sorted} do not exist")
488490

489491
# Can't use func.values(); wouldn't work for a Series
490492
if any(is_dict_like(v) for _, v in func.items()):
@@ -738,7 +740,11 @@ def agg_dict_like(
738740
if isinstance(selected_obj, ABCDataFrame) and len(
739741
selected_obj.columns.intersection(keys)
740742
) != len(keys):
741-
cols = sorted(set(keys) - set(selected_obj.columns.intersection(keys)))
743+
cols = list(
744+
safe_sort(
745+
list(set(keys) - set(selected_obj.columns.intersection(keys))),
746+
)
747+
)
742748
raise SpecificationError(f"Column(s) {cols} do not exist")
743749

744750
from pandas.core.reshape.concat import concat

pandas/tests/frame/apply/test_frame_transform.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,24 @@ def f(x, a, b, c):
253253

254254

255255
def test_transform_missing_columns(axis):
256-
# GH 35964
256+
# GH#35964
257257
df = DataFrame({"A": [1, 2], "B": [3, 4]})
258258
match = re.escape("Column(s) ['C'] do not exist")
259259
with pytest.raises(SpecificationError, match=match):
260260
df.transform({"C": "cumsum"})
261+
262+
263+
def test_transform_none_to_type():
264+
# GH#34377
265+
df = DataFrame({"a": [None]})
266+
msg = "Transform function failed"
267+
with pytest.raises(ValueError, match=msg):
268+
df.transform({"a": int})
269+
270+
271+
def test_transform_mixed_column_name_dtypes():
272+
# GH39025
273+
df = DataFrame({"a": ["1"]})
274+
msg = r"Column\(s\) \[1, 'b'\] do not exist"
275+
with pytest.raises(SpecificationError, match=msg):
276+
df.transform({"a": int, 1: str, "b": int})

pandas/tests/resample/test_resample_api.py

+15
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,21 @@ def test_agg_consistency():
297297
r.agg({"r1": "mean", "r2": "sum"})
298298

299299

300+
def test_agg_consistency_int_str_column_mix():
301+
# GH#39025
302+
df = DataFrame(
303+
np.random.randn(1000, 2),
304+
index=pd.date_range("1/1/2012", freq="S", periods=1000),
305+
columns=[1, "a"],
306+
)
307+
308+
r = df.resample("3T")
309+
310+
msg = r"Column\(s\) \[2, 'b'\] do not exist"
311+
with pytest.raises(pd.core.base.SpecificationError, match=msg):
312+
r.agg({2: "mean", "b": "sum"})
313+
314+
300315
# TODO: once GH 14008 is fixed, move these tests into
301316
# `Base` test class
302317

pandas/tests/series/apply/test_series_transform.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from pandas import DataFrame, Series, concat
4+
from pandas import Series, concat
55
import pandas._testing as tm
66
from pandas.core.base import SpecificationError
77
from pandas.core.groupby.base import transformation_kernels
@@ -65,14 +65,6 @@ def test_transform_wont_agg(string_series):
6565
string_series.transform(["sqrt", "max"])
6666

6767

68-
def test_transform_none_to_type():
69-
# GH34377
70-
df = DataFrame({"a": [None]})
71-
msg = "Transform function failed"
72-
with pytest.raises(ValueError, match=msg):
73-
df.transform({"a": int})
74-
75-
7668
def test_transform_axis_1_raises():
7769
# GH 35964
7870
msg = "No axis named 1 for object type Series"

0 commit comments

Comments
 (0)