Skip to content

Commit fd04fa5

Browse files
krajatclKevin D Smith
authored and
Kevin D Smith
committed
TST: insert 'match' to bare pytest raises in pandas/tests/tools/test_to_datetime.py (pandas-dev#37027)
1 parent 42096d1 commit fd04fa5

File tree

1 file changed

+55
-28
lines changed

1 file changed

+55
-28
lines changed

pandas/tests/tools/test_to_datetime.py

+55-28
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,9 @@ def test_to_datetime_parse_tzname_or_tzoffset_different_tz_to_utc(self):
349349
def test_to_datetime_parse_timezone_malformed(self, offset):
350350
fmt = "%Y-%m-%d %H:%M:%S %z"
351351
date = "2010-01-01 12:00:00 " + offset
352-
with pytest.raises(ValueError):
352+
353+
msg = "does not match format|unconverted data remains"
354+
with pytest.raises(ValueError, match=msg):
353355
pd.to_datetime([date], format=fmt)
354356

355357
def test_to_datetime_parse_timezone_keeps_name(self):
@@ -784,17 +786,19 @@ def test_to_datetime_tz_psycopg2(self, cache):
784786
@pytest.mark.parametrize("cache", [True, False])
785787
def test_datetime_bool(self, cache):
786788
# GH13176
787-
with pytest.raises(TypeError):
789+
msg = r"dtype bool cannot be converted to datetime64\[ns\]"
790+
with pytest.raises(TypeError, match=msg):
788791
to_datetime(False)
789792
assert to_datetime(False, errors="coerce", cache=cache) is NaT
790793
assert to_datetime(False, errors="ignore", cache=cache) is False
791-
with pytest.raises(TypeError):
794+
with pytest.raises(TypeError, match=msg):
792795
to_datetime(True)
793796
assert to_datetime(True, errors="coerce", cache=cache) is NaT
794797
assert to_datetime(True, errors="ignore", cache=cache) is True
795-
with pytest.raises(TypeError):
798+
msg = f"{type(cache)} is not convertible to datetime"
799+
with pytest.raises(TypeError, match=msg):
796800
to_datetime([False, datetime.today()], cache=cache)
797-
with pytest.raises(TypeError):
801+
with pytest.raises(TypeError, match=msg):
798802
to_datetime(["20130101", True], cache=cache)
799803
tm.assert_index_equal(
800804
to_datetime([0, False, NaT, 0.0], errors="coerce", cache=cache),
@@ -805,10 +809,10 @@ def test_datetime_bool(self, cache):
805809

806810
def test_datetime_invalid_datatype(self):
807811
# GH13176
808-
809-
with pytest.raises(TypeError):
812+
msg = "is not convertible to datetime"
813+
with pytest.raises(TypeError, match=msg):
810814
pd.to_datetime(bool)
811-
with pytest.raises(TypeError):
815+
with pytest.raises(TypeError, match=msg):
812816
pd.to_datetime(pd.to_datetime)
813817

814818
@pytest.mark.parametrize("value", ["a", "00:01:99"])
@@ -826,7 +830,12 @@ def test_datetime_invalid_scalar(self, value, format, infer):
826830
)
827831
assert res is pd.NaT
828832

829-
with pytest.raises(ValueError):
833+
msg = (
834+
"is a bad directive in format|"
835+
"second must be in 0..59: 00:01:99|"
836+
"Given date string not likely a datetime"
837+
)
838+
with pytest.raises(ValueError, match=msg):
830839
pd.to_datetime(
831840
value, errors="raise", format=format, infer_datetime_format=infer
832841
)
@@ -847,12 +856,14 @@ def test_datetime_outofbounds_scalar(self, value, format, infer):
847856
assert res is pd.NaT
848857

849858
if format is not None:
850-
with pytest.raises(ValueError):
859+
msg = "is a bad directive in format|Out of bounds nanosecond timestamp"
860+
with pytest.raises(ValueError, match=msg):
851861
pd.to_datetime(
852862
value, errors="raise", format=format, infer_datetime_format=infer
853863
)
854864
else:
855-
with pytest.raises(OutOfBoundsDatetime):
865+
msg = "Out of bounds nanosecond timestamp"
866+
with pytest.raises(OutOfBoundsDatetime, match=msg):
856867
pd.to_datetime(
857868
value, errors="raise", format=format, infer_datetime_format=infer
858869
)
@@ -872,7 +883,12 @@ def test_datetime_invalid_index(self, values, format, infer):
872883
)
873884
tm.assert_index_equal(res, pd.DatetimeIndex([pd.NaT] * len(values)))
874885

875-
with pytest.raises(ValueError):
886+
msg = (
887+
"is a bad directive in format|"
888+
"Given date string not likely a datetime|"
889+
"second must be in 0..59: 00:01:99"
890+
)
891+
with pytest.raises(ValueError, match=msg):
876892
pd.to_datetime(
877893
values, errors="raise", format=format, infer_datetime_format=infer
878894
)
@@ -1070,7 +1086,8 @@ def test_timestamp_utc_true(self, ts, expected):
10701086
@pytest.mark.parametrize("dt_str", ["00010101", "13000101", "30000101", "99990101"])
10711087
def test_to_datetime_with_format_out_of_bounds(self, dt_str):
10721088
# GH 9107
1073-
with pytest.raises(OutOfBoundsDatetime):
1089+
msg = "Out of bounds nanosecond timestamp"
1090+
with pytest.raises(OutOfBoundsDatetime, match=msg):
10741091
pd.to_datetime(dt_str, format="%Y%m%d")
10751092

10761093
def test_to_datetime_utc(self):
@@ -1096,8 +1113,8 @@ class TestToDatetimeUnit:
10961113
def test_unit(self, cache):
10971114
# GH 11758
10981115
# test proper behavior with errors
1099-
1100-
with pytest.raises(ValueError):
1116+
msg = "cannot specify both format and unit"
1117+
with pytest.raises(ValueError, match=msg):
11011118
to_datetime([1], unit="D", format="%Y%m%d", cache=cache)
11021119

11031120
values = [11111111, 1, 1.0, iNaT, NaT, np.nan, "NaT", ""]
@@ -1123,7 +1140,8 @@ def test_unit(self, cache):
11231140
)
11241141
tm.assert_index_equal(result, expected)
11251142

1126-
with pytest.raises(tslib.OutOfBoundsDatetime):
1143+
msg = "cannot convert input 11111111 with the unit 'D'"
1144+
with pytest.raises(tslib.OutOfBoundsDatetime, match=msg):
11271145
to_datetime(values, unit="D", errors="raise", cache=cache)
11281146

11291147
values = [1420043460000, iNaT, NaT, np.nan, "NaT"]
@@ -1136,7 +1154,8 @@ def test_unit(self, cache):
11361154
expected = DatetimeIndex(["NaT", "NaT", "NaT", "NaT", "NaT"])
11371155
tm.assert_index_equal(result, expected)
11381156

1139-
with pytest.raises(tslib.OutOfBoundsDatetime):
1157+
msg = "cannot convert input 1420043460000 with the unit 's'"
1158+
with pytest.raises(tslib.OutOfBoundsDatetime, match=msg):
11401159
to_datetime(values, errors="raise", unit="s", cache=cache)
11411160

11421161
# if we have a string, then we raise a ValueError
@@ -1204,15 +1223,16 @@ def test_unit_mixed(self, cache):
12041223
result = pd.to_datetime(arr, errors="coerce", cache=cache)
12051224
tm.assert_index_equal(result, expected)
12061225

1207-
with pytest.raises(ValueError):
1226+
msg = "mixed datetimes and integers in passed array"
1227+
with pytest.raises(ValueError, match=msg):
12081228
pd.to_datetime(arr, errors="raise", cache=cache)
12091229

12101230
expected = DatetimeIndex(["NaT", "NaT", "2013-01-01"])
12111231
arr = [1.434692e18, 1.432766e18, pd.Timestamp("20130101")]
12121232
result = pd.to_datetime(arr, errors="coerce", cache=cache)
12131233
tm.assert_index_equal(result, expected)
12141234

1215-
with pytest.raises(ValueError):
1235+
with pytest.raises(ValueError, match=msg):
12161236
pd.to_datetime(arr, errors="raise", cache=cache)
12171237

12181238
@pytest.mark.parametrize("cache", [True, False])
@@ -1392,7 +1412,8 @@ def test_dataframe_dtypes(self, cache):
13921412

13931413
# float
13941414
df = DataFrame({"year": [2000, 2001], "month": [1.5, 1], "day": [1, 1]})
1395-
with pytest.raises(ValueError):
1415+
msg = "cannot assemble the datetimes: unconverted data remains: 1"
1416+
with pytest.raises(ValueError, match=msg):
13961417
to_datetime(df, cache=cache)
13971418

13981419
def test_dataframe_utc_true(self):
@@ -1500,7 +1521,8 @@ def test_to_datetime_barely_out_of_bounds(self):
15001521
# in an in-bounds datetime
15011522
arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object)
15021523

1503-
with pytest.raises(OutOfBoundsDatetime):
1524+
msg = "Out of bounds nanosecond timestamp"
1525+
with pytest.raises(OutOfBoundsDatetime, match=msg):
15041526
to_datetime(arr)
15051527

15061528
@pytest.mark.parametrize("cache", [True, False])
@@ -1638,7 +1660,8 @@ def test_to_datetime_overflow(self):
16381660
# gh-17637
16391661
# we are overflowing Timedelta range here
16401662

1641-
with pytest.raises(OverflowError):
1663+
msg = "Python int too large to convert to C long"
1664+
with pytest.raises(OverflowError, match=msg):
16421665
date_range(start="1/1/1700", freq="B", periods=100000)
16431666

16441667
@pytest.mark.parametrize("cache", [True, False])
@@ -2265,23 +2288,26 @@ def test_julian_round_trip(self):
22652288
assert result.to_julian_date() == 2456658
22662289

22672290
# out-of-bounds
2268-
with pytest.raises(ValueError):
2291+
msg = "1 is Out of Bounds for origin='julian'"
2292+
with pytest.raises(ValueError, match=msg):
22692293
pd.to_datetime(1, origin="julian", unit="D")
22702294

22712295
def test_invalid_unit(self, units, julian_dates):
22722296

22732297
# checking for invalid combination of origin='julian' and unit != D
22742298
if units != "D":
2275-
with pytest.raises(ValueError):
2299+
msg = "unit must be 'D' for origin='julian'"
2300+
with pytest.raises(ValueError, match=msg):
22762301
pd.to_datetime(julian_dates, unit=units, origin="julian")
22772302

22782303
def test_invalid_origin(self):
22792304

22802305
# need to have a numeric specified
2281-
with pytest.raises(ValueError):
2306+
msg = "it must be numeric with a unit specified"
2307+
with pytest.raises(ValueError, match=msg):
22822308
pd.to_datetime("2005-01-01", origin="1960-01-01")
22832309

2284-
with pytest.raises(ValueError):
2310+
with pytest.raises(ValueError, match=msg):
22852311
pd.to_datetime("2005-01-01", origin="1960-01-01", unit="D")
22862312

22872313
def test_epoch(self, units, epochs, epoch_1960, units_from_epochs):
@@ -2304,12 +2330,13 @@ def test_epoch(self, units, epochs, epoch_1960, units_from_epochs):
23042330
)
23052331
def test_invalid_origins(self, origin, exc, units, units_from_epochs):
23062332

2307-
with pytest.raises(exc):
2333+
msg = f"origin {origin} (is Out of Bounds|cannot be converted to a Timestamp)"
2334+
with pytest.raises(exc, match=msg):
23082335
pd.to_datetime(units_from_epochs, unit=units, origin=origin)
23092336

23102337
def test_invalid_origins_tzinfo(self):
23112338
# GH16842
2312-
with pytest.raises(ValueError):
2339+
with pytest.raises(ValueError, match="must be tz-naive"):
23132340
pd.to_datetime(1, unit="D", origin=datetime(2000, 1, 1, tzinfo=pytz.utc))
23142341

23152342
@pytest.mark.parametrize("format", [None, "%Y-%m-%d %H:%M:%S"])

0 commit comments

Comments
 (0)