File tree 4 files changed +44
-3
lines changed
4 files changed +44
-3
lines changed Original file line number Diff line number Diff line change @@ -622,6 +622,7 @@ Bug Fixes
622
622
623
623
- Bug in ``DataFrame.to_stata()`` and ``StataWriter`` which produces incorrectly formatted files to be produced for some locales (:issue:`13856`)
624
624
- Bug in ``pd.concat()`` in which concatting with an empty dataframe with ``join='inner'`` was being improperly handled (:issue:`15328`)
625
+ - Bug in ``groupby.agg()`` incorrectly localizing timezone on ``datetime`` (:issue:`15426`, :issue:`10668`)
625
626
626
627
627
628
Original file line number Diff line number Diff line change 6
6
"""
7
7
8
8
from __future__ import print_function
9
- from datetime import datetime
9
+ from datetime import datetime , timedelta
10
10
from functools import partial
11
11
12
12
import numpy as np
@@ -738,3 +738,32 @@ def test_agg_over_numpy_arrays(self):
738
738
columns = expected_column )
739
739
740
740
assert_frame_equal (result , expected )
741
+
742
+ def test_agg_timezone_round_trip (self ):
743
+ # GH 15426
744
+ ts = pd .Timestamp ("2016-01-01 12:00:00" , tz = 'US/Pacific' )
745
+ df = pd .DataFrame ({'a' : 1 , 'b' : [ts + timedelta (minutes = nn )
746
+ for nn in range (10 )]})
747
+
748
+ result1 = df .groupby ('a' )['b' ].agg (np .min ).iloc [0 ]
749
+ result2 = df .groupby ('a' )['b' ].agg (lambda x : np .min (x )).iloc [0 ]
750
+ result3 = df .groupby ('a' )['b' ].min ().iloc [0 ]
751
+
752
+ assert result1 == ts
753
+ assert result2 == ts
754
+ assert result3 == ts
755
+
756
+ dates = [pd .Timestamp ("2016-01-0%d 12:00:00" % i , tz = 'US/Pacific' )
757
+ for i in range (1 , 5 )]
758
+ df = pd .DataFrame ({'A' : ['a' , 'b' ] * 2 , 'B' : dates })
759
+ grouped = df .groupby ('A' )
760
+
761
+ ts = df ['B' ].iloc [0 ]
762
+ assert ts == grouped .nth (0 )['B' ].iloc [0 ]
763
+ assert ts == grouped .head (1 )['B' ].iloc [0 ]
764
+ assert ts == grouped .first ()['B' ].iloc [0 ]
765
+ assert ts == grouped .apply (lambda x : x .iloc [0 ])[0 ]
766
+
767
+ ts = df ['B' ].iloc [2 ]
768
+ assert ts == grouped .last ()['B' ].iloc [0 ]
769
+ assert ts == grouped .apply (lambda x : x .iloc [- 1 ])[0 ]
Original file line number Diff line number Diff line change 8
8
from datetime import datetime
9
9
import numpy as np
10
10
11
- from pandas import Timedelta , Timestamp
11
+ from pandas import Timedelta , Timestamp , DatetimeIndex
12
12
from pandas .types .cast import (_possibly_downcast_to_dtype ,
13
13
_possibly_convert_objects ,
14
14
_infer_dtype_from_scalar ,
@@ -71,6 +71,16 @@ def test_datetimelikes_nan(self):
71
71
res = _possibly_downcast_to_dtype (arr , 'timedelta64[ns]' )
72
72
tm .assert_numpy_array_equal (res , exp )
73
73
74
+ def test_datetime_with_timezone (self ):
75
+ # GH 15426
76
+ ts = Timestamp ("2016-01-01 12:00:00" , tz = 'US/Pacific' )
77
+ exp = DatetimeIndex ([ts , ts ])
78
+ res = _possibly_downcast_to_dtype (exp , exp .dtype )
79
+ tm .assert_index_equal (res , exp )
80
+
81
+ res = _possibly_downcast_to_dtype (exp .asi8 , exp .dtype )
82
+ tm .assert_index_equal (res , exp )
83
+
74
84
75
85
class TestInferDtype (tm .TestCase ):
76
86
Original file line number Diff line number Diff line change @@ -133,7 +133,8 @@ def trans(x): # noqa
133
133
if dtype .tz :
134
134
# convert to datetime and change timezone
135
135
from pandas import to_datetime
136
- result = to_datetime (result ).tz_localize (dtype .tz )
136
+ result = to_datetime (result ).tz_localize ('utc' )
137
+ result = result .tz_convert (dtype .tz )
137
138
138
139
except :
139
140
pass
You can’t perform that action at this time.
0 commit comments