Repository: pandas-dev/pandas

Issue:
Odd behaviour of resample+mean+interpolate on int64 series
this issue is present on the latest stable release (as well as latest master at the time of this writing). for frames with only `int64` values, the following has strange behaviour

```python
df = {"a": [1,3,1,4]}
df = pd.DataFrame(df, index=pd.date_range("2017-01-01", "2017-01-04"))

# these two are not the same
df.resample("H").mean()["a"].interpolate("cubic")  # bad
df.resample("H")["a"].mean().interpolate("cubic")  # good

# this works
df.astype("float64").resample("H").mean()["a"].interpolate("cubic")
```

my workaround is more than enough for me but i figured i'd report it anyway...

<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Darwin
OS-release: 16.5.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_IE.UTF-8
LANG: en_IE.UTF-8
LOCALE: en_IE.UTF-8

pandas: 0.21.0.dev+31.g0ea0f25bf
pytest: 3.0.7
pip: 9.0.1
setuptools: 35.0.2
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
xarray: None
IPython: 6.0.0
sphinx: None
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None
</details>



Discussion:
Thanks for the report!  It looks like the in the first case the internal data structures are getting into an invalid state.

```
In [106]: s1 = df.resample("H").mean()["a"]

In [107]: s1._data.blocks[0]
Out[107]: IntBlock: 73 dtype: float64

In [108]: s2 = df.resample("H")["a"].mean()

In [109]: s2._data.blocks[0]
Out[109]: FloatBlock: 73 dtype: float64
```
@jreback I think this blocker for 0.20.2? I can take a look tomorrow if you don't have time.
hmm did this work on 0.19.2?
I think so

```python
In [6]: pd.__version__
Out[6]: '0.19.2'

In [7]: df = {"a": [1,3,1,4]}
   ...: df = pd.DataFrame(df, index=pd.date_range("2017-01-01", "2017-01-04"))
   ...:

In [8]: df.resample("H").mean()["a"]._data.blocks[0]
Out[8]: FloatBlock: 73 dtype: float64
```

Resolving patch:
diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py
--- a/pandas/core/groupby.py
+++ b/pandas/core/groupby.py
@@ -3337,13 +3337,15 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True):
                 obj = self.obj[data.items[locs]]
                 s = groupby(obj, self.grouper)
                 result = s.aggregate(lambda x: alt(x, axis=self.axis))
-                result = result._data.blocks[0]
+                newb = result._data.blocks[0]
 
-            # see if we can cast the block back to the original dtype
-            result = block._try_coerce_and_cast_result(result)
+            finally:
+
+                # see if we can cast the block back to the original dtype
+                result = block._try_coerce_and_cast_result(result)
+                newb = block.make_block(result)
 
             new_items.append(locs)
-            newb = block.make_block_same_class(result)
             new_blocks.append(newb)
 
         if len(new_blocks) == 0:


Test patch:
diff --git a/pandas/tests/test_resample.py b/pandas/tests/test_resample.py
--- a/pandas/tests/test_resample.py
+++ b/pandas/tests/test_resample.py
@@ -1672,6 +1672,28 @@ def test_resample_dtype_preservation(self):
         result = df.groupby('group').resample('1D').ffill()
         assert result.val.dtype == np.int32
 
+    def test_resample_dtype_coerceion(self):
+
+        pytest.importorskip('scipy')
+
+        # GH 16361
+        df = {"a": [1, 3, 1, 4]}
+        df = pd.DataFrame(
+            df, index=pd.date_range("2017-01-01", "2017-01-04"))
+
+        expected = (df.astype("float64")
+                    .resample("H")
+                    .mean()
+                    ["a"]
+                    .interpolate("cubic")
+                    )
+
+        result = df.resample("H")["a"].mean().interpolate("cubic")
+        tm.assert_series_equal(result, expected)
+
+        result = df.resample("H").mean()["a"].interpolate("cubic")
+        tm.assert_series_equal(result, expected)
+
     def test_weekly_resample_buglet(self):
         # #1327
         rng = date_range('1/1/2000', freq='B', periods=20)