Skip to content

Commit 0975505

Browse files
meeseeksmachinejreback
authored andcommitted
Backport PR #25264: BUG: groupby.transform retains timezone information (#25342)
1 parent fcfd1d4 commit 0975505

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v0.24.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Bug Fixes
7878

7979
**Reshaping**
8080

81-
-
81+
- 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`)
8282
- Bug in :func:`DataFrame.join` when joining on a timezone aware :class:`DatetimeIndex` (:issue:`23931`)
8383
-
8484

pandas/core/groupby/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ def _transform_fast(self, func, func_nm):
965965

966966
ids, _, ngroup = self.grouper.group_info
967967
cast = self._transform_should_cast(func_nm)
968-
out = algorithms.take_1d(func().values, ids)
968+
out = algorithms.take_1d(func()._values, ids)
969969
if cast:
970970
out = self._try_cast(out, self.obj)
971971
return Series(out, index=self.obj.index, name=self.obj.name)

pandas/tests/groupby/test_transform.py

+11
Original file line numberDiff line numberDiff line change
@@ -834,3 +834,14 @@ def demean_rename(x):
834834
tm.assert_frame_equal(result, expected)
835835
result_single = df.groupby('group').value.transform(demean_rename)
836836
tm.assert_series_equal(result_single, expected['value'])
837+
838+
839+
@pytest.mark.parametrize('func', [min, max, np.min, np.max, 'first', 'last'])
840+
def test_groupby_transform_timezone_column(func):
841+
# GH 24198
842+
ts = pd.to_datetime('now', utc=True).tz_convert('Asia/Singapore')
843+
result = pd.DataFrame({'end_time': [ts], 'id': [1]})
844+
result['max_end_time'] = result.groupby('id').end_time.transform(func)
845+
expected = pd.DataFrame([[ts, 1, ts]], columns=['end_time', 'id',
846+
'max_end_time'])
847+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)