Skip to content

Commit

Permalink
BUG: Bug in .resample() and .groupby() when aggregating on integers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback authored May 31, 2017
1 parent 03d44f3 commit e437ad5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Groupby/Resample/Rolling

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


Sparse
Expand Down
10 changes: 6 additions & 4 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e437ad5

Please sign in to comment.