@@ -1839,6 +1839,60 @@ def test_dayfirst(self, cache):
1839
1839
tm .assert_index_equal (expected , idx5 )
1840
1840
tm .assert_index_equal (expected , idx6 )
1841
1841
1842
+ def test_dayfirst_warnings (self ):
1843
+ # GH 12585
1844
+
1845
+ # CASE 1: valid input
1846
+ arr = ["31/12/2014" , "10/03/2011" ]
1847
+ expected = DatetimeIndex (
1848
+ ["2014-12-31" , "2011-03-10" ], dtype = "datetime64[ns]" , freq = None
1849
+ )
1850
+
1851
+ # A. dayfirst arg correct, no warning
1852
+ res1 = to_datetime (arr , dayfirst = True )
1853
+ tm .assert_index_equal (expected , res1 )
1854
+
1855
+ # B. dayfirst arg incorrect, warning + incorrect output
1856
+ msg = r"Parsing '31/12/2014' in DD/MM/YYYY format."
1857
+ with pytest .warns (UserWarning , match = msg ):
1858
+ res2 = to_datetime (arr , dayfirst = False )
1859
+ with pytest .raises (AssertionError ):
1860
+ tm .assert_index_equal (expected , res2 )
1861
+
1862
+ # C. dayfirst default arg, same as B
1863
+ msg = r"Parsing '31/12/2014' in DD/MM/YYYY format."
1864
+ with pytest .warns (UserWarning , match = msg ):
1865
+ res3 = to_datetime (arr , dayfirst = False )
1866
+ with pytest .raises (AssertionError ):
1867
+ tm .assert_index_equal (expected , res3 )
1868
+
1869
+ # D. infer_datetime_format=True overrides dayfirst default
1870
+ # no warning + correct result
1871
+ res4 = to_datetime (arr , infer_datetime_format = True )
1872
+ tm .assert_index_equal (expected , res4 )
1873
+
1874
+ # CASE 2: invalid input
1875
+ # cannot consistently process with single format
1876
+ # warnings *always* raised
1877
+
1878
+ arr = ["31/12/2014" , "03/30/2011" ]
1879
+
1880
+ msg = r"Parsing '03/30/2011' in MM/DD/YYYY format."
1881
+ with pytest .warns (UserWarning , match = msg ):
1882
+ to_datetime (arr , dayfirst = True )
1883
+
1884
+ msg = r"Parsing '31/12/2014' in DD/MM/YYYY format."
1885
+ with pytest .warns (UserWarning , match = msg ):
1886
+ to_datetime (arr , dayfirst = False )
1887
+
1888
+ msg = r"Parsing '31/12/2014' in DD/MM/YYYY format."
1889
+ with pytest .warns (UserWarning , match = msg ):
1890
+ to_datetime (arr )
1891
+
1892
+ msg = r"Parsing '31/12/2014' in DD/MM/YYYY format."
1893
+ with pytest .warns (UserWarning , match = msg ):
1894
+ to_datetime (arr , infer_datetime_format = True )
1895
+
1842
1896
@pytest .mark .parametrize ("klass" , [DatetimeIndex , DatetimeArray ])
1843
1897
def test_to_datetime_dta_tz (self , klass ):
1844
1898
# GH#27733
0 commit comments