Skip to content

WARN: Only warn about inconsistent parsing if there are multiple non-null elements #52195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,16 @@ def _guess_datetime_format_for_array(arr, dayfirst: bool | None = False) -> str
)
if guessed_format is not None:
return guessed_format
warnings.warn(
"Could not infer format, so each element will be parsed "
"individually, falling back to `dateutil`. To ensure parsing is "
"consistent and as-expected, please specify a format.",
UserWarning,
stacklevel=find_stack_level(),
)
# If there are multiple non-null elements, warn about
# how parsing might not be consistent
if tslib.first_non_null(arr[first_non_null + 1 :]) != -1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only minor nit I'd have is to add an explicit if first_non_null == len(arr) check preceding this. It doesn't have any impact as long as this stays a purely Python function, but if someone tried to convert down to C I'm not sure how slicing beyond the length of the array would work

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore this. Thought this was a pyx file

warnings.warn(
"Could not infer format, so each element will be parsed "
"individually, falling back to `dateutil`. To ensure parsing is "
"consistent and as-expected, please specify a format.",
UserWarning,
stacklevel=find_stack_level(),
)
return None


Expand Down
16 changes: 9 additions & 7 deletions pandas/tests/io/parser/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,13 +1252,15 @@ def test_bad_date_parse(all_parsers, cache_dates, value):
parser = all_parsers
s = StringIO((f"{value},\n") * 50000)

if parser.engine == "pyarrow":
if parser.engine == "pyarrow" and not cache_dates:
# None in input gets converted to 'None', for which
# pandas tries to guess the datetime format, triggering
# the warning. TODO: parse dates directly in pyarrow, see
# https://github.com/pandas-dev/pandas/issues/48017
warn = UserWarning
else:
# Note: warning is not raised if 'cache_dates', because here there is only a
# single unique date and hence no risk of inconsistent parsing.
warn = None
parser.read_csv_check_warnings(
warn,
Expand All @@ -1285,6 +1287,10 @@ def test_bad_date_parse_with_warning(all_parsers, cache_dates, value):
# TODO: parse dates directly in pyarrow, see
# https://github.com/pandas-dev/pandas/issues/48017
warn = None
elif cache_dates:
# Note: warning is not raised if 'cache_dates', because here there is only a
# single unique date and hence no risk of inconsistent parsing.
warn = None
else:
warn = UserWarning
parser.read_csv_check_warnings(
Expand Down Expand Up @@ -1737,9 +1743,7 @@ def test_parse_timezone(all_parsers):
def test_invalid_parse_delimited_date(all_parsers, date_string):
parser = all_parsers
expected = DataFrame({0: [date_string]}, dtype="object")
result = parser.read_csv_check_warnings(
UserWarning,
"Could not infer format",
result = parser.read_csv(
StringIO(date_string),
header=None,
parse_dates=[0],
Expand Down Expand Up @@ -2063,9 +2067,7 @@ def test_infer_first_column_as_index(all_parsers):
# GH#11019
parser = all_parsers
data = "a,b,c\n1970-01-01,2,3,4"
result = parser.read_csv_check_warnings(
UserWarning,
"Could not infer format",
result = parser.read_csv(
StringIO(data),
parse_dates=["a"],
)
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/io/parser/usecols/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ def test_usecols_with_parse_dates4(all_parsers):
}
expected = DataFrame(cols, columns=["a_b"] + list("cdefghij"))

result = parser.read_csv_check_warnings(
UserWarning,
"Could not infer format",
result = parser.read_csv(
StringIO(data),
usecols=usecols,
parse_dates=parse_dates,
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,8 +1231,7 @@ def test_value_counts_datetime_outofbounds(self):
tm.assert_series_equal(res, exp)

# GH 12424
with tm.assert_produces_warning(UserWarning, match="Could not infer format"):
res = to_datetime(Series(["2362-01-01", np.nan]), errors="ignore")
res = to_datetime(Series(["2362-01-01", np.nan]), errors="ignore")
exp = Series(["2362-01-01", np.nan], dtype=object)
tm.assert_series_equal(res, exp)

Expand Down
Loading