Skip to content

Commit 7d74809

Browse files
committed
BUG: Bug in .resample() and .groupby() when aggregating on integers
closes #16361
1 parent 9b0ea41 commit 7d74809

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

doc/source/whatsnew/v0.20.2.txt

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

8787
- Bug creating datetime rolling window on an empty DataFrame (:issue:`15819`)
8888
- Bug in ``rolling.cov()`` with offset window (:issue:`16058`)
89+
- Bug in ``.resample()`` and ``.groupby()`` when aggregating on integers (:issue:`16361`)
8990

9091

9192
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

+20
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,26 @@ 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+
# GH 16361
1678+
df = {"a": [1, 3, 1, 4]}
1679+
df = pd.DataFrame(
1680+
df, index=pd.date_range("2017-01-01", "2017-01-04"))
1681+
1682+
expected = (df.astype("float64")
1683+
.resample("H")
1684+
.mean()
1685+
["a"]
1686+
.interpolate("cubic")
1687+
)
1688+
1689+
result = df.resample("H")["a"].mean().interpolate("cubic")
1690+
tm.assert_series_equal(result, expected)
1691+
1692+
result = df.resample("H").mean()["a"].interpolate("cubic")
1693+
tm.assert_series_equal(result, expected)
1694+
16751695
def test_weekly_resample_buglet(self):
16761696
# #1327
16771697
rng = date_range('1/1/2000', freq='B', periods=20)

0 commit comments

Comments
 (0)