@@ -1751,6 +1751,104 @@ def test_to_datetime_iso8601(self, cache, arg, exp_str):
1751
1751
exp = Timestamp (exp_str )
1752
1752
assert result [0 ] == exp
1753
1753
1754
+ @pytest .mark .parametrize (
1755
+ "input, format" ,
1756
+ [
1757
+ ("2012" , "%Y-%m" ),
1758
+ ("2012-01" , "%Y-%m-%d" ),
1759
+ ("2012-01-01" , "%Y-%m-%d %H" ),
1760
+ ("2012-01-01 10" , "%Y-%m-%d %H:%M" ),
1761
+ ("2012-01-01 10:00" , "%Y-%m-%d %H:%M:%S" ),
1762
+ (0 , "%Y-%m-%d" ),
1763
+ ],
1764
+ )
1765
+ @pytest .mark .parametrize ("exact" , [True , False ])
1766
+ def test_to_datetime_iso8601_fails (self , input , format , exact ):
1767
+ # https://github.com/pandas-dev/pandas/issues/12649
1768
+ with pytest .raises (
1769
+ ValueError ,
1770
+ match = rf"time data \"{ input } \" at position 0 doesn't match format { format } " ,
1771
+ ):
1772
+ to_datetime (input , format = format , exact = exact )
1773
+
1774
+ @pytest .mark .parametrize (
1775
+ "input, format" ,
1776
+ [
1777
+ ("2012-01-01" , "%Y-%m" ),
1778
+ ("2012-01-01 10" , "%Y-%m-%d" ),
1779
+ ("2012-01-01 10:00" , "%Y-%m-%d %H" ),
1780
+ ("2012-01-01 10:00:00" , "%Y-%m-%d %H:%M" ),
1781
+ (0 , "%Y-%m-%d" ),
1782
+ ],
1783
+ )
1784
+ def test_to_datetime_iso8601_exact_fails (self , input , format ):
1785
+ # https://github.com/pandas-dev/pandas/issues/12649
1786
+ with pytest .raises (
1787
+ ValueError ,
1788
+ match = rf"time data \"{ input } \" at position 0 doesn't match format { format } " ,
1789
+ ):
1790
+ to_datetime (input , format = format )
1791
+
1792
+ @pytest .mark .parametrize (
1793
+ "input, format" ,
1794
+ [
1795
+ ("2012-01-01" , "%Y-%m" ),
1796
+ ("2012-01-01 00" , "%Y-%m-%d" ),
1797
+ ("2012-01-01 00:00" , "%Y-%m-%d %H" ),
1798
+ ("2012-01-01 00:00:00" , "%Y-%m-%d %H:%M" ),
1799
+ ],
1800
+ )
1801
+ def test_to_datetime_iso8601_non_exact (self , input , format ):
1802
+ # https://github.com/pandas-dev/pandas/issues/12649
1803
+ expected = Timestamp (2012 , 1 , 1 )
1804
+ result = to_datetime (input , format = format , exact = False )
1805
+ assert result == expected
1806
+
1807
+ @pytest .mark .parametrize (
1808
+ "input, format" ,
1809
+ [
1810
+ ("2020-01" , "%Y/%m" ),
1811
+ ("2020-01-01" , "%Y/%m/%d" ),
1812
+ ("2020-01-01 00" , "%Y/%m/%dT%H" ),
1813
+ ("2020-01-01T00" , "%Y/%m/%d %H" ),
1814
+ ("2020-01-01 00:00" , "%Y/%m/%dT%H:%M" ),
1815
+ ("2020-01-01T00:00" , "%Y/%m/%d %H:%M" ),
1816
+ ("2020-01-01 00:00:00" , "%Y/%m/%dT%H:%M:%S" ),
1817
+ ("2020-01-01T00:00:00" , "%Y/%m/%d %H:%M:%S" ),
1818
+ ],
1819
+ )
1820
+ def test_to_datetime_iso8601_separator (self , input , format ):
1821
+ # https://github.com/pandas-dev/pandas/issues/12649
1822
+ with pytest .raises (
1823
+ ValueError ,
1824
+ match = (
1825
+ rf"time data \"{ input } \" at position 0 doesn\'t match format { format } "
1826
+ ),
1827
+ ):
1828
+ to_datetime (input , format = format )
1829
+
1830
+ @pytest .mark .parametrize (
1831
+ "input, format" ,
1832
+ [
1833
+ ("2020-01" , "%Y-%m" ),
1834
+ ("2020-01-01" , "%Y-%m-%d" ),
1835
+ ("2020-01-01 00" , "%Y-%m-%d %H" ),
1836
+ ("2020-01-01T00" , "%Y-%m-%dT%H" ),
1837
+ ("2020-01-01 00:00" , "%Y-%m-%d %H:%M" ),
1838
+ ("2020-01-01T00:00" , "%Y-%m-%dT%H:%M" ),
1839
+ ("2020-01-01 00:00:00" , "%Y-%m-%d %H:%M:%S" ),
1840
+ ("2020-01-01T00:00:00" , "%Y-%m-%dT%H:%M:%S" ),
1841
+ ("2020-01-01T00:00:00.000" , "%Y-%m-%dT%H:%M:%S.%f" ),
1842
+ ("2020-01-01T00:00:00.000000" , "%Y-%m-%dT%H:%M:%S.%f" ),
1843
+ ("2020-01-01T00:00:00.000000000" , "%Y-%m-%dT%H:%M:%S.%f" ),
1844
+ ],
1845
+ )
1846
+ def test_to_datetime_iso8601_valid (self , input , format ):
1847
+ # https://github.com/pandas-dev/pandas/issues/12649
1848
+ expected = Timestamp (2020 , 1 , 1 )
1849
+ result = to_datetime (input , format = format )
1850
+ assert result == expected
1851
+
1754
1852
def test_to_datetime_default (self , cache ):
1755
1853
rs = to_datetime ("2001" , cache = cache )
1756
1854
xp = datetime (2001 , 1 , 1 )
@@ -2264,7 +2362,7 @@ def test_day_not_in_month_raise(self, cache):
2264
2362
2265
2363
@pytest .mark .parametrize ("arg" , ["2015-02-29" , "2015-02-32" , "2015-04-31" ])
2266
2364
def test_day_not_in_month_raise_value (self , cache , arg ):
2267
- msg = f'time data "{ arg } " at position 0 doesn\' t match format specified '
2365
+ msg = f'time data "{ arg } " at position 0 doesn\' t match format \" %Y-%m-%d \" '
2268
2366
with pytest .raises (ValueError , match = msg ):
2269
2367
to_datetime (arg , errors = "raise" , format = "%Y-%m-%d" , cache = cache )
2270
2368
0 commit comments