diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index 5ccf829fd5a42..fa16a0513f4bb 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -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`) diff --git a/pandas/core/common.py b/pandas/core/common.py index 43d018edb872c..b3a42335e14da 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -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]') @@ -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 diff --git a/pandas/tests/test_groupby.py b/pandas/tests/test_groupby.py index 025ed17194979..0120320692f9a 100644 --- a/pandas/tests/test_groupby.py +++ b/pandas/tests/test_groupby.py @@ -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 @@ -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