Skip to content

BUG: GH11616 fixes timezone selection error #11672

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

Closed
Closed
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
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,6 @@ Performance Improvements

Bug Fixes
~~~~~~~~~

- Bug aggregating on UTC timestamps with selection returns int64 object (:issue:`11616`)
- Bug timezone info lost when broadcasting scalar datetime to DataFrame (:issue:`11682`)
6 changes: 4 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ def _infer_dtype_from_scalar(val):

dtype = np.object_

elif isinstance(val, (np.datetime64, datetime)) and getattr(val,'tz',None) is None:
elif isinstance(val, (np.datetime64, datetime)) and getattr(val,'tzinfo',None) is None:
val = lib.Timestamp(val).value
dtype = np.dtype('M8[ns]')

Expand Down Expand Up @@ -1321,7 +1321,9 @@ def _possibly_downcast_to_dtype(result, dtype):
try:
result = result.astype(dtype)
except:
pass
if dtype.tz:
# convert to datetime and change timezone
result = pd.to_datetime(result).tz_localize(dtype.tz)

except:
pass
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import pandas.core.nanops as nanops

import pandas.util.testing as tm
import pytz
import pandas as pd
from numpy.testing import assert_equal

Expand Down Expand Up @@ -4096,6 +4097,27 @@ def test_groupby_with_empty(self):
grouped = series.groupby(grouper)
assert next(iter(grouped), None) is None

def test_groupby_with_timezone_selection(self):
# GH 11616
# Test that column selection returns output in correct timezone.
np.random.seed(42)
df = pd.DataFrame({
'factor': np.random.randint(0, 3, size=60),
'time': pd.date_range('01/01/2000 00:00', periods=60, freq='s', tz='UTC')
})
df1 = df.groupby('factor').max()['time']
df2 = df.groupby('factor')['time'].max()
tm.assert_series_equal(df1, df2)

def test_timezone_info(self):
#GH 11682
# Timezone info lost when broadcasting scalar datetime to DataFrame
df = pd.DataFrame({'a': [1], 'b': [datetime.now(pytz.utc)]})
tm.assert_equal(df['b'][0].tzinfo, pytz.utc)
df = pd.DataFrame({'a': [1,2,3]})
df['b'] = datetime.now(pytz.utc)
tm.assert_equal(df['b'][0].tzinfo, pytz.utc)

def test_groupby_with_timegrouper(self):
# GH 4161
# TimeGrouper requires a sorted index
Expand Down