Skip to content

Commit f79d8f7

Browse files
committed
set stacklevel, assert warning messages
1 parent f939a82 commit f79d8f7

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

pandas/_libs/tslibs/parsing.pyx

+20-4
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,32 @@ cdef inline object _parse_delimited_date(str date_string, bint dayfirst):
180180
# in C api, thus we call faster C version for 3.6.1 or newer
181181

182182
if dayfirst and not swapped_day_and_month:
183-
warnings.warn(f"Parsing '{date_string}' in MM/DD/YYYY format.")
183+
warnings.warn(
184+
f"Parsing '{date_string}' in MM/DD/YYYY format. Provide format "
185+
"or specify infer_datetime_format=True for consistent parsing.",
186+
stacklevel=4,
187+
)
184188
elif not dayfirst and swapped_day_and_month:
185-
warnings.warn(f"Parsing '{date_string}' in DD/MM/YYYY format.")
189+
warnings.warn(
190+
f"Parsing '{date_string}' in DD/MM/YYYY format. Provide format "
191+
"or specify infer_datetime_format=True for consistent parsing.",
192+
stacklevel=4,
193+
)
186194

187195
return datetime_new(year, month, day, 0, 0, 0, 0, None), reso
188196

189197
if dayfirst and not swapped_day_and_month:
190-
warnings.warn(f"Parsing '{date_string}' in MM/DD/YYYY format.")
198+
warnings.warn(
199+
f"Parsing '{date_string}' in MM/DD/YYYY format. Provide format or "
200+
"specify infer_datetime_format=True for consistent parsing.",
201+
stacklevel=4,
202+
)
191203
elif not dayfirst and swapped_day_and_month:
192-
warnings.warn(f"Parsing '{date_string}' in DD/MM/YYYY format.")
204+
warnings.warn(
205+
f"Parsing '{date_string}' in DD/MM/YYYY format. Provide format or "
206+
"specify infer_datetime_format=True for consistent parsing.",
207+
stacklevel=4,
208+
)
193209

194210
return datetime(year, month, day, 0, 0, 0, 0, None), reso
195211

pandas/tests/tools/test_to_datetime.py

+26-8
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,14 @@ def test_dayfirst(self, cache):
18411841

18421842
def test_dayfirst_warnings(self):
18431843
# GH 12585
1844+
warning_msg_day_first = (
1845+
"Parsing '31/12/2014' in DD/MM/YYYY format. Provide "
1846+
"format or specify infer_datetime_format=True for consistent parsing."
1847+
)
1848+
warning_msg_month_first = (
1849+
"Parsing '03/30/2011' in MM/DD/YYYY format. Provide "
1850+
"format or specify infer_datetime_format=True for consistent parsing."
1851+
)
18441852

18451853
# CASE 1: valid input
18461854
arr = ["31/12/2014", "10/03/2011"]
@@ -1853,13 +1861,19 @@ def test_dayfirst_warnings(self):
18531861
tm.assert_index_equal(expected, res1)
18541862

18551863
# B. dayfirst arg incorrect, warning + incorrect output
1856-
res2 = to_datetime(arr, dayfirst=False)
1857-
with pytest.raises(AssertionError):
1864+
with tm.assert_produces_warning(UserWarning, match=warning_msg_day_first):
1865+
res2 = to_datetime(arr, dayfirst=False)
1866+
with pytest.raises(AssertionError, match=None), tm.assert_produces_warning(
1867+
UserWarning, match=warning_msg_day_first
1868+
):
18581869
tm.assert_index_equal(expected, res2)
18591870

18601871
# C. dayfirst default arg, same as B
1861-
res3 = to_datetime(arr, dayfirst=False)
1862-
with pytest.raises(AssertionError):
1872+
with tm.assert_produces_warning(UserWarning, match=warning_msg_day_first):
1873+
res3 = to_datetime(arr, dayfirst=False)
1874+
with pytest.raises(AssertionError, match=None), tm.assert_produces_warning(
1875+
UserWarning, match=warning_msg_day_first
1876+
):
18631877
tm.assert_index_equal(expected, res3)
18641878

18651879
# D. infer_datetime_format=True overrides dayfirst default
@@ -1878,19 +1892,23 @@ def test_dayfirst_warnings(self):
18781892
)
18791893

18801894
# A. use dayfirst=True
1881-
res5 = to_datetime(arr, dayfirst=True)
1895+
with tm.assert_produces_warning(UserWarning, match=warning_msg_month_first):
1896+
res5 = to_datetime(arr, dayfirst=True)
18821897
tm.assert_index_equal(expected, res5)
18831898

18841899
# B. use dayfirst=False
1885-
res6 = to_datetime(arr, dayfirst=False)
1900+
with tm.assert_produces_warning(UserWarning, match=warning_msg_day_first):
1901+
res6 = to_datetime(arr, dayfirst=False)
18861902
tm.assert_index_equal(expected, res6)
18871903

18881904
# C. use dayfirst default arg, same as B
1889-
res7 = to_datetime(arr, dayfirst=False)
1905+
with tm.assert_produces_warning(UserWarning, match=warning_msg_day_first):
1906+
res7 = to_datetime(arr, dayfirst=False)
18901907
tm.assert_index_equal(expected, res7)
18911908

18921909
# D. use infer_datetime_format=True
1893-
res8 = to_datetime(arr, infer_datetime_format=True)
1910+
with tm.assert_produces_warning(UserWarning, match=warning_msg_day_first):
1911+
res8 = to_datetime(arr, infer_datetime_format=True)
18941912
tm.assert_index_equal(expected, res8)
18951913

18961914
@pytest.mark.parametrize("klass", [DatetimeIndex, DatetimeArray])

0 commit comments

Comments
 (0)