peacock-data-public-datasets-idc-cronscript
/
venv
/lib
/python3.10
/site-packages
/pandas
/tests
/tslibs
/test_parsing.py
""" | |
Tests for Timestamp parsing, aimed at pandas/_libs/tslibs/parsing.pyx | |
""" | |
from datetime import datetime | |
import re | |
from dateutil.parser import parse as du_parse | |
from dateutil.tz import tzlocal | |
from hypothesis import given | |
import numpy as np | |
import pytest | |
from pandas._libs.tslibs import ( | |
parsing, | |
strptime, | |
) | |
from pandas._libs.tslibs.parsing import parse_datetime_string_with_reso | |
from pandas.compat import ( | |
ISMUSL, | |
is_platform_windows, | |
) | |
import pandas.util._test_decorators as td | |
import pandas._testing as tm | |
from pandas._testing._hypothesis import DATETIME_NO_TZ | |
def test_parsing_tzlocal_deprecated(): | |
# GH#50791 | |
msg = ( | |
"Parsing 'EST' as tzlocal.*" | |
"Pass the 'tz' keyword or call tz_localize after construction instead" | |
) | |
dtstr = "Jan 15 2004 03:00 EST" | |
with tm.set_timezone("US/Eastern"): | |
with tm.assert_produces_warning(FutureWarning, match=msg): | |
res, _ = parse_datetime_string_with_reso(dtstr) | |
assert isinstance(res.tzinfo, tzlocal) | |
with tm.assert_produces_warning(FutureWarning, match=msg): | |
res = parsing.py_parse_datetime_string(dtstr) | |
assert isinstance(res.tzinfo, tzlocal) | |
def test_parse_datetime_string_with_reso(): | |
(parsed, reso) = parse_datetime_string_with_reso("4Q1984") | |
(parsed_lower, reso_lower) = parse_datetime_string_with_reso("4q1984") | |
assert reso == reso_lower | |
assert parsed == parsed_lower | |
def test_parse_datetime_string_with_reso_nanosecond_reso(): | |
# GH#46811 | |
parsed, reso = parse_datetime_string_with_reso("2022-04-20 09:19:19.123456789") | |
assert reso == "nanosecond" | |
def test_parse_datetime_string_with_reso_invalid_type(): | |
# Raise on invalid input, don't just return it | |
msg = "Argument 'date_string' has incorrect type (expected str, got tuple)" | |
with pytest.raises(TypeError, match=re.escape(msg)): | |
parse_datetime_string_with_reso((4, 5)) | |
def test_parse_time_quarter_with_dash(dashed, normal): | |
# see gh-9688 | |
(parsed_dash, reso_dash) = parse_datetime_string_with_reso(dashed) | |
(parsed, reso) = parse_datetime_string_with_reso(normal) | |
assert parsed_dash == parsed | |
assert reso_dash == reso | |
def test_parse_time_quarter_with_dash_error(dashed): | |
msg = f"Unknown datetime string format, unable to parse: {dashed}" | |
with pytest.raises(parsing.DateParseError, match=msg): | |
parse_datetime_string_with_reso(dashed) | |
def test_does_not_convert_mixed_integer(date_string, expected): | |
assert parsing._does_string_look_like_datetime(date_string) is expected | |
def test_parsers_quarterly_with_freq_error(date_str, kwargs, msg): | |
with pytest.raises(parsing.DateParseError, match=msg): | |
parsing.parse_datetime_string_with_reso(date_str, **kwargs) | |
def test_parsers_quarterly_with_freq(date_str, freq, expected): | |
result, _ = parsing.parse_datetime_string_with_reso(date_str, freq=freq) | |
assert result == expected | |
def test_parsers_quarter_invalid(date_str): | |
if date_str == "6Q-20": | |
msg = ( | |
"Incorrect quarterly string is given, quarter " | |
f"must be between 1 and 4: {date_str}" | |
) | |
else: | |
msg = f"Unknown datetime string format, unable to parse: {date_str}" | |
with pytest.raises(ValueError, match=msg): | |
parsing.parse_datetime_string_with_reso(date_str) | |
def test_parsers_month_freq(date_str, expected): | |
result, _ = parsing.parse_datetime_string_with_reso(date_str, freq="ME") | |
assert result == expected | |
def test_guess_datetime_format_with_parseable_formats(string, fmt): | |
with tm.maybe_produces_warning( | |
UserWarning, fmt is not None and re.search(r"%d.*%m", fmt) | |
): | |
result = parsing.guess_datetime_format(string) | |
assert result == fmt | |
def test_guess_datetime_format_with_dayfirst(dayfirst, expected): | |
ambiguous_string = "01/01/2011" | |
result = parsing.guess_datetime_format(ambiguous_string, dayfirst=dayfirst) | |
assert result == expected | |
def test_guess_datetime_format_with_locale_specific_formats(string, fmt): | |
result = parsing.guess_datetime_format(string) | |
assert result == fmt | |
def test_guess_datetime_format_invalid_inputs(invalid_dt): | |
# A datetime string must include a year, month and a day for it to be | |
# guessable, in addition to being a string that looks like a datetime. | |
assert parsing.guess_datetime_format(invalid_dt) is None | |
def test_guess_datetime_format_wrong_type_inputs(invalid_type_dt): | |
# A datetime string must include a year, month and a day for it to be | |
# guessable, in addition to being a string that looks like a datetime. | |
with pytest.raises( | |
TypeError, | |
match=r"^Argument 'dt_str' has incorrect type \(expected str, got .*\)$", | |
): | |
parsing.guess_datetime_format(invalid_type_dt) | |
def test_guess_datetime_format_no_padding(string, fmt, dayfirst, warning): | |
# see gh-11142 | |
msg = ( | |
rf"Parsing dates in {fmt} format when dayfirst=False \(the default\) " | |
"was specified. " | |
"Pass `dayfirst=True` or specify a format to silence this warning." | |
) | |
with tm.assert_produces_warning(warning, match=msg): | |
result = parsing.guess_datetime_format(string, dayfirst=dayfirst) | |
assert result == fmt | |
def test_try_parse_dates(): | |
arr = np.array(["5/1/2000", "6/1/2000", "7/1/2000"], dtype=object) | |
result = parsing.try_parse_dates(arr, parser=lambda x: du_parse(x, dayfirst=True)) | |
expected = np.array([du_parse(d, dayfirst=True) for d in arr]) | |
tm.assert_numpy_array_equal(result, expected) | |
def test_parse_datetime_string_with_reso_check_instance_type_raise_exception(): | |
# issue 20684 | |
msg = "Argument 'date_string' has incorrect type (expected str, got tuple)" | |
with pytest.raises(TypeError, match=re.escape(msg)): | |
parse_datetime_string_with_reso((1, 2, 3)) | |
result = parse_datetime_string_with_reso("2019") | |
expected = (datetime(2019, 1, 1), "year") | |
assert result == expected | |
def test_is_iso_format(fmt, expected): | |
# see gh-41047 | |
result = strptime._test_format_is_iso(fmt) | |
assert result == expected | |
def test_guess_datetime_format_f(input): | |
# https://github.com/pandas-dev/pandas/issues/49043 | |
result = parsing.guess_datetime_format(input) | |
expected = "%Y-%m-%dT%H:%M:%S.%f" | |
assert result == expected | |
def _helper_hypothesis_delimited_date(call, date_string, **kwargs): | |
msg, result = None, None | |
try: | |
result = call(date_string, **kwargs) | |
except ValueError as err: | |
msg = str(err) | |
return msg, result | |
def test_hypothesis_delimited_date( | |
request, date_format, dayfirst, delimiter, test_datetime | |
): | |
if date_format == "%m %Y" and delimiter == ".": | |
request.applymarker( | |
pytest.mark.xfail( | |
reason="parse_datetime_string cannot reliably tell whether " | |
"e.g. %m.%Y is a float or a date" | |
) | |
) | |
date_string = test_datetime.strftime(date_format.replace(" ", delimiter)) | |
except_out_dateutil, result = _helper_hypothesis_delimited_date( | |
parsing.py_parse_datetime_string, date_string, dayfirst=dayfirst | |
) | |
except_in_dateutil, expected = _helper_hypothesis_delimited_date( | |
du_parse, | |
date_string, | |
default=datetime(1, 1, 1), | |
dayfirst=dayfirst, | |
yearfirst=False, | |
) | |
assert except_out_dateutil == except_in_dateutil | |
assert result == expected | |