Skip to content

REF: remove unnecessary args in libparsing #46821

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 3 commits into from
Apr 22, 2022
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
4 changes: 1 addition & 3 deletions pandas/_libs/tslibs/parsing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ def try_parse_datetime_components(
def format_is_iso(f: str) -> bool: ...
def guess_datetime_format(
dt_str,
dayfirst: bool = ...,
dt_str_parse=...,
dt_str_split=...,
dayfirst: bool | None = ...,
) -> str | None: ...
def concat_date_cols(
date_cols: tuple,
Expand Down
23 changes: 4 additions & 19 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -872,12 +872,7 @@ def format_is_iso(f: str) -> bint:
return False


def guess_datetime_format(
dt_str,
bint dayfirst=False,
dt_str_parse=du_parse,
dt_str_split=_DATEUTIL_LEXER_SPLIT,
):
def guess_datetime_format(dt_str, bint dayfirst=False):
"""
Guess the datetime format of a given datetime string.

Expand All @@ -889,20 +884,11 @@ def guess_datetime_format(
If True parses dates with the day first, eg 20/01/2005
Warning: dayfirst=True is not strict, but will prefer to parse
with day first (this is a known bug).
dt_str_parse : function, defaults to `dateutil.parser.parse`
This function should take in a datetime string and return
a `datetime.datetime` guess that the datetime string represents
dt_str_split : function, defaults to `_DATEUTIL_LEXER_SPLIT` (dateutil)
This function should take in a datetime string and return
a list of strings, the guess of the various specific parts
e.g. '2011/12/30' -> ['2011', '/', '12', '/', '30']

Returns
-------
ret : datetime format string (for `strftime` or `strptime`)
"""
if dt_str_parse is None or dt_str_split is None:
return None

if not isinstance(dt_str, str):
return None
Expand Down Expand Up @@ -934,17 +920,16 @@ def guess_datetime_format(
datetime_attrs_to_format.insert(0, day_attribute_and_format)

try:
parsed_datetime = dt_str_parse(dt_str, dayfirst=dayfirst)
parsed_datetime = du_parse(dt_str, dayfirst=dayfirst)
except (ValueError, OverflowError):
# In case the datetime can't be parsed, its format cannot be guessed
return None

if parsed_datetime is None:
return None

# the default dt_str_split from dateutil will never raise here; we assume
# that any user-provided function will not either.
tokens = dt_str_split(dt_str)
# _DATEUTIL_LEXER_SPLIT from dateutil will never raise here
tokens = _DATEUTIL_LEXER_SPLIT(dt_str)

# Normalize offset part of tokens.
# There are multiple formats for the timezone offset.
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ class FulldatetimeDict(YearMonthDayDict, total=False):
# ---------------------------------------------------------------------


def _guess_datetime_format_for_array(arr, **kwargs):
def _guess_datetime_format_for_array(arr, dayfirst: bool | None = False):
# Try to guess the format based on the first non-NaN element
non_nan_elements = notna(arr).nonzero()[0]
if len(non_nan_elements):
return guess_datetime_format(arr[non_nan_elements[0]], **kwargs)
return guess_datetime_format(arr[non_nan_elements[0]], dayfirst=dayfirst)


def should_cache(
Expand Down