File tree 3 files changed +27
-14
lines changed
3 files changed +27
-14
lines changed Original file line number Diff line number Diff line change @@ -421,6 +421,20 @@ def array_with_unit_to_datetime(
421
421
422
422
return oresult, tz
423
423
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
424
438
425
439
@ cython.wraparound (False )
426
440
@ cython.boundscheck (False )
Original file line number Diff line number Diff line change @@ -129,19 +129,16 @@ class FulldatetimeDict(YearMonthDayDict, total=False):
129
129
130
130
def _guess_datetime_format_for_array (arr , dayfirst : bool | None = False ) -> str | None :
131
131
# 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
+ )
145
142
return None
146
143
147
144
Original file line number Diff line number Diff line change @@ -2100,7 +2100,7 @@ def test_to_datetime_dta_tz(self, klass):
2100
2100
2101
2101
2102
2102
class TestGuessDatetimeFormat :
2103
- @td .skip_if_not_us_locale
2103
+ # @td.skip_if_not_us_locale
2104
2104
@pytest .mark .parametrize (
2105
2105
"test_array" ,
2106
2106
[
@@ -2110,6 +2110,8 @@ class TestGuessDatetimeFormat:
2110
2110
"2011-12-30 00:00:00.000000" ,
2111
2111
],
2112
2112
[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" ],
2113
2115
["2011-12-30 00:00:00.000000" , "random_string" ],
2114
2116
],
2115
2117
)
You can’t perform that action at this time.
0 commit comments