Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Bug in .resample() and .groupby() when aggregating on integers #16549

Merged
merged 1 commit into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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