diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index fff28c329494b..5a0bc590eb4f4 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -3568,7 +3568,8 @@ def first_non_None_value(values): # as we are stacking can easily have object dtypes here so = self._selected_obj if (so.ndim == 2 and so.dtypes.apply(is_datetimelike).any()): - result = result.apply(lambda x: pd.to_numeric(x, errors='ignore')) + result = result.apply( + lambda x: pd.to_numeric(x, errors='ignore')) date_cols = self._selected_obj.select_dtypes( include=['datetime', 'timedelta']).columns date_cols = date_cols.intersection(result.columns) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index fb0a103caef59..263eb6497e990 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -4316,12 +4316,15 @@ def test_cummin_cummax(self): def test_numeric_coercion(self): # GH 14423 - df = pd.DataFrame({'Number' : [1, 2], 'Date' : ["2017-03-02"] * 2, 'Str' : ["foo", "inf"]}) + df = pd.DataFrame({'Number': [1, 2], + 'Date': ["2017-03-02"] * 2, + 'Str': ["foo", "inf"]}) expected = df.groupby(['Number']).apply(lambda x: x.iloc[0]) df.Date = pd.to_datetime(df.Date) result = df.groupby(['Number']).apply(lambda x: x.iloc[0]) tm.assert_series_equal(result['Str'], expected['Str']) - + + def _check_groupby(df, result, keys, field, f=lambda x: x.sum()): tups = lmap(tuple, df[keys].values) tups = com._asarray_tuplesafe(tups)