Skip to content

Commit

Permalink
BUG: Retain tz transformation in groupby.transform (#27510)
Browse files Browse the repository at this point in the history
* BUG: Retain tz transformation in groupby.transform
  • Loading branch information
mroeschke authored and TomAugspurger committed Jul 22, 2019
1 parent 86049dd commit 41e3b29
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Plotting
Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^

-
- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.transform` where applying a timezone conversion lambda function would drop timezone information (:issue:`27496`)
-
-

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from pandas.core.base import DataError, SpecificationError
import pandas.core.common as com
from pandas.core.frame import DataFrame
from pandas.core.generic import NDFrame, _shared_docs
from pandas.core.generic import ABCDataFrame, ABCSeries, NDFrame, _shared_docs
from pandas.core.groupby import base
from pandas.core.groupby.groupby import GroupBy, _apply_docs, _transform_template
from pandas.core.index import Index, MultiIndex
Expand Down Expand Up @@ -1025,8 +1025,8 @@ def transform(self, func, *args, **kwargs):
object.__setattr__(group, "name", name)
res = wrapper(group)

if hasattr(res, "values"):
res = res.values
if isinstance(res, (ABCDataFrame, ABCSeries)):
res = res._values

indexer = self._get_index(name)
s = klass(res, indexer)
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/groupby/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,3 +1001,27 @@ def test_ffill_not_in_axis(func, key, val):
expected = df

assert_frame_equal(result, expected)


def test_transform_lambda_with_datetimetz():
# GH 27496
df = DataFrame(
{
"time": [
Timestamp("2010-07-15 03:14:45"),
Timestamp("2010-11-19 18:47:06"),
],
"timezone": ["Etc/GMT+4", "US/Eastern"],
}
)
result = df.groupby(["timezone"])["time"].transform(
lambda x: x.dt.tz_localize(x.name)
)
expected = Series(
[
Timestamp("2010-07-15 03:14:45", tz="Etc/GMT+4"),
Timestamp("2010-11-19 18:47:06", tz="US/Eastern"),
],
name="time",
)
assert_series_equal(result, expected)

0 comments on commit 41e3b29

Please sign in to comment.