Skip to content

Commit ae0ea81

Browse files
committed
BUG: GH15426 timezone lost in groupby-agg with cython functions
1 parent 9b5d848 commit ae0ea81

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ Bug Fixes
578578

579579
- Bug in ``DataFrame.to_stata()`` and ``StataWriter`` which produces incorrectly formatted files to be produced for some locales (:issue:`13856`)
580580
- Bug in ``pd.concat()`` in which concatting with an empty dataframe with ``join='inner'`` was being improperly handled (:issue:`15328`)
581+
- Bug in ``groupby.agg()`` incorrectly localizing timezone on ``datetime`` (:issue:`15426`)
581582

582583

583584

pandas/tests/groupby/test_aggregate.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77

88
from __future__ import print_function
9-
from datetime import datetime
9+
from datetime import datetime, timedelta
1010
from functools import partial
1111

1212
import numpy as np
@@ -738,3 +738,14 @@ def test_agg_over_numpy_arrays(self):
738738
columns=expected_column)
739739

740740
assert_frame_equal(result, expected)
741+
742+
def test_agg_time_zone_round_trip(self):
743+
ts = pd.Timestamp("2016-01-01 12:00:00", tz='US/Pacific')
744+
df = pd.DataFrame({'a': 1, 'b': [ts + timedelta(minutes=nn)
745+
for nn in range(10)]})
746+
747+
result1 = df.groupby('a')['b'].agg(np.min).iloc[0]
748+
result2 = df.groupby('a')['b'].agg(lambda x: np.min(x)).iloc[0]
749+
750+
self.assertEqual(result1, ts)
751+
self.assertEqual(result2, ts)

pandas/types/cast.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ def trans(x): # noqa
133133
if dtype.tz:
134134
# convert to datetime and change timezone
135135
from pandas import to_datetime
136-
result = to_datetime(result).tz_localize(dtype.tz)
136+
result = to_datetime(result).tz_localize('utc')
137+
result = result.tz_convert(dtype.tz)
137138

138139
except:
139140
pass

0 commit comments

Comments
 (0)