Skip to content

Commit 6499a67

Browse files
author
Chris Bertinato
committed
Amend tests to suppress warnings from deprecation
1 parent 4056059 commit 6499a67

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
@@ -19,15 +20,18 @@ def conv(v):
1920

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

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

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

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

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

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

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

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

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

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

98-
result = to_timedelta(np.arange(3), unit='s', box=False)
99-
tm.assert_numpy_array_equal(expected, result)
111+
result = to_timedelta(np.arange(3), unit='s', box=False)
112+
tm.assert_numpy_array_equal(expected, result)
100113

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

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

110123
def test_to_timedelta_invalid(self):
111124

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
@@ -309,8 +310,11 @@ def test_iso_conversion(self):
309310
assert to_timedelta('P0DT0H0M1S') == expected
310311

311312
def test_nat_converters(self):
312-
assert to_timedelta('nat', box=False).astype('int64') == iNaT
313-
assert to_timedelta('nan', box=False).astype('int64') == iNaT
313+
with catch_warnings():
314+
simplefilter("ignore", FutureWarning)
315+
316+
assert to_timedelta('nat', box=False).astype('int64') == iNaT
317+
assert to_timedelta('nan', box=False).astype('int64') == iNaT
314318

315319
@pytest.mark.parametrize('units, np_unit',
316320
[(['Y', 'y'], 'Y'),

0 commit comments

Comments
 (0)