@@ -186,63 +186,65 @@ def test_to_datetime_format_weeks(self, cache):
186
186
for s , format , dt in data :
187
187
assert to_datetime (s , format = format , cache = cache ) == dt
188
188
189
- @pytest .mark .skipif (not PY3 ,
190
- reason = "datetime.timezone not supported in PY2" )
191
- def test_to_datetime_parse_timezone (self ):
192
- # %Z parsing only
193
- fmt = '%Y-%m-%d %H:%M:%S %Z'
194
- dates = ['2010-01-01 12:00:00 UTC' ] * 2
195
- result = pd .to_datetime (dates , format = fmt )
196
- expected_dates = [pd .Timestamp ('2010-01-01 12:00:00' , tz = 'UTC' )] * 2
197
- expected = pd .DatetimeIndex (expected_dates )
198
- tm .assert_index_equal (result , expected )
199
-
200
- result = pd .to_datetime (dates , format = fmt , box = False )
201
- expected = np .array (expected_dates , dtype = object )
202
- tm .assert_numpy_array_equal (result , expected )
203
-
204
- dates = ['2010-01-01 12:00:00 UTC' , '2010-01-01 12:00:00 GMT' ]
205
- result = pd .to_datetime (dates , format = fmt )
206
- expected_dates = [pd .Timestamp ('2010-01-01 12:00:00' , tz = 'UTC' ),
207
- pd .Timestamp ('2010-01-01 12:00:00' , tz = 'GMT' )]
208
- expected = np .array (expected_dates , dtype = object )
209
- tm .assert_numpy_array_equal (result , expected )
210
-
211
- # %z parsing only
212
- dates = ['2010-01-01 12:00:00 +0100' ] * 2
213
- fmt = '%Y-%m-%d %H:%M:%S %z'
214
- result = pd .to_datetime (dates , format = fmt )
215
- expected_dates = [pd .Timestamp ('2010-01-01 12:00:00' ,
216
- tzinfo = pytz .FixedOffset (60 ))] * 2
217
- expected = pd .DatetimeIndex (expected_dates )
218
- tm .assert_index_equal (result , expected )
219
-
220
- result = pd .to_datetime (dates , format = fmt , box = False )
221
- expected = np .array (expected_dates , dtype = object )
222
- tm .assert_numpy_array_equal (result , expected )
189
+ @pytest .mark .parametrize ("box,const,assert_equal" , [
190
+ [True , pd .Index , 'assert_index_equal' ],
191
+ [False , np .array , 'assert_numpy_array_equal' ]])
192
+ @pytest .mark .parametrize ("fmt,dates,expected_dates" , [
193
+ ['%Y-%m-%d %H:%M:%S %Z' ,
194
+ ['2010-01-01 12:00:00 UTC' ] * 2 ,
195
+ [pd .Timestamp ('2010-01-01 12:00:00' , tz = 'UTC' )] * 2 ],
196
+ ['%Y-%m-%d %H:%M:%S %Z' ,
197
+ ['2010-01-01 12:00:00 UTC' , '2010-01-01 12:00:00 GMT' ],
198
+ [pd .Timestamp ('2010-01-01 12:00:00' , tz = 'UTC' ),
199
+ pd .Timestamp ('2010-01-01 12:00:00' , tz = 'GMT' )]],
200
+ ['%Y-%m-%d %H:%M:%S %z' ,
201
+ ['2010-01-01 12:00:00 +0100' ] * 2 ,
202
+ [pd .Timestamp ('2010-01-01 12:00:00' ,
203
+ tzinfo = pytz .FixedOffset (60 ))] * 2 ],
204
+ ['%Y-%m-%d %H:%M:%S %z' ,
205
+ ['2010-01-01 12:00:00 +0100' , '2010-01-01 12:00:00 -0100' ],
206
+ [pd .Timestamp ('2010-01-01 12:00:00' ,
207
+ tzinfo = pytz .FixedOffset (60 )),
208
+ pd .Timestamp ('2010-01-01 12:00:00' ,
209
+ tzinfo = pytz .FixedOffset (- 60 ))]]])
210
+ def test_to_datetime_parse_tzname_or_tzoffset (self , box , const ,
211
+ assert_equal , fmt ,
212
+ dates , expected_dates ):
213
+ # %z or %Z parsing
214
+ result = pd .to_datetime (dates , format = fmt , box = box )
215
+ expected = const (expected_dates )
216
+ getattr (tm , assert_equal )(result , expected )
223
217
224
- dates = ['2010-01-01 12:00:00 +0100' , '2010-01-01 12:00:00 -0100' ]
225
- result = pd .to_datetime (dates , format = fmt )
226
- expected_dates = [pd .Timestamp ('2010-01-01 12:00:00' ,
227
- tzinfo = pytz .FixedOffset (60 )),
228
- pd .Timestamp ('2010-01-01 12:00:00' ,
229
- tzinfo = pytz .FixedOffset (- 60 ))]
230
- expected = np .array (expected_dates , dtype = object )
231
- tm .assert_numpy_array_equal (result , expected )
218
+ with pytest .raises (ValueError ):
219
+ pd .to_datetime (dates , format = fmt , box = box , utc = True )
232
220
221
+ @pytest .mark .skipif (not PY3 ,
222
+ reason = "datetime.timezone not supported in PY2" )
223
+ @pytest .mark .parametrize ("box,const,assert_equal" , [
224
+ [True , pd .Index , 'assert_index_equal' ],
225
+ [False , np .array , 'assert_numpy_array_equal' ]])
226
+ @pytest .mark .parametrize ("dates,expected_dates" , [
227
+ [['2010-01-01 12:00:00 UTC +0100' ] * 2 ,
228
+ [pd .Timestamp ('2010-01-01 13:00:00' ,
229
+ tzinfo = timezone (timedelta (minutes = 60 ), 'UTC' ))] * 2 ],
230
+ [['2010-01-01 12:00:00 UTC +0100' , '2010-01-01 12:00:00 GMT -0200' ],
231
+ [pd .Timestamp ('2010-01-01 13:00:00' ,
232
+ tzinfo = timezone (timedelta (minutes = 60 ), 'UTC' )),
233
+ pd .Timestamp ('2010-01-01 10:00:00' ,
234
+ tzinfo = timezone (timedelta (minutes = - 120 ), 'GMT' ))]]])
235
+ def test_to_datetime_parse_tzname_and_tzoffset (self , box , const ,
236
+ assert_equal , dates ,
237
+ expected_dates ):
233
238
# %z and %Z parsing
234
- dates = ['2010-01-01 12:00:00 UTC +0100' ] * 2
235
239
fmt = '%Y-%m-%d %H:%M:%S %Z %z'
236
- result = pd .to_datetime (dates , format = fmt )
237
- tzinfo = timezone (timedelta (minutes = 60 ), 'UTC' )
238
- expected_dates = [pd .Timestamp ('2010-01-01 13:00:00' , tzinfo = tzinfo )]
239
- expected = np .array (expected_dates * 2 , dtype = object )
240
- tm .assert_numpy_array_equal (result , expected )
240
+ result = pd .to_datetime (dates , format = fmt , box = box )
241
+ expected = const (expected_dates )
242
+ getattr (tm , assert_equal )(result , expected )
241
243
242
244
with pytest .raises (ValueError ):
243
- pd .to_datetime (dates , format = fmt , utc = True )
245
+ pd .to_datetime (dates , format = fmt , box = box , utc = True )
244
246
245
- @pytest .mark .parametrize ('cache ' , ['+0' , '-1foo' , 'UTCbar' , ':10' ])
247
+ @pytest .mark .parametrize ('offset ' , ['+0' , '-1foo' , 'UTCbar' , ':10' ])
246
248
def test_to_datetime_parse_timezone_malformed (self , offset ):
247
249
fmt = '%Y-%m-%d %H:%M:%S %z'
248
250
date = '2010-01-01 12:00:00 ' + offset
0 commit comments