Skip to content
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 groupby idxmin #25531

Merged
merged 15 commits into from Mar 30, 2019
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.2.rst
Expand Up @@ -86,6 +86,7 @@ Bug Fixes

- Bug in :meth:`pandas.core.groupby.GroupBy.transform` where applying a function to a timezone aware column would return a timezone naive result (:issue:`24198`)
- Bug in :func:`DataFrame.join` when joining on a timezone aware :class:`DatetimeIndex` (:issue:`23931`)
- Bug in groupby.idxmin() that returns DateTime values for DataTime columns (:issue:`25444`)
jreback marked this conversation as resolved.
Show resolved Hide resolved
-

**Visualization**
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/groupby/base.py
Expand Up @@ -89,7 +89,8 @@ def _gotitem(self, key, ndim, subset=None):
cython_transforms = frozenset(['cumprod', 'cumsum', 'shift',
'cummin', 'cummax'])

cython_cast_blacklist = frozenset(['rank', 'count', 'size'])
cython_cast_blacklist = frozenset(['rank', 'count', 'size', 'idxmin',
'idxmax'])


def whitelist_method_generator(base, klass, whitelist):
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/groupby/generic.py
Expand Up @@ -254,8 +254,13 @@ def _aggregate_item_by_item(self, func, *args, **kwargs):
data = obj[item]
colg = SeriesGroupBy(data, selection=item,
grouper=self.grouper)
result[item] = self._try_cast(
colg.aggregate(func, *args, **kwargs), data)

jreback marked this conversation as resolved.
Show resolved Hide resolved
cast = self._transform_should_cast(func)

result[item] = colg.aggregate(func, *args, **kwargs)
if cast:
result[item] = self._try_cast(result[item], data)

except ValueError:
cannot_agg.append(item)
continue
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/groupby/test_function.py
Expand Up @@ -400,6 +400,25 @@ def test_groupby_non_arithmetic_agg_int_like_precision(i):
assert res.iloc[0].b == data["expected"]


@pytest.mark.parametrize("func, values", [
("idxmin", {'c_int': [0, 2], 'c_float': [1, 3], 'c_date': [1, 2]}),
("idxmax", {'c_int': [1, 3], 'c_float': [0, 2], 'c_date': [0, 3]})
])
def test_idxmin_idxmax_returns_int_types(func, values):
# GH 25444
df = pd.DataFrame({'name': ['A', 'A', 'B', 'B'],
'c_int': [1, 2, 3, 4],
'c_float': [4.02, 3.03, 2.04, 1.05],
'c_date': ['2019', '2018', '2016', '2017']})
df['c_date'] = pd.to_datetime(df['c_date'])

result = getattr(df.groupby('name'), func)()

expected = pd.DataFrame(values, index=Index(['A', 'B'], name="name"))

tm.assert_frame_equal(result, expected)


def test_fill_consistency():

# GH9221
Expand Down