Skip to content

Commit c03d9ba

Browse files
committed
Merge pull request #6338 from jreback/agg
BUG: Issue with groupby agg with a single function and a a mixed-type frame (GH6337)
2 parents dae25de + f6ba374 commit c03d9ba

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Bug Fixes
9191
- ``HDFStore.select_as_multiple`` handles start and stop the same way as ``select`` (:issue:`6177`)
9292
- ``HDFStore.select_as_coordinates`` and ``select_column`` works where clauses that result in filters (:issue:`6177`)
9393
- Regression in join of non_unique_indexes (:issue:`6329`)
94+
- Issue with groupby ``agg`` with a single function and a a mixed-type frame (:issue:`6337`)
9495

9596
pandas 0.13.1
9697
-------------

pandas/core/groupby.py

+9
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,7 @@ def _aggregate_item_by_item(self, func, *args, **kwargs):
21232123
obj = self._obj_with_exclusions
21242124
result = {}
21252125
cannot_agg = []
2126+
errors=None
21262127
for item in obj:
21272128
try:
21282129
data = obj[item]
@@ -2133,11 +2134,19 @@ def _aggregate_item_by_item(self, func, *args, **kwargs):
21332134
except ValueError:
21342135
cannot_agg.append(item)
21352136
continue
2137+
except TypeError as e:
2138+
cannot_agg.append(item)
2139+
errors=e
2140+
continue
21362141

21372142
result_columns = obj.columns
21382143
if cannot_agg:
21392144
result_columns = result_columns.drop(cannot_agg)
21402145

2146+
# GH6337
2147+
if not len(result_columns) and errors is not None:
2148+
raise errors
2149+
21412150
return DataFrame(result, columns=result_columns)
21422151

21432152
def _decide_output_index(self, output, labels):

pandas/tests/test_groupby.py

+20
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,26 @@ def f(grp):
370370
e.name = None
371371
assert_series_equal(result,e)
372372

373+
def test_agg_api(self):
374+
375+
# GH 6337
376+
# http://stackoverflow.com/questions/21706030/pandas-groupby-agg-function-column-dtype-error
377+
# different api for agg when passed custom function with mixed frame
378+
379+
df = DataFrame({'data1':np.random.randn(5),
380+
'data2':np.random.randn(5),
381+
'key1':['a','a','b','b','a'],
382+
'key2':['one','two','one','two','one']})
383+
grouped = df.groupby('key1')
384+
385+
def peak_to_peak(arr):
386+
return arr.max() - arr.min()
387+
388+
expected = grouped.agg([peak_to_peak])
389+
expected.columns=['data1','data2']
390+
result = grouped.agg(peak_to_peak)
391+
assert_frame_equal(result,expected)
392+
373393
def test_agg_regression1(self):
374394
grouped = self.tsframe.groupby([lambda x: x.year, lambda x: x.month])
375395
result = grouped.agg(np.mean)

0 commit comments

Comments
 (0)