@@ -1841,6 +1841,14 @@ def test_dayfirst(self, cache):
1841
1841
1842
1842
def test_dayfirst_warnings (self ):
1843
1843
# 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
+ )
1844
1852
1845
1853
# CASE 1: valid input
1846
1854
arr = ["31/12/2014" , "10/03/2011" ]
@@ -1853,13 +1861,19 @@ def test_dayfirst_warnings(self):
1853
1861
tm .assert_index_equal (expected , res1 )
1854
1862
1855
1863
# 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
+ ):
1858
1869
tm .assert_index_equal (expected , res2 )
1859
1870
1860
1871
# 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
+ ):
1863
1877
tm .assert_index_equal (expected , res3 )
1864
1878
1865
1879
# D. infer_datetime_format=True overrides dayfirst default
@@ -1878,19 +1892,23 @@ def test_dayfirst_warnings(self):
1878
1892
)
1879
1893
1880
1894
# 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 )
1882
1897
tm .assert_index_equal (expected , res5 )
1883
1898
1884
1899
# 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 )
1886
1902
tm .assert_index_equal (expected , res6 )
1887
1903
1888
1904
# 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 )
1890
1907
tm .assert_index_equal (expected , res7 )
1891
1908
1892
1909
# 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 )
1894
1912
tm .assert_index_equal (expected , res8 )
1895
1913
1896
1914
@pytest .mark .parametrize ("klass" , [DatetimeIndex , DatetimeArray ])
0 commit comments