Skip to content

Commit 60aff01

Browse files
committed
BUG: Bug in .resample() and .groupby() when aggregating on integers
closes #16361
1 parent 03d44f3 commit 60aff01

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

doc/source/whatsnew/v0.20.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Groupby/Resample/Rolling
8888

8989
- Bug creating datetime rolling window on an empty DataFrame (:issue:`15819`)
9090
- Bug in ``rolling.cov()`` with offset window (:issue:`16058`)
91+
- Bug in ``.resample()`` and ``.groupby()`` when aggregating on integers (:issue:`16361`)
9192

9293

9394
Sparse

pandas/core/groupby.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -3337,13 +3337,15 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True):
33373337
obj = self.obj[data.items[locs]]
33383338
s = groupby(obj, self.grouper)
33393339
result = s.aggregate(lambda x: alt(x, axis=self.axis))
3340-
result = result._data.blocks[0]
3340+
newb = result._data.blocks[0]
33413341

3342-
# see if we can cast the block back to the original dtype
3343-
result = block._try_coerce_and_cast_result(result)
3342+
finally:
3343+
3344+
# see if we can cast the block back to the original dtype
3345+
result = block._try_coerce_and_cast_result(result)
3346+
newb = block.make_block(result)
33443347

33453348
new_items.append(locs)
3346-
newb = block.make_block_same_class(result)
33473349
new_blocks.append(newb)
33483350

33493351
if len(new_blocks) == 0:

pandas/tests/test_resample.py

+22
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,28 @@ def test_resample_dtype_preservation(self):
16721672
result = df.groupby('group').resample('1D').ffill()
16731673
assert result.val.dtype == np.int32
16741674

1675+
def test_resample_dtype_coerceion(self):
1676+
1677+
pytest.importorskip('scipy')
1678+
1679+
# GH 16361
1680+
df = {"a": [1, 3, 1, 4]}
1681+
df = pd.DataFrame(
1682+
df, index=pd.date_range("2017-01-01", "2017-01-04"))
1683+
1684+
expected = (df.astype("float64")
1685+
.resample("H")
1686+
.mean()
1687+
["a"]
1688+
.interpolate("cubic")
1689+
)
1690+
1691+
result = df.resample("H")["a"].mean().interpolate("cubic")
1692+
tm.assert_series_equal(result, expected)
1693+
1694+
result = df.resample("H").mean()["a"].interpolate("cubic")
1695+
tm.assert_series_equal(result, expected)
1696+
16751697
def test_weekly_resample_buglet(self):
16761698
# #1327
16771699
rng = date_range('1/1/2000', freq='B', periods=20)

0 commit comments

Comments
 (0)