Skip to content

Commit 772c274

Browse files
author
MarcoGorelli
committed
test non-padded
1 parent 5c87a15 commit 772c274

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

pandas/tests/tools/test_to_datetime.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ def test_to_datetime_format_YYYYMM_with_nat(self, cache):
146146
result = to_datetime(ser, format="%Y%m", cache=cache)
147147
tm.assert_series_equal(result, expected)
148148

149-
def test_to_datetime_format_YYYYMM_with_nat(self, cache):
149+
def test_to_datetime_format_YYYYMM_with_nat_2(self, cache):
150+
# is this different from above? can remove?
150151
# https://github.com/pandas-dev/pandas/issues/50237
151152
ser = Series([198012, 198012] + [198101] * 5)
152153
expected = Series(
@@ -1748,8 +1749,8 @@ def test_dataframe_coerce(self, cache):
17481749
df2 = DataFrame({"year": [2015, 2016], "month": [2, 20], "day": [4, 5]})
17491750

17501751
msg = (
1751-
r"cannot assemble the datetimes: time data .+ at position 1 doesn\'t "
1752-
r"match format \"%Y%m%d\""
1752+
r"cannot assemble the datetimes: time data '.*' does not match "
1753+
r"format '%Y%m%d' \(match\)"
17531754
)
17541755
with pytest.raises(ValueError, match=msg):
17551756
to_datetime(df2, cache=cache)
@@ -1825,10 +1826,7 @@ def test_dataframe_mixed(self, cache):
18251826
def test_dataframe_float(self, cache):
18261827
# float
18271828
df = DataFrame({"year": [2000, 2001], "month": [1.5, 1], "day": [1, 1]})
1828-
msg = (
1829-
'cannot assemble the datetimes: time data "20000151" at position 0 '
1830-
'doesn\'t match format "%Y%m%d"'
1831-
)
1829+
msg = "cannot assemble the datetimes: unconverted data remains: 1"
18321830
with pytest.raises(ValueError, match=msg):
18331831
to_datetime(df, cache=cache)
18341832

@@ -1887,10 +1885,7 @@ def test_to_datetime_iso8601_fails(self, input, format, exact):
18871885
# `format` is longer than the string, so this fails regardless of `exact`
18881886
with pytest.raises(
18891887
ValueError,
1890-
match=(
1891-
rf"time data \"{input}\" at position 0 doesn't match format "
1892-
rf"\"{format}\""
1893-
),
1888+
match=(rf"time data '{input}' does not match format '{format}'"),
18941889
):
18951890
to_datetime(input, format=format, exact=exact)
18961891

@@ -1909,10 +1904,7 @@ def test_to_datetime_iso8601_exact_fails(self, input, format):
19091904
# `format` is shorter than the date string, so only fails with `exact=True`
19101905
with pytest.raises(
19111906
ValueError,
1912-
match=(
1913-
rf"time data \"{input}\" at position 0 doesn't match format "
1914-
rf"\"{format}\""
1915-
),
1907+
match=("unconverted data remains: |does not match format"),
19161908
):
19171909
to_datetime(input, format=format)
19181910

@@ -1948,10 +1940,7 @@ def test_to_datetime_iso8601_separator(self, input, format):
19481940
# https://github.com/pandas-dev/pandas/issues/12649
19491941
with pytest.raises(
19501942
ValueError,
1951-
match=(
1952-
rf"time data \"{input}\" at position 0 doesn\'t match format "
1953-
rf"\"{format}\""
1954-
),
1943+
match=(rf"time data \'{input}\' does not match format '{format}'"),
19551944
):
19561945
to_datetime(input, format=format)
19571946

@@ -1977,6 +1966,28 @@ def test_to_datetime_iso8601_valid(self, input, format):
19771966
result = to_datetime(input, format=format)
19781967
assert result == expected
19791968

1969+
@pytest.mark.parametrize(
1970+
"input, format",
1971+
[
1972+
("2020-1", "%Y-%m"),
1973+
("2020-1-1", "%Y-%m-%d"),
1974+
("2020-1-1 0", "%Y-%m-%d %H"),
1975+
("2020-1-1T0", "%Y-%m-%dT%H"),
1976+
("2020-1-1 0:0", "%Y-%m-%d %H:%M"),
1977+
("2020-1-1T0:0", "%Y-%m-%dT%H:%M"),
1978+
("2020-1-1 0:0:0", "%Y-%m-%d %H:%M:%S"),
1979+
("2020-1-1T0:0:0", "%Y-%m-%dT%H:%M:%S"),
1980+
("2020-1-1T0:0:0.000", "%Y-%m-%dT%H:%M:%S.%f"),
1981+
("2020-1-1T0:0:0.000000", "%Y-%m-%dT%H:%M:%S.%f"),
1982+
("2020-1-1T0:0:0.000000000", "%Y-%m-%dT%H:%M:%S.%f"),
1983+
],
1984+
)
1985+
def test_to_datetime_iso8601_non_padded(self, input, format):
1986+
# https://github.com/pandas-dev/pandas/issues/21422
1987+
expected = Timestamp(2020, 1, 1)
1988+
result = to_datetime(input, format=format)
1989+
assert result == expected
1990+
19801991
@pytest.mark.parametrize(
19811992
"input, format",
19821993
[
@@ -2479,7 +2490,7 @@ def test_day_not_in_month_raise(self, cache):
24792490

24802491
@pytest.mark.parametrize("arg", ["2015-02-29", "2015-02-32", "2015-04-31"])
24812492
def test_day_not_in_month_raise_value(self, cache, arg):
2482-
msg = f'time data "{arg}" at position 0 doesn\'t match format "%Y-%m-%d"'
2493+
msg = "day is out of range for month|unconverted data remains"
24832494
with pytest.raises(ValueError, match=msg):
24842495
to_datetime(arg, errors="raise", format="%Y-%m-%d", cache=cache)
24852496

0 commit comments

Comments
 (0)