Skip to content

Commit

Permalink
Backport PR pandas-dev#25264: BUG: groupby.transform retains timezone…
Browse files Browse the repository at this point in the history
… information
  • Loading branch information
mroeschke authored and MeeseeksDev[bot] committed Feb 16, 2019
1 parent fcfd1d4 commit d93879a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Bug Fixes

**Reshaping**

-
- Bug in :meth:`pandas.core.groupby.GroupBy.transform` where applying a function to a timezone aware column would return a timezone naive result (:issue:`24198`)
- Bug in :func:`DataFrame.join` when joining on a timezone aware :class:`DatetimeIndex` (:issue:`23931`)
-

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ def _transform_fast(self, func, func_nm):

ids, _, ngroup = self.grouper.group_info
cast = self._transform_should_cast(func_nm)
out = algorithms.take_1d(func().values, ids)
out = algorithms.take_1d(func()._values, ids)
if cast:
out = self._try_cast(out, self.obj)
return Series(out, index=self.obj.index, name=self.obj.name)
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/groupby/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,3 +834,14 @@ def demean_rename(x):
tm.assert_frame_equal(result, expected)
result_single = df.groupby('group').value.transform(demean_rename)
tm.assert_series_equal(result_single, expected['value'])


@pytest.mark.parametrize('func', [min, max, np.min, np.max, 'first', 'last'])
def test_groupby_transform_timezone_column(func):
# GH 24198
ts = pd.to_datetime('now', utc=True).tz_convert('Asia/Singapore')
result = pd.DataFrame({'end_time': [ts], 'id': [1]})
result['max_end_time'] = result.groupby('id').end_time.transform(func)
expected = pd.DataFrame([[ts, 1, ts]], columns=['end_time', 'id',
'max_end_time'])
tm.assert_frame_equal(result, expected)

0 comments on commit d93879a

Please sign in to comment.