From fd6be3d1383ecfa93af2fdda1f0e3a90093d76bd Mon Sep 17 00:00:00 2001 From: jreback Date: Sat, 15 Jun 2013 07:37:46 -0400 Subject: [PATCH] BUG: (GH3911) groupby appying with custom function resulting in frame did not convert dtypes on the result object --- RELEASE.rst | 6 +++++- pandas/core/groupby.py | 2 +- pandas/tests/test_groupby.py | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/RELEASE.rst b/RELEASE.rst index 285bbb2095488..8fb9406a3ba0e 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -234,8 +234,11 @@ pandas 0.11.1 - ``read_html`` now correctly skips tests (GH3741_) - PandasObjects raise TypeError when trying to hash (GH3882_) - Fix incorrect arguments passed to concat that are not list-like (e.g. concat(df1,df2)) (GH3481_) - - Correctly parse when passed the ``dtype=str`` (or other variable-len string dtypes) in ``read_csv`` (GH3795_) + - Correctly parse when passed the ``dtype=str`` (or other variable-len string dtypes) + in ``read_csv`` (GH3795_) - Fix index name not propogating when using ``loc/ix`` (GH3880_) + - Fix groupby when applying a custom function resulting in a returned DataFrame was + not converting dtypes (GH3911_) .. _GH3164: https://github.com/pydata/pandas/issues/3164 .. _GH2786: https://github.com/pydata/pandas/issues/2786 @@ -331,6 +334,7 @@ pandas 0.11.1 .. _GH3873: https://github.com/pydata/pandas/issues/3873 .. _GH3877: https://github.com/pydata/pandas/issues/3877 .. _GH3880: https://github.com/pydata/pandas/issues/3880 +.. _GH3911: https://github.com/pydata/pandas/issues/3911 pandas 0.11.0 diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index 0be5d438e5e7c..168615c060c2b 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -1928,7 +1928,7 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False): return Series(values, index=key_index) return DataFrame(stacked_values, index=index, - columns=columns) + columns=columns).convert_objects() else: return Series(values, index=key_index) diff --git a/pandas/tests/test_groupby.py b/pandas/tests/test_groupby.py index f3a608b82e756..6989d3bcae42b 100644 --- a/pandas/tests/test_groupby.py +++ b/pandas/tests/test_groupby.py @@ -261,6 +261,20 @@ def test_groupby_nonobject_dtype(self): expected = self.mframe.groupby(key.astype('O')).sum() assert_frame_equal(result, expected) + # GH 3911, mixed frame non-conversion + df = self.df_mixed_floats.copy() + df['value'] = range(len(df)) + + def max_value(group): + return group.ix[group['value'].idxmax()] + + applied = df.groupby('A').apply(max_value) + result = applied.get_dtype_counts() + result.sort() + expected = Series({ 'object' : 2, 'float64' : 2, 'int64' : 1 }) + expected.sort() + assert_series_equal(result,expected) + def test_groupby_return_type(self): # GH2893, return a reduced type