Skip to content

Commit e838266

Browse files
varunkumar-devjreback
authored andcommitted
BUG: fixes timezone selection error, #11616 & timezone info lost, #11682
1 parent 44cc8e5 commit e838266

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

doc/source/whatsnew/v0.18.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,7 @@ Bug Fixes
108108

109109

110110

111+
111112
- Bug in ``.loc`` against ``CategoricalIndex`` may result in normal ``Index`` (:issue:`11586`)
113+
- Bug groupby on tz-aware data where selection not returning ``Timestamp`` (:issue:`11616`)
114+
- Bug in 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

+24
Original file line numberDiff line numberDiff line change
@@ -4096,6 +4096,30 @@ def test_groupby_with_empty(self):
40964096
grouped = series.groupby(grouper)
40974097
assert next(iter(grouped), None) is None
40984098

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

0 commit comments

Comments
 (0)