Skip to content

Commit eec9837

Browse files
author
MarcoGorelli
committed
fixup
1 parent 6a8390d commit eec9837

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

pandas/_libs/tslib.pyx

+14
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,20 @@ def array_with_unit_to_datetime(
421421

422422
return oresult, tz
423423

424+
def first_non_null(values: ndarray):
425+
"""Find first non-null value, return None if there isn't one."""
426+
cdef:
427+
Py_ssize_t n = len(values)
428+
Py_ssize_t i
429+
for i in range(n):
430+
val = values[i]
431+
if checknull_with_nat_and_na(val):
432+
continue
433+
if isinstance(val, str) and (len(val) == 0 or val in nat_strings):
434+
continue
435+
return val
436+
else:
437+
return None
424438

425439
@cython.wraparound(False)
426440
@cython.boundscheck(False)

pandas/core/tools/datetimes.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,16 @@ class FulldatetimeDict(YearMonthDayDict, total=False):
129129

130130
def _guess_datetime_format_for_array(arr, dayfirst: bool | None = False) -> str | None:
131131
# Try to guess the format based on the first non-NaN element, return None if can't
132-
if len(non_nan_elements := notna(arr).nonzero()[0]):
133-
if type(first_non_nan_element := arr[non_nan_elements[0]]) is str:
134-
# GH#32264 np.str_ object
135-
guessed_format = guess_datetime_format(
136-
first_non_nan_element, dayfirst=dayfirst
137-
)
138-
if guessed_format is not None:
139-
return guessed_format
140-
warnings.warn(
141-
"Could not infer format - "
142-
"to ensure consistent parsing, specify a format.",
143-
stacklevel=find_stack_level(),
144-
)
132+
if type(first_non_nan_element := tslib.first_non_null(arr)) is str:
133+
# GH#32264 np.str_ object
134+
guessed_format = guess_datetime_format(first_non_nan_element, dayfirst=dayfirst)
135+
if guessed_format is not None:
136+
return guessed_format
137+
warnings.warn(
138+
"Could not infer format - "
139+
"to ensure consistent parsing, specify a format.",
140+
stacklevel=find_stack_level(),
141+
)
145142
return None
146143

147144

pandas/tests/tools/test_to_datetime.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2100,7 +2100,7 @@ def test_to_datetime_dta_tz(self, klass):
21002100

21012101

21022102
class TestGuessDatetimeFormat:
2103-
@td.skip_if_not_us_locale
2103+
# @td.skip_if_not_us_locale
21042104
@pytest.mark.parametrize(
21052105
"test_array",
21062106
[
@@ -2110,6 +2110,8 @@ class TestGuessDatetimeFormat:
21102110
"2011-12-30 00:00:00.000000",
21112111
],
21122112
[np.nan, np.nan, "2011-12-30 00:00:00.000000"],
2113+
["", "2011-12-30 00:00:00.000000"],
2114+
["NaT", "2011-12-30 00:00:00.000000"],
21132115
["2011-12-30 00:00:00.000000", "random_string"],
21142116
],
21152117
)

0 commit comments

Comments
 (0)