4
4
from datetime import datetime , time
5
5
from distutils .version import LooseVersion
6
6
import locale
7
- from warnings import catch_warnings , simplefilter
8
7
9
8
import dateutil
10
9
from dateutil .parser import parse
@@ -222,9 +221,7 @@ def test_to_datetime_format_weeks(self, cache):
222
221
def test_to_datetime_parse_tzname_or_tzoffset (self , box , const ,
223
222
fmt , dates , expected_dates ):
224
223
# GH 13486
225
- with catch_warnings ():
226
- simplefilter ("ignore" , FutureWarning )
227
-
224
+ with tm .assert_produces_warning (FutureWarning ):
228
225
result = pd .to_datetime (dates , format = fmt , box = box )
229
226
expected = const (expected_dates )
230
227
tm .assert_equal (result , expected )
@@ -260,11 +257,8 @@ def test_to_datetime_dtarr(self, tz):
260
257
result = to_datetime (arr )
261
258
assert result is arr
262
259
263
- with catch_warnings ():
264
- simplefilter ("ignore" , FutureWarning )
265
-
266
- result = to_datetime (arr , box = True )
267
- assert result is arr
260
+ result = to_datetime (arr )
261
+ assert result is arr
268
262
269
263
def test_to_datetime_pydatetime (self ):
270
264
actual = pd .to_datetime (datetime (2008 , 1 , 15 ))
@@ -364,8 +358,7 @@ def test_to_datetime_dt64s(self, cache):
364
358
def test_to_datetime_array_of_dt64s (self , cache ):
365
359
dts = [np .datetime64 ('2000-01-01' ), np .datetime64 ('2000-01-02' ), ]
366
360
367
- with catch_warnings ():
368
- simplefilter ("ignore" , FutureWarning )
361
+ with tm .assert_produces_warning (FutureWarning ):
369
362
370
363
# Assuming all datetimes are in bounds, to_datetime() returns
371
364
# an array that is equal to Timestamp() parsing
@@ -380,6 +373,7 @@ def test_to_datetime_array_of_dt64s(self, cache):
380
373
pytest .raises (ValueError , pd .to_datetime , dts_with_oob ,
381
374
errors = 'raise' )
382
375
376
+ with tm .assert_produces_warning (FutureWarning ):
383
377
tm .assert_numpy_array_equal (
384
378
pd .to_datetime (dts_with_oob , box = False , errors = 'coerce' ,
385
379
cache = cache ),
@@ -393,6 +387,7 @@ def test_to_datetime_array_of_dt64s(self, cache):
393
387
)
394
388
)
395
389
390
+ with tm .assert_produces_warning (FutureWarning ):
396
391
# With errors='ignore', out of bounds datetime64s
397
392
# are converted to their .item(), which depending on the version of
398
393
# numpy is either a python datetime.datetime or datetime.date
@@ -574,11 +569,11 @@ def test_to_datetime_cache(self, utc, format, box, constructor):
574
569
test_dates = [date ] * 10 ** 5
575
570
data = constructor (test_dates )
576
571
577
- with catch_warnings ():
578
- simplefilter ("ignore" , FutureWarning )
579
-
572
+ with tm .assert_produces_warning (FutureWarning ):
580
573
result = pd .to_datetime (data , utc = utc , format = format , box = box ,
581
574
cache = True )
575
+
576
+ with tm .assert_produces_warning (FutureWarning ):
582
577
expected = pd .to_datetime (data , utc = utc , format = format , box = box ,
583
578
cache = False )
584
579
if box :
@@ -634,8 +629,7 @@ def test_iso_8601_strings_same_offset_no_box(self):
634
629
# GH 22446
635
630
data = ['2018-01-04 09:01:00+09:00' , '2018-01-04 09:02:00+09:00' ]
636
631
637
- with catch_warnings ():
638
- simplefilter ("ignore" , FutureWarning )
632
+ with tm .assert_produces_warning (FutureWarning ):
639
633
result = pd .to_datetime (data , box = False )
640
634
641
635
expected = np .array ([
@@ -993,8 +987,7 @@ def test_dataframe_box_false(self):
993
987
'month' : [2 , 3 ],
994
988
'day' : [4 , 5 ]})
995
989
996
- with catch_warnings ():
997
- simplefilter ("ignore" , FutureWarning )
990
+ with tm .assert_produces_warning (FutureWarning ):
998
991
result = pd .to_datetime (df , box = False )
999
992
1000
993
expected = np .array (['2015-02-04' , '2016-03-05' ],
@@ -1139,13 +1132,14 @@ def test_to_datetime_types(self, cache):
1139
1132
def test_to_datetime_unprocessable_input (self , cache , box , klass ):
1140
1133
# GH 4928
1141
1134
# GH 21864
1142
- with catch_warnings ():
1143
- simplefilter ("ignore" , FutureWarning )
1135
+ with tm .assert_produces_warning (FutureWarning ):
1144
1136
result = to_datetime ([1 , '1' ], errors = 'ignore' , cache = cache ,
1145
1137
box = box )
1146
1138
1147
1139
expected = klass (np .array ([1 , '1' ], dtype = 'O' ))
1148
1140
tm .assert_equal (result , expected )
1141
+
1142
+ with tm .assert_produces_warning (FutureWarning ):
1149
1143
pytest .raises (TypeError , to_datetime , [1 , '1' ], errors = 'raise' ,
1150
1144
cache = cache , box = box )
1151
1145
0 commit comments