@@ -1666,9 +1666,9 @@ def test_parse_delimited_date_swap_no_warning(
1666
1666
@pytest .mark .parametrize (
1667
1667
"date_string,dayfirst,expected" ,
1668
1668
[
1669
- # %d/%m/%Y; month > 12 thus replacement
1669
+ # %d/%m/%Y; month > 12
1670
1670
("13/02/2019" , False , datetime (2019 , 2 , 13 )),
1671
- # %m/%d/%Y; day > 12 thus there will be no replacement
1671
+ # %m/%d/%Y; day > 12
1672
1672
("02/13/2019" , True , datetime (2019 , 2 , 13 )),
1673
1673
],
1674
1674
)
@@ -1677,7 +1677,10 @@ def test_parse_delimited_date_swap_with_warning(
1677
1677
):
1678
1678
parser = all_parsers
1679
1679
expected = DataFrame ({0 : [expected ]}, dtype = "datetime64[ns]" )
1680
- warning_msg = "Specify a format to ensure consistent parsing"
1680
+ warning_msg = (
1681
+ "Parsing dates in .* format when dayfirst=.* was specified. "
1682
+ "Pass `dayfirst=.*` or specify a format to silence this warning."
1683
+ )
1681
1684
result = parser .read_csv_check_warnings (
1682
1685
UserWarning ,
1683
1686
warning_msg ,
@@ -1691,13 +1694,11 @@ def test_parse_delimited_date_swap_with_warning(
1691
1694
1692
1695
def test_parse_multiple_delimited_dates_with_swap_warnings ():
1693
1696
# GH46210
1694
- warning_msg = "Specify a format to ensure consistent parsing"
1695
- with tm .assert_produces_warning (UserWarning , match = warning_msg ) as record :
1697
+ with pytest .raises (
1698
+ ValueError ,
1699
+ match = r"^time data '31/05/2000' does not match format '%m/%d/%Y' \(match\)$" ,
1700
+ ):
1696
1701
pd .to_datetime (["01/01/2000" , "31/05/2000" , "31/05/2001" , "01/02/2000" ])
1697
- assert len ({str (warning .message ) for warning in record }) == 1
1698
- # Using set(record) as repetitions of the same warning are suppressed
1699
- # https://docs.python.org/3/library/warnings.html
1700
- # and here we care to check that the warning is only shows once to users.
1701
1702
1702
1703
1703
1704
def _helper_hypothesis_delimited_date (call , date_string , ** kwargs ):
@@ -1860,97 +1861,51 @@ def test_parse_dates_and_keep_orgin_column(all_parsers):
1860
1861
1861
1862
def test_dayfirst_warnings ():
1862
1863
# GH 12585
1863
- warning_msg_day_first = (
1864
- r"Parsing dates in DD/MM/YYYY format when dayfirst=False \(the default\) was "
1865
- r"specified. This may lead to inconsistently parsed dates! Specify a format "
1866
- r"to ensure consistent parsing."
1867
- )
1868
- warning_msg_month_first = (
1869
- "Parsing dates in MM/DD/YYYY format when dayfirst=True was "
1870
- "specified. This may lead to inconsistently parsed dates! Specify a format "
1871
- "to ensure consistent parsing."
1872
- )
1873
1864
1874
1865
# CASE 1: valid input
1875
1866
input = "date\n 31/12/2014\n 10/03/2011"
1876
- expected_consistent = DatetimeIndex (
1867
+ expected = DatetimeIndex (
1877
1868
["2014-12-31" , "2011-03-10" ], dtype = "datetime64[ns]" , freq = None , name = "date"
1878
1869
)
1879
- expected_inconsistent = DatetimeIndex (
1880
- ["2014-12-31" , "2011-10-03" ], dtype = "datetime64[ns]" , freq = None , name = "date"
1870
+ warning_msg = (
1871
+ "Parsing dates in .* format when dayfirst=.* was specified. "
1872
+ "Pass `dayfirst=.*` or specify a format to silence this warning."
1881
1873
)
1882
1874
1883
1875
# A. dayfirst arg correct, no warning
1884
1876
res1 = read_csv (
1885
1877
StringIO (input ), parse_dates = ["date" ], dayfirst = True , index_col = "date"
1886
1878
).index
1887
- tm .assert_index_equal (expected_consistent , res1 )
1879
+ tm .assert_index_equal (expected , res1 )
1888
1880
1889
- # B. dayfirst arg incorrect, warning + incorrect output
1890
- with tm .assert_produces_warning (UserWarning , match = warning_msg_day_first ):
1881
+ # B. dayfirst arg incorrect, warning
1882
+ with tm .assert_produces_warning (UserWarning , match = warning_msg ):
1891
1883
res2 = read_csv (
1892
1884
StringIO (input ), parse_dates = ["date" ], dayfirst = False , index_col = "date"
1893
1885
).index
1894
- tm .assert_index_equal (expected_inconsistent , res2 )
1895
-
1896
- # C. dayfirst default arg, same as B
1897
- with tm .assert_produces_warning (UserWarning , match = warning_msg_day_first ):
1898
- res3 = read_csv (
1899
- StringIO (input ), parse_dates = ["date" ], dayfirst = False , index_col = "date"
1900
- ).index
1901
- tm .assert_index_equal (expected_inconsistent , res3 )
1902
-
1903
- # D. infer_datetime_format=True overrides dayfirst default
1904
- # no warning + correct result
1905
- res4 = read_csv (
1906
- StringIO (input ),
1907
- parse_dates = ["date" ],
1908
- infer_datetime_format = True ,
1909
- index_col = "date" ,
1910
- ).index
1911
- tm .assert_index_equal (expected_consistent , res4 )
1886
+ tm .assert_index_equal (expected , res2 )
1912
1887
1913
1888
# CASE 2: invalid input
1914
1889
# cannot consistently process with single format
1915
- # warnings *always* raised
1890
+ # return to user unaltered
1916
1891
1917
1892
# first in DD/MM/YYYY, second in MM/DD/YYYY
1918
1893
input = "date\n 31/12/2014\n 03/30/2011"
1919
- expected = DatetimeIndex (
1920
- ["2014-12-31" , "2011-03-30" ], dtype = "datetime64[ns]" , freq = None , name = "date"
1921
- )
1894
+ expected = Index (["31/12/2014" , "03/30/2011" ], dtype = "object" , name = "date" )
1922
1895
1923
1896
# A. use dayfirst=True
1924
- with tm .assert_produces_warning (UserWarning , match = warning_msg_month_first ):
1925
- res5 = read_csv (
1926
- StringIO (input ), parse_dates = ["date" ], dayfirst = True , index_col = "date"
1927
- ).index
1897
+ res5 = read_csv (
1898
+ StringIO (input ), parse_dates = ["date" ], dayfirst = True , index_col = "date"
1899
+ ).index
1928
1900
tm .assert_index_equal (expected , res5 )
1929
1901
1930
1902
# B. use dayfirst=False
1931
- with tm .assert_produces_warning (UserWarning , match = warning_msg_day_first ):
1903
+ with tm .assert_produces_warning (UserWarning , match = warning_msg ):
1932
1904
res6 = read_csv (
1933
1905
StringIO (input ), parse_dates = ["date" ], dayfirst = False , index_col = "date"
1934
1906
).index
1935
1907
tm .assert_index_equal (expected , res6 )
1936
1908
1937
- # C. use dayfirst default arg, same as B
1938
- with tm .assert_produces_warning (UserWarning , match = warning_msg_day_first ):
1939
- res7 = read_csv (
1940
- StringIO (input ), parse_dates = ["date" ], dayfirst = False , index_col = "date"
1941
- ).index
1942
- tm .assert_index_equal (expected , res7 )
1943
-
1944
- # D. use infer_datetime_format=True
1945
- with tm .assert_produces_warning (UserWarning , match = warning_msg_day_first ):
1946
- res8 = read_csv (
1947
- StringIO (input ),
1948
- parse_dates = ["date" ],
1949
- infer_datetime_format = True ,
1950
- index_col = "date" ,
1951
- ).index
1952
- tm .assert_index_equal (expected , res8 )
1953
-
1954
1909
1955
1910
@pytest .mark .parametrize (
1956
1911
"date_string, dayfirst" ,
@@ -1973,9 +1928,11 @@ def test_dayfirst_warnings_no_leading_zero(date_string, dayfirst):
1973
1928
expected = DatetimeIndex (
1974
1929
["2014-01-31" ], dtype = "datetime64[ns]" , freq = None , name = "date"
1975
1930
)
1976
- with tm .assert_produces_warning (
1977
- UserWarning , match = r"may lead to inconsistently parsed dates"
1978
- ):
1931
+ warning_msg = (
1932
+ "Parsing dates in .* format when dayfirst=.* was specified. "
1933
+ "Pass `dayfirst=.*` or specify a format to silence this warning."
1934
+ )
1935
+ with tm .assert_produces_warning (UserWarning , match = warning_msg ):
1979
1936
res = read_csv (
1980
1937
StringIO (initial_value ),
1981
1938
parse_dates = ["date" ],
0 commit comments