-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
[BUG] Make the datetime guesser cover edge case where the first date's formatting isn't the whole list's formatting #62965
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
Open
Ben-Cutler
wants to merge
6
commits into
pandas-dev:main
Choose a base branch
from
Ben-Cutler:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+92
−83
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5b024e8
[-] Try to make the datetime guesser better
ben-cutler-tamarin ac7d240
Add my note + add type hint
ben-cutler-tamarin 195e405
Move Import
ben-cutler-tamarin 9ffc3d3
Remove imports + add if statement for NaTType
ben-cutler-tamarin dbcb428
Fix typing and add test + check for NaT
ben-cutler-tamarin 2e31faa
Fix a few tests, want to make sure it'll be approved if the tests are…
ben-cutler-tamarin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections import abc | ||
| from datetime import date | ||
| from datetime import ( | ||
| date, | ||
| datetime, | ||
| ) | ||
| from functools import partial | ||
| from itertools import islice | ||
| from typing import ( | ||
|
|
@@ -12,7 +15,6 @@ | |
| cast, | ||
| overload, | ||
| ) | ||
| import warnings | ||
|
|
||
| import numpy as np | ||
|
|
||
|
|
@@ -42,7 +44,6 @@ | |
| DateTimeErrorChoices, | ||
| ) | ||
| from pandas.util._decorators import set_module | ||
| from pandas.util._exceptions import find_stack_level | ||
|
|
||
| from pandas.core.dtypes.common import ( | ||
| ensure_object, | ||
|
|
@@ -84,7 +85,6 @@ | |
| Callable, | ||
| Hashable, | ||
| ) | ||
|
|
||
| from pandas._libs.tslibs.nattype import NaTType | ||
| from pandas._libs.tslibs.timedeltas import UnitChoices | ||
|
|
||
|
|
@@ -129,26 +129,34 @@ class FulldatetimeDict(YearMonthDayDict, total=False): | |
| # --------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def _guess_datetime_format_for_array(arr, dayfirst: bool | None = False) -> str | None: | ||
| def _guess_datetime_format_for_array( | ||
| arr: np.ndarray, dayfirst: bool | None = False | ||
| ) -> str | None: | ||
| # Try to guess the format based on the first non-NaN element, return None if can't | ||
| if (first_non_null := tslib.first_non_null(arr)) != -1: | ||
| if type(first_non_nan_element := arr[first_non_null]) is str: | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to replace |
||
| # GH#32264 np.str_ object | ||
| guessed_format = guess_datetime_format( | ||
| first_non_nan_element, dayfirst=dayfirst | ||
| ) | ||
| search_start = 0 | ||
| allowed_formats = set() | ||
| while not search_start >= len(arr): | ||
| non_null_offset = tslib.first_non_null(arr[search_start:]) | ||
| if non_null_offset == -1: | ||
| break | ||
| idx = search_start + non_null_offset | ||
| element = arr[idx] | ||
| if isinstance(element, str): | ||
| guessed_format = guess_datetime_format(str(element), dayfirst=dayfirst) | ||
| if guessed_format is not None: | ||
| return guessed_format | ||
| # 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: | ||
| 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(), | ||
| ) | ||
| allowed_formats.add(guessed_format) | ||
| search_start = idx + 1 | ||
| # Look through the formats and see if one satisfies each item in the array | ||
| for fmt in list(allowed_formats): | ||
| try: | ||
| [ | ||
| datetime.strptime(date_string, fmt) | ||
| for date_string in arr | ||
| if date_string and isinstance(date_string, str) | ||
| ] | ||
| return fmt | ||
| except ValueError: | ||
| pass | ||
| return None | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure what to do with this code. Are these kinds of warnings helpful? They were causing my tests to fail on pytest teardown, and was being logged on tests which had different formatting for the input string (So if I specified dayfirst = True on a month first input it'd raise the warning, and if I specified dayfirst=False on a day first test case it'd fail.
Happy to revert this or make some other change