File tree 3 files changed +24
-6
lines changed
3 files changed +24
-6
lines changed Original file line number Diff line number Diff line change @@ -421,6 +421,23 @@ def array_with_unit_to_datetime(
421
421
422
422
return oresult, tz
423
423
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."""
428
+ cdef:
429
+ Py_ssize_t n = len (values)
430
+ Py_ssize_t i
431
+ int result
432
+ for i in range(n ):
433
+ val = values[i]
434
+ if checknull_with_nat_and_na(val):
435
+ continue
436
+ if isinstance (val, str ) and (len (val) == 0 or val in nat_strings):
437
+ continue
438
+ return i
439
+ else :
440
+ return - 1
424
441
425
442
@ cython.wraparound (False )
426
443
@ cython.boundscheck (False )
Original file line number Diff line number Diff line change @@ -126,9 +126,8 @@ class FulldatetimeDict(YearMonthDayDict, total=False):
126
126
127
127
def _guess_datetime_format_for_array (arr , dayfirst : bool | None = False ) -> str | None :
128
128
# Try to guess the format based on the first non-NaN element, return None if can't
129
- non_nan_elements = notna (arr ).nonzero ()[0 ]
130
- if len (non_nan_elements ):
131
- if type (first_non_nan_element := arr [non_nan_elements [0 ]]) is str :
129
+ if (first_non_null := tslib .first_non_null (arr )) != - 1 :
130
+ if type (first_non_nan_element := arr [first_non_null ]) is str :
132
131
# GH#32264 np.str_ object
133
132
return guess_datetime_format (first_non_nan_element , dayfirst = dayfirst )
134
133
return None
Original file line number Diff line number Diff line change @@ -2093,21 +2093,23 @@ def test_to_datetime_dta_tz(self, klass):
2093
2093
2094
2094
2095
2095
class TestGuessDatetimeFormat :
2096
- @td .skip_if_not_us_locale
2097
2096
@pytest .mark .parametrize (
2098
- "test_array " ,
2097
+ "test_list " ,
2099
2098
[
2100
2099
[
2101
2100
"2011-12-30 00:00:00.000000" ,
2102
2101
"2011-12-30 00:00:00.000000" ,
2103
2102
"2011-12-30 00:00:00.000000" ,
2104
2103
],
2105
2104
[np .nan , np .nan , "2011-12-30 00:00:00.000000" ],
2105
+ ["" , "2011-12-30 00:00:00.000000" ],
2106
+ ["NaT" , "2011-12-30 00:00:00.000000" ],
2106
2107
["2011-12-30 00:00:00.000000" , "random_string" ],
2107
2108
],
2108
2109
)
2109
- def test_guess_datetime_format_for_array (self , test_array ):
2110
+ def test_guess_datetime_format_for_array (self , test_list ):
2110
2111
expected_format = "%Y-%m-%d %H:%M:%S.%f"
2112
+ test_array = np .array (test_list , dtype = object )
2111
2113
assert tools ._guess_datetime_format_for_array (test_array ) == expected_format
2112
2114
2113
2115
@td .skip_if_not_us_locale
You can’t perform that action at this time.
0 commit comments