Skip to content

Commit f6daf50

Browse files
BUG: GH11616 fixes timezone selection error
Fixes GH11682 timzone info lost
1 parent 5823a6d commit f6daf50

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

doc/source/whatsnew/v0.18.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,6 @@ Performance Improvements
105105

106106
Bug Fixes
107107
~~~~~~~~~
108+
109+
- Bug aggregating on UTC timestamps with selection returns int64 object (:issue:`11616`)
110+
- Bug timezone info lost when broadcasting scalar datetime to DataFrame (:issue:`11682`)

pandas/core/common.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ def _infer_dtype_from_scalar(val):
10251025

10261026
dtype = np.object_
10271027

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

@@ -1321,7 +1321,9 @@ def _possibly_downcast_to_dtype(result, dtype):
13211321
try:
13221322
result = result.astype(dtype)
13231323
except:
1324-
pass
1324+
if dtype.tz:
1325+
# convert to datetime and change timezone
1326+
result = pd.to_datetime(result).tz_localize(dtype.tz)
13251327

13261328
except:
13271329
pass

pandas/tests/test_groupby.py

+22
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import pandas.core.nanops as nanops
3333

3434
import pandas.util.testing as tm
35+
import pytz
3536
import pandas as pd
3637
from numpy.testing import assert_equal
3738

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

4100+
def test_groupby_with_timezone_selection(self):
4101+
# GH 11616
4102+
# Test that column selection returns output in correct timezone.
4103+
np.random.seed(42)
4104+
df = pd.DataFrame({
4105+
'factor': np.random.randint(0, 3, size=60),
4106+
'time': pd.date_range('01/01/2000 00:00', periods=60, freq='s', tz='UTC')
4107+
})
4108+
df1 = df.groupby('factor').max()['time']
4109+
df2 = df.groupby('factor')['time'].max()
4110+
tm.assert_series_equal(df1, df2)
4111+
4112+
def test_timezone_info(self):
4113+
#GH 11682
4114+
# Timezone info lost when broadcasting scalar datetime to DataFrame
4115+
df = pd.DataFrame({'a': [1], 'b': [datetime.now(pytz.utc)]})
4116+
tm.assert_equal(df['b'][0].tzinfo, pytz.utc)
4117+
df = pd.DataFrame({'a': [1,2,3]})
4118+
df['b'] = datetime.now(pytz.utc)
4119+
tm.assert_equal(df['b'][0].tzinfo, pytz.utc)
4120+
40994121
def test_groupby_with_timegrouper(self):
41004122
# GH 4161
41014123
# TimeGrouper requires a sorted index

0 commit comments

Comments
 (0)