@@ -1752,6 +1752,60 @@ def test_dayfirst(self, cache):
1752
1752
tm .assert_index_equal (expected , idx5 )
1753
1753
tm .assert_index_equal (expected , idx6 )
1754
1754
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
+
1755
1809
@pytest .mark .parametrize ("klass" , [DatetimeIndex , DatetimeArray ])
1756
1810
def test_to_datetime_dta_tz (self , klass ):
1757
1811
# GH#27733
0 commit comments