Skip to content

Commit cbcc31b

Browse files
author
Chris Bertinato
committed
Changed catch_warnings to assertions
1 parent ddfcc1e commit cbcc31b

File tree

3 files changed

+25
-34
lines changed

3 files changed

+25
-34
lines changed

pandas/io/parsers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3130,7 +3130,7 @@ def converter(*date_cols):
31303130
dayfirst=dayfirst,
31313131
errors='ignore',
31323132
infer_datetime_format=infer_datetime_format
3133-
).to_pydatetime()
3133+
)
31343134
except ValueError:
31353135
return tools.to_datetime(
31363136
parsing.try_parse_dates(strs, dayfirst=dayfirst))

pandas/tests/indexes/datetimes/test_tools.py

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

98
import dateutil
109
from dateutil.parser import parse
@@ -222,9 +221,7 @@ def test_to_datetime_format_weeks(self, cache):
222221
def test_to_datetime_parse_tzname_or_tzoffset(self, box, const,
223222
fmt, dates, expected_dates):
224223
# GH 13486
225-
with catch_warnings():
226-
simplefilter("ignore", FutureWarning)
227-
224+
with tm.assert_produces_warning(FutureWarning):
228225
result = pd.to_datetime(dates, format=fmt, box=box)
229226
expected = const(expected_dates)
230227
tm.assert_equal(result, expected)
@@ -260,11 +257,8 @@ def test_to_datetime_dtarr(self, tz):
260257
result = to_datetime(arr)
261258
assert result is arr
262259

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
268262

269263
def test_to_datetime_pydatetime(self):
270264
actual = pd.to_datetime(datetime(2008, 1, 15))
@@ -364,8 +358,7 @@ def test_to_datetime_dt64s(self, cache):
364358
def test_to_datetime_array_of_dt64s(self, cache):
365359
dts = [np.datetime64('2000-01-01'), np.datetime64('2000-01-02'), ]
366360

367-
with catch_warnings():
368-
simplefilter("ignore", FutureWarning)
361+
with tm.assert_produces_warning(FutureWarning):
369362

370363
# Assuming all datetimes are in bounds, to_datetime() returns
371364
# an array that is equal to Timestamp() parsing
@@ -380,6 +373,7 @@ def test_to_datetime_array_of_dt64s(self, cache):
380373
pytest.raises(ValueError, pd.to_datetime, dts_with_oob,
381374
errors='raise')
382375

376+
with tm.assert_produces_warning(FutureWarning):
383377
tm.assert_numpy_array_equal(
384378
pd.to_datetime(dts_with_oob, box=False, errors='coerce',
385379
cache=cache),
@@ -393,6 +387,7 @@ def test_to_datetime_array_of_dt64s(self, cache):
393387
)
394388
)
395389

390+
with tm.assert_produces_warning(FutureWarning):
396391
# With errors='ignore', out of bounds datetime64s
397392
# are converted to their .item(), which depending on the version of
398393
# numpy is either a python datetime.datetime or datetime.date
@@ -574,11 +569,11 @@ def test_to_datetime_cache(self, utc, format, box, constructor):
574569
test_dates = [date] * 10**5
575570
data = constructor(test_dates)
576571

577-
with catch_warnings():
578-
simplefilter("ignore", FutureWarning)
579-
572+
with tm.assert_produces_warning(FutureWarning):
580573
result = pd.to_datetime(data, utc=utc, format=format, box=box,
581574
cache=True)
575+
576+
with tm.assert_produces_warning(FutureWarning):
582577
expected = pd.to_datetime(data, utc=utc, format=format, box=box,
583578
cache=False)
584579
if box:
@@ -634,8 +629,7 @@ def test_iso_8601_strings_same_offset_no_box(self):
634629
# GH 22446
635630
data = ['2018-01-04 09:01:00+09:00', '2018-01-04 09:02:00+09:00']
636631

637-
with catch_warnings():
638-
simplefilter("ignore", FutureWarning)
632+
with tm.assert_produces_warning(FutureWarning):
639633
result = pd.to_datetime(data, box=False)
640634

641635
expected = np.array([
@@ -993,8 +987,7 @@ def test_dataframe_box_false(self):
993987
'month': [2, 3],
994988
'day': [4, 5]})
995989

996-
with catch_warnings():
997-
simplefilter("ignore", FutureWarning)
990+
with tm.assert_produces_warning(FutureWarning):
998991
result = pd.to_datetime(df, box=False)
999992

1000993
expected = np.array(['2015-02-04', '2016-03-05'],
@@ -1139,13 +1132,14 @@ def test_to_datetime_types(self, cache):
11391132
def test_to_datetime_unprocessable_input(self, cache, box, klass):
11401133
# GH 4928
11411134
# GH 21864
1142-
with catch_warnings():
1143-
simplefilter("ignore", FutureWarning)
1135+
with tm.assert_produces_warning(FutureWarning):
11441136
result = to_datetime([1, '1'], errors='ignore', cache=cache,
11451137
box=box)
11461138

11471139
expected = klass(np.array([1, '1'], dtype='O'))
11481140
tm.assert_equal(result, expected)
1141+
1142+
with tm.assert_produces_warning(FutureWarning):
11491143
pytest.raises(TypeError, to_datetime, [1, '1'], errors='raise',
11501144
cache=cache, box=box)
11511145

pandas/tests/indexes/timedeltas/test_tools.py

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

43
import numpy as np
54
import pytest
@@ -19,12 +18,12 @@ def conv(v):
1918

2019
d1 = np.timedelta64(1, 'D')
2120

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

@@ -40,9 +39,7 @@ def conv(v):
4039
expected = pd.Index(np.array([np.timedelta64(1, 's')]))
4140
tm.assert_index_equal(result, expected)
4241

43-
with catch_warnings():
44-
simplefilter("ignore", FutureWarning)
45-
42+
with tm.assert_produces_warning(FutureWarning):
4643
# ints
4744
result = np.timedelta64(0, 'ns')
4845
expected = to_timedelta(0, box=False)
@@ -59,15 +56,14 @@ def conv(v):
5956
expected = to_timedelta([0, 10], unit='s')
6057
tm.assert_index_equal(result, expected)
6158

62-
with catch_warnings():
63-
simplefilter("ignore", FutureWarning)
64-
59+
with tm.assert_produces_warning(FutureWarning):
6560
# single element conversion
6661
v = timedelta(seconds=1)
6762
result = to_timedelta(v, box=False)
6863
expected = np.timedelta64(timedelta(seconds=1))
6964
assert result == expected
7065

66+
with tm.assert_produces_warning(FutureWarning):
7167
v = np.timedelta64(timedelta(seconds=1))
7268
result = to_timedelta(v, box=False)
7369
expected = np.timedelta64(timedelta(seconds=1))
@@ -99,20 +95,21 @@ def conv(v):
9995
expected = TimedeltaIndex([np.timedelta64(1, 'D')] * 5)
10096
tm.assert_index_equal(result, expected)
10197

102-
with catch_warnings():
103-
simplefilter("ignore", FutureWarning)
104-
98+
with tm.assert_produces_warning(FutureWarning):
10599
# Test with lists as input when box=false
106100
expected = np.array(np.arange(3) * 1000000000, dtype='timedelta64[ns]')
107101
result = to_timedelta(range(3), unit='s', box=False)
108102
tm.assert_numpy_array_equal(expected, result)
109103

104+
with tm.assert_produces_warning(FutureWarning):
110105
result = to_timedelta(np.arange(3), unit='s', box=False)
111106
tm.assert_numpy_array_equal(expected, result)
112107

108+
with tm.assert_produces_warning(FutureWarning):
113109
result = to_timedelta([0, 1, 2], unit='s', box=False)
114110
tm.assert_numpy_array_equal(expected, result)
115111

112+
with tm.assert_produces_warning(FutureWarning):
116113
# Tests with fractional seconds as input:
117114
expected = np.array(
118115
[0, 500000000, 800000000, 1200000000], dtype='timedelta64[ns]')

0 commit comments

Comments
 (0)