File tree 3 files changed +24
-17
lines changed
3 files changed +24
-17
lines changed Original file line number Diff line number Diff line change @@ -421,20 +421,23 @@ 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."""
424
+ @ cython.wraparound (False )
425
+ @ cython.boundscheck (False )
426
+ def first_non_null (values: ndarray ) -> int:
427
+ """Find position of first non-null value , return -1 if there isn't one."""
426
428
cdef:
427
429
Py_ssize_t n = len (values)
428
430
Py_ssize_t i
431
+ int result
429
432
for i in range(n ):
430
433
val = values[i]
431
434
if checknull_with_nat_and_na(val):
432
435
continue
433
436
if isinstance (val, str ) and (len (val) == 0 or val in nat_strings):
434
437
continue
435
- return val
438
+ return i
436
439
else :
437
- return None
440
+ return - 1
438
441
439
442
@ cython.wraparound (False )
440
443
@ cython.boundscheck (False )
Original file line number Diff line number Diff line change @@ -129,16 +129,19 @@ 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 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
- )
132
+ if (first_non_null := tslib .first_non_null (arr )) != - 1 :
133
+ if type (first_non_nan_element := arr [first_non_null ]) 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
+ )
142
145
return None
143
146
144
147
Original file line number Diff line number Diff line change @@ -2100,9 +2100,9 @@ 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
- "test_array " ,
2105
+ "test_list " ,
2106
2106
[
2107
2107
[
2108
2108
"2011-12-30 00:00:00.000000" ,
@@ -2115,8 +2115,9 @@ class TestGuessDatetimeFormat:
2115
2115
["2011-12-30 00:00:00.000000" , "random_string" ],
2116
2116
],
2117
2117
)
2118
- def test_guess_datetime_format_for_array (self , test_array ):
2118
+ def test_guess_datetime_format_for_array (self , test_list ):
2119
2119
expected_format = "%Y-%m-%d %H:%M:%S.%f"
2120
+ test_array = np .array (test_list , dtype = object )
2120
2121
assert tools ._guess_datetime_format_for_array (test_array ) == expected_format
2121
2122
2122
2123
@td .skip_if_not_us_locale
You can’t perform that action at this time.
0 commit comments