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
7
8
8
9
import dateutil
9
10
from dateutil .parser import parse
@@ -221,12 +222,15 @@ def test_to_datetime_format_weeks(self, cache):
221
222
def test_to_datetime_parse_tzname_or_tzoffset (self , box , const ,
222
223
fmt , dates , expected_dates ):
223
224
# GH 13486
224
- result = pd .to_datetime (dates , format = fmt , box = box )
225
- expected = const (expected_dates )
226
- tm .assert_equal (result , expected )
225
+ with catch_warnings ():
226
+ simplefilter ("ignore" , FutureWarning )
227
227
228
- with pytest .raises (ValueError ):
229
- pd .to_datetime (dates , format = fmt , box = box , utc = True )
228
+ result = pd .to_datetime (dates , format = fmt , box = box )
229
+ expected = const (expected_dates )
230
+ tm .assert_equal (result , expected )
231
+
232
+ with pytest .raises (ValueError ):
233
+ pd .to_datetime (dates , format = fmt , box = box , utc = True )
230
234
231
235
@pytest .mark .parametrize ('offset' , [
232
236
'+0' , '-1foo' , 'UTCbar' , ':10' , '+01:000:01' , '' ])
@@ -256,8 +260,11 @@ def test_to_datetime_dtarr(self, tz):
256
260
result = to_datetime (arr )
257
261
assert result is arr
258
262
259
- result = to_datetime (arr , box = True )
260
- assert result is arr
263
+ with catch_warnings ():
264
+ simplefilter ("ignore" , FutureWarning )
265
+
266
+ result = to_datetime (arr , box = True )
267
+ assert result is arr
261
268
262
269
def test_to_datetime_pydatetime (self ):
263
270
actual = pd .to_datetime (datetime (2008 , 1 , 15 ))
@@ -357,43 +364,46 @@ def test_to_datetime_dt64s(self, cache):
357
364
def test_to_datetime_array_of_dt64s (self , cache ):
358
365
dts = [np .datetime64 ('2000-01-01' ), np .datetime64 ('2000-01-02' ), ]
359
366
360
- # Assuming all datetimes are in bounds, to_datetime() returns
361
- # an array that is equal to Timestamp() parsing
362
- tm .assert_numpy_array_equal (
363
- pd .to_datetime (dts , box = False , cache = cache ),
364
- np .array ([Timestamp (x ).asm8 for x in dts ])
365
- )
366
-
367
- # A list of datetimes where the last one is out of bounds
368
- dts_with_oob = dts + [np .datetime64 ('9999-01-01' )]
367
+ with catch_warnings ():
368
+ simplefilter ("ignore" , FutureWarning )
369
369
370
- pytest .raises (ValueError , pd .to_datetime , dts_with_oob ,
371
- errors = 'raise' )
370
+ # Assuming all datetimes are in bounds, to_datetime() returns
371
+ # an array that is equal to Timestamp() parsing
372
+ tm .assert_numpy_array_equal (
373
+ pd .to_datetime (dts , box = False , cache = cache ),
374
+ np .array ([Timestamp (x ).asm8 for x in dts ])
375
+ )
372
376
373
- tm .assert_numpy_array_equal (
374
- pd .to_datetime (dts_with_oob , box = False , errors = 'coerce' ,
375
- cache = cache ),
376
- np .array (
377
- [
378
- Timestamp (dts_with_oob [0 ]).asm8 ,
379
- Timestamp (dts_with_oob [1 ]).asm8 ,
380
- tslib .iNaT ,
381
- ],
382
- dtype = 'M8'
377
+ # A list of datetimes where the last one is out of bounds
378
+ dts_with_oob = dts + [np .datetime64 ('9999-01-01' )]
379
+
380
+ pytest .raises (ValueError , pd .to_datetime , dts_with_oob ,
381
+ errors = 'raise' )
382
+
383
+ tm .assert_numpy_array_equal (
384
+ pd .to_datetime (dts_with_oob , box = False , errors = 'coerce' ,
385
+ cache = cache ),
386
+ np .array (
387
+ [
388
+ Timestamp (dts_with_oob [0 ]).asm8 ,
389
+ Timestamp (dts_with_oob [1 ]).asm8 ,
390
+ tslib .iNaT ,
391
+ ],
392
+ dtype = 'M8'
393
+ )
383
394
)
384
- )
385
-
386
- # With errors='ignore', out of bounds datetime64s
387
- # are converted to their .item(), which depending on the version of
388
- # numpy is either a python datetime.datetime or datetime.date
389
- tm . assert_numpy_array_equal (
390
- pd . to_datetime ( dts_with_oob , box = False , errors = 'ignore' ,
391
- cache = cache ),
392
- np . array (
393
- [ dt . item () for dt in dts_with_oob ],
394
- dtype = 'O'
395
+
396
+ # With errors='ignore', out of bounds datetime64s
397
+ # are converted to their .item(), which depending on the version of
398
+ # numpy is either a python datetime.datetime or datetime.date
399
+ tm . assert_numpy_array_equal (
400
+ pd . to_datetime ( dts_with_oob , box = False , errors = 'ignore' ,
401
+ cache = cache ) ,
402
+ np . array (
403
+ [ dt . item () for dt in dts_with_oob ],
404
+ dtype = 'O'
405
+ )
395
406
)
396
- )
397
407
398
408
@pytest .mark .parametrize ('cache' , [True , False ])
399
409
def test_to_datetime_tz (self , cache ):
@@ -563,10 +573,14 @@ def test_to_datetime_cache(self, utc, format, box, constructor):
563
573
date = '20130101 00:00:00'
564
574
test_dates = [date ] * 10 ** 5
565
575
data = constructor (test_dates )
566
- result = pd .to_datetime (data , utc = utc , format = format , box = box ,
567
- cache = True )
568
- expected = pd .to_datetime (data , utc = utc , format = format , box = box ,
569
- cache = False )
576
+
577
+ with catch_warnings ():
578
+ simplefilter ("ignore" , FutureWarning )
579
+
580
+ result = pd .to_datetime (data , utc = utc , format = format , box = box ,
581
+ cache = True )
582
+ expected = pd .to_datetime (data , utc = utc , format = format , box = box ,
583
+ cache = False )
570
584
if box :
571
585
tm .assert_index_equal (result , expected )
572
586
else :
@@ -619,7 +633,11 @@ def test_iso_8601_strings_with_same_offset(self):
619
633
def test_iso_8601_strings_same_offset_no_box (self ):
620
634
# GH 22446
621
635
data = ['2018-01-04 09:01:00+09:00' , '2018-01-04 09:02:00+09:00' ]
622
- result = pd .to_datetime (data , box = False )
636
+
637
+ with catch_warnings ():
638
+ simplefilter ("ignore" , FutureWarning )
639
+ result = pd .to_datetime (data , box = False )
640
+
623
641
expected = np .array ([
624
642
datetime (2018 , 1 , 4 , 9 , 1 , tzinfo = pytz .FixedOffset (540 )),
625
643
datetime (2018 , 1 , 4 , 9 , 2 , tzinfo = pytz .FixedOffset (540 ))
@@ -813,7 +831,7 @@ def test_unit_rounding(self, cache):
813
831
def test_unit_ignore_keeps_name (self , cache ):
814
832
# GH 21697
815
833
expected = pd .Index ([15e9 ] * 2 , name = 'name' )
816
- result = pd .to_datetime (expected , errors = 'ignore' , box = True , unit = 's' ,
834
+ result = pd .to_datetime (expected , errors = 'ignore' , unit = 's' ,
817
835
cache = cache )
818
836
tm .assert_index_equal (result , expected )
819
837
@@ -974,7 +992,11 @@ def test_dataframe_box_false(self):
974
992
df = pd .DataFrame ({'year' : [2015 , 2016 ],
975
993
'month' : [2 , 3 ],
976
994
'day' : [4 , 5 ]})
977
- result = pd .to_datetime (df , box = False )
995
+
996
+ with catch_warnings ():
997
+ simplefilter ("ignore" , FutureWarning )
998
+ result = pd .to_datetime (df , box = False )
999
+
978
1000
expected = np .array (['2015-02-04' , '2016-03-05' ],
979
1001
dtype = 'datetime64[ns]' )
980
1002
tm .assert_numpy_array_equal (result , expected )
@@ -991,8 +1013,7 @@ def test_dataframe_utc_true(self):
991
1013
992
1014
def test_to_datetime_errors_ignore_utc_true (self ):
993
1015
# GH 23758
994
- result = pd .to_datetime ([1 ], unit = 's' , box = True , utc = True ,
995
- errors = 'ignore' )
1016
+ result = pd .to_datetime ([1 ], unit = 's' , utc = True , errors = 'ignore' )
996
1017
expected = DatetimeIndex (['1970-01-01 00:00:01' ], tz = 'UTC' )
997
1018
tm .assert_index_equal (result , expected )
998
1019
@@ -1118,11 +1139,15 @@ def test_to_datetime_types(self, cache):
1118
1139
def test_to_datetime_unprocessable_input (self , cache , box , klass ):
1119
1140
# GH 4928
1120
1141
# GH 21864
1121
- result = to_datetime ([1 , '1' ], errors = 'ignore' , cache = cache , box = box )
1122
- expected = klass (np .array ([1 , '1' ], dtype = 'O' ))
1123
- tm .assert_equal (result , expected )
1124
- pytest .raises (TypeError , to_datetime , [1 , '1' ], errors = 'raise' ,
1125
- cache = cache , box = box )
1142
+ with catch_warnings ():
1143
+ simplefilter ("ignore" , FutureWarning )
1144
+ result = to_datetime ([1 , '1' ], errors = 'ignore' , cache = cache ,
1145
+ box = box )
1146
+
1147
+ expected = klass (np .array ([1 , '1' ], dtype = 'O' ))
1148
+ tm .assert_equal (result , expected )
1149
+ pytest .raises (TypeError , to_datetime , [1 , '1' ], errors = 'raise' ,
1150
+ cache = cache , box = box )
1126
1151
1127
1152
def test_to_datetime_other_datetime64_units (self ):
1128
1153
# 5/25/2012
0 commit comments