Skip to content

Commit a4f3d97

Browse files
author
Chris Bertinato
committed
Amend tests to suppress warnings from deprecation
1 parent 2fbe070 commit a4f3d97

File tree

3 files changed

+132
-90
lines changed

3 files changed

+132
-90
lines changed

pandas/tests/indexes/datetimes/test_tools.py

Lines changed: 79 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datetime import datetime, time
55
from distutils.version import LooseVersion
66
import locale
7+
from warnings import catch_warnings, simplefilter
78

89
import dateutil
910
from dateutil.parser import parse
@@ -221,12 +222,15 @@ def test_to_datetime_format_weeks(self, cache):
221222
def test_to_datetime_parse_tzname_or_tzoffset(self, box, const,
222223
fmt, dates, expected_dates):
223224
# 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)
227227

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)
230234

231235
@pytest.mark.parametrize('offset', [
232236
'+0', '-1foo', 'UTCbar', ':10', '+01:000:01', ''])
@@ -256,8 +260,11 @@ def test_to_datetime_dtarr(self, tz):
256260
result = to_datetime(arr)
257261
assert result is arr
258262

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
261268

262269
def test_to_datetime_pydatetime(self):
263270
actual = pd.to_datetime(datetime(2008, 1, 15))
@@ -357,43 +364,46 @@ def test_to_datetime_dt64s(self, cache):
357364
def test_to_datetime_array_of_dt64s(self, cache):
358365
dts = [np.datetime64('2000-01-01'), np.datetime64('2000-01-02'), ]
359366

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)
369369

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+
)
372376

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+
)
383394
)
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+
)
395406
)
396-
)
397407

398408
@pytest.mark.parametrize('cache', [True, False])
399409
def test_to_datetime_tz(self, cache):
@@ -563,10 +573,14 @@ def test_to_datetime_cache(self, utc, format, box, constructor):
563573
date = '20130101 00:00:00'
564574
test_dates = [date] * 10**5
565575
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)
570584
if box:
571585
tm.assert_index_equal(result, expected)
572586
else:
@@ -619,7 +633,11 @@ def test_iso_8601_strings_with_same_offset(self):
619633
def test_iso_8601_strings_same_offset_no_box(self):
620634
# GH 22446
621635
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+
623641
expected = np.array([
624642
datetime(2018, 1, 4, 9, 1, tzinfo=pytz.FixedOffset(540)),
625643
datetime(2018, 1, 4, 9, 2, tzinfo=pytz.FixedOffset(540))
@@ -813,7 +831,7 @@ def test_unit_rounding(self, cache):
813831
def test_unit_ignore_keeps_name(self, cache):
814832
# GH 21697
815833
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',
817835
cache=cache)
818836
tm.assert_index_equal(result, expected)
819837

@@ -974,7 +992,11 @@ def test_dataframe_box_false(self):
974992
df = pd.DataFrame({'year': [2015, 2016],
975993
'month': [2, 3],
976994
'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+
9781000
expected = np.array(['2015-02-04', '2016-03-05'],
9791001
dtype='datetime64[ns]')
9801002
tm.assert_numpy_array_equal(result, expected)
@@ -991,8 +1013,7 @@ def test_dataframe_utc_true(self):
9911013

9921014
def test_to_datetime_errors_ignore_utc_true(self):
9931015
# 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')
9961017
expected = DatetimeIndex(['1970-01-01 00:00:01'], tz='UTC')
9971018
tm.assert_index_equal(result, expected)
9981019

@@ -1118,11 +1139,15 @@ def test_to_datetime_types(self, cache):
11181139
def test_to_datetime_unprocessable_input(self, cache, box, klass):
11191140
# GH 4928
11201141
# 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)
11261151

11271152
def test_to_datetime_other_datetime64_units(self):
11281153
# 5/25/2012

pandas/tests/indexes/timedeltas/test_tools.py

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import time, timedelta
2+
from warnings import catch_warnings, simplefilter
23

34
import numpy as np
45
import pytest
@@ -18,15 +19,18 @@ def conv(v):
1819

1920
d1 = np.timedelta64(1, 'D')
2021

21-
assert (to_timedelta('1 days 06:05:01.00003', box=False) ==
22-
conv(d1 + np.timedelta64(6 * 3600 + 5 * 60 + 1, 's') +
23-
np.timedelta64(30, 'us')))
24-
assert (to_timedelta('15.5us', box=False) ==
25-
conv(np.timedelta64(15500, 'ns')))
22+
with catch_warnings():
23+
simplefilter("ignore", FutureWarning)
2624

27-
# empty string
28-
result = to_timedelta('', box=False)
29-
assert result.astype('int64') == iNaT
25+
assert (to_timedelta('1 days 06:05:01.00003', box=False) ==
26+
conv(d1 + np.timedelta64(6 * 3600 + 5 * 60 + 1, 's') +
27+
np.timedelta64(30, 'us')))
28+
assert (to_timedelta('15.5us', box=False) ==
29+
conv(np.timedelta64(15500, 'ns')))
30+
31+
# empty string
32+
result = to_timedelta('', box=False)
33+
assert result.astype('int64') == iNaT
3034

3135
result = to_timedelta(['', ''])
3236
assert isna(result).all()
@@ -36,10 +40,13 @@ def conv(v):
3640
expected = pd.Index(np.array([np.timedelta64(1, 's')]))
3741
tm.assert_index_equal(result, expected)
3842

39-
# ints
40-
result = np.timedelta64(0, 'ns')
41-
expected = to_timedelta(0, box=False)
42-
assert result == expected
43+
with catch_warnings():
44+
simplefilter("ignore", FutureWarning)
45+
46+
# ints
47+
result = np.timedelta64(0, 'ns')
48+
expected = to_timedelta(0, box=False)
49+
assert result == expected
4350

4451
# Series
4552
expected = Series([timedelta(days=1), timedelta(days=1, seconds=1)])
@@ -52,16 +59,19 @@ def conv(v):
5259
expected = to_timedelta([0, 10], unit='s')
5360
tm.assert_index_equal(result, expected)
5461

55-
# single element conversion
56-
v = timedelta(seconds=1)
57-
result = to_timedelta(v, box=False)
58-
expected = np.timedelta64(timedelta(seconds=1))
59-
assert result == expected
62+
with catch_warnings():
63+
simplefilter("ignore", FutureWarning)
6064

61-
v = np.timedelta64(timedelta(seconds=1))
62-
result = to_timedelta(v, box=False)
63-
expected = np.timedelta64(timedelta(seconds=1))
64-
assert result == expected
65+
# single element conversion
66+
v = timedelta(seconds=1)
67+
result = to_timedelta(v, box=False)
68+
expected = np.timedelta64(timedelta(seconds=1))
69+
assert result == expected
70+
71+
v = np.timedelta64(timedelta(seconds=1))
72+
result = to_timedelta(v, box=False)
73+
expected = np.timedelta64(timedelta(seconds=1))
74+
assert result == expected
6575

6676
# arrays of various dtypes
6777
arr = np.array([1] * 5, dtype='int64')
@@ -89,22 +99,25 @@ def conv(v):
8999
expected = TimedeltaIndex([np.timedelta64(1, 'D')] * 5)
90100
tm.assert_index_equal(result, expected)
91101

92-
# Test with lists as input when box=false
93-
expected = np.array(np.arange(3) * 1000000000, dtype='timedelta64[ns]')
94-
result = to_timedelta(range(3), unit='s', box=False)
95-
tm.assert_numpy_array_equal(expected, result)
102+
with catch_warnings():
103+
simplefilter("ignore", FutureWarning)
104+
105+
# Test with lists as input when box=false
106+
expected = np.array(np.arange(3) * 1000000000, dtype='timedelta64[ns]')
107+
result = to_timedelta(range(3), unit='s', box=False)
108+
tm.assert_numpy_array_equal(expected, result)
96109

97-
result = to_timedelta(np.arange(3), unit='s', box=False)
98-
tm.assert_numpy_array_equal(expected, result)
110+
result = to_timedelta(np.arange(3), unit='s', box=False)
111+
tm.assert_numpy_array_equal(expected, result)
99112

100-
result = to_timedelta([0, 1, 2], unit='s', box=False)
101-
tm.assert_numpy_array_equal(expected, result)
113+
result = to_timedelta([0, 1, 2], unit='s', box=False)
114+
tm.assert_numpy_array_equal(expected, result)
102115

103-
# Tests with fractional seconds as input:
104-
expected = np.array(
105-
[0, 500000000, 800000000, 1200000000], dtype='timedelta64[ns]')
106-
result = to_timedelta([0., 0.5, 0.8, 1.2], unit='s', box=False)
107-
tm.assert_numpy_array_equal(expected, result)
116+
# Tests with fractional seconds as input:
117+
expected = np.array(
118+
[0, 500000000, 800000000, 1200000000], dtype='timedelta64[ns]')
119+
result = to_timedelta([0., 0.5, 0.8, 1.2], unit='s', box=False)
120+
tm.assert_numpy_array_equal(expected, result)
108121

109122
def test_to_timedelta_invalid(self):
110123

pandas/tests/scalar/timedelta/test_timedelta.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" test the scalar Timedelta """
22
from datetime import timedelta
3+
from warnings import catch_warnings, simplefilter
34

45
import numpy as np
56
import pytest
@@ -289,8 +290,11 @@ def test_iso_conversion(self):
289290
assert to_timedelta('P0DT0H0M1S') == expected
290291

291292
def test_nat_converters(self):
292-
assert to_timedelta('nat', box=False).astype('int64') == iNaT
293-
assert to_timedelta('nan', box=False).astype('int64') == iNaT
293+
with catch_warnings():
294+
simplefilter("ignore", FutureWarning)
295+
296+
assert to_timedelta('nat', box=False).astype('int64') == iNaT
297+
assert to_timedelta('nan', box=False).astype('int64') == iNaT
294298

295299
@pytest.mark.parametrize('units, np_unit',
296300
[(['Y', 'y'], 'Y'),

0 commit comments

Comments
 (0)