Skip to content

Commit 90018c9

Browse files
author
Chris Bertinato
committed
Changed catch_warnings to assertions
1 parent 899228a commit 90018c9

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
@@ -3149,7 +3149,7 @@ def converter(*date_cols):
31493149
dayfirst=dayfirst,
31503150
errors='ignore',
31513151
infer_datetime_format=infer_datetime_format
3152-
).to_pydatetime()
3152+
)
31533153
except ValueError:
31543154
return tools.to_datetime(
31553155
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
@@ -20,12 +19,12 @@ def conv(v):
2019

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)