Skip to content

Commit 7ce9722

Browse files
committed
TST: added tests
1 parent 56175ea commit 7ce9722

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

pandas/tests/tools/test_to_datetime.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,60 @@ def test_dayfirst(self, cache):
17521752
tm.assert_index_equal(expected, idx5)
17531753
tm.assert_index_equal(expected, idx6)
17541754

1755+
def test_dayfirst_warnings(self):
1756+
# GH 12585
1757+
1758+
# CASE 1: valid input
1759+
arr = ["31/12/2014", "10/03/2011"]
1760+
expected = DatetimeIndex(
1761+
["2014-12-31", "2011-03-10"], dtype="datetime64[ns]", freq=None
1762+
)
1763+
1764+
# A. dayfirst arg correct, no warning
1765+
res1 = to_datetime(arr, dayfirst=True)
1766+
tm.assert_index_equal(expected, res1)
1767+
1768+
# B. dayfirst arg incorrect, warning + incorrect output
1769+
msg = r"Parsing '31/12/2014' in DD/MM/YYYY format."
1770+
with pytest.warns(UserWarning, match=msg):
1771+
res2 = to_datetime(arr, dayfirst=False)
1772+
with pytest.raises(AssertionError):
1773+
tm.assert_index_equal(expected, res2)
1774+
1775+
# C. dayfirst default arg, same as B
1776+
msg = r"Parsing '31/12/2014' in DD/MM/YYYY format."
1777+
with pytest.warns(UserWarning, match=msg):
1778+
res3 = to_datetime(arr, dayfirst=False)
1779+
with pytest.raises(AssertionError):
1780+
tm.assert_index_equal(expected, res3)
1781+
1782+
# D. infer_datetime_format=True overrides dayfirst default
1783+
# no warning + correct result
1784+
res4 = to_datetime(arr, infer_datetime_format=True)
1785+
tm.assert_index_equal(expected, res4)
1786+
1787+
# CASE 2: invalid input
1788+
# cannot consistently process with single format
1789+
# warnings *always* raised
1790+
1791+
arr = ["31/12/2014", "03/30/2011"]
1792+
1793+
msg = r"Parsing '03/30/2011' in MM/DD/YYYY format."
1794+
with pytest.warns(UserWarning, match=msg):
1795+
to_datetime(arr, dayfirst=True)
1796+
1797+
msg = r"Parsing '31/12/2014' in DD/MM/YYYY format."
1798+
with pytest.warns(UserWarning, match=msg):
1799+
to_datetime(arr, dayfirst=False)
1800+
1801+
msg = r"Parsing '31/12/2014' in DD/MM/YYYY format."
1802+
with pytest.warns(UserWarning, match=msg):
1803+
to_datetime(arr)
1804+
1805+
msg = r"Parsing '31/12/2014' in DD/MM/YYYY format."
1806+
with pytest.warns(UserWarning, match=msg):
1807+
to_datetime(arr, infer_datetime_format=True)
1808+
17551809
@pytest.mark.parametrize("klass", [DatetimeIndex, DatetimeArray])
17561810
def test_to_datetime_dta_tz(self, klass):
17571811
# GH#27733

0 commit comments

Comments
 (0)