@@ -103,6 +103,7 @@ def infer_dtype(object _values):
103
103
Py_ssize_t i, n
104
104
object val
105
105
ndarray values
106
+ bint seen_pdnat = False , seen_val = False
106
107
107
108
if isinstance (_values, np.ndarray):
108
109
values = _values
@@ -141,17 +142,34 @@ def infer_dtype(object _values):
141
142
values = values.ravel()
142
143
143
144
# try to use a valid value
144
- for i in range (n):
145
- val = util.get_value_1d(values, i)
146
- if not is_null_datetimelike(val):
147
- break
145
+ for i from 0 <= i < n:
146
+ val = util.get_value_1d(values, i)
148
147
149
- if util.is_datetime64_object(val) or val is NaT:
148
+ # do not use is_nul_datetimelike to keep
149
+ # np.datetime64('nat') and np.timedelta64('nat')
150
+ if util._checknull(val):
151
+ pass
152
+ elif val is NaT:
153
+ seen_pdnat = True
154
+ else :
155
+ seen_val = True
156
+ break
157
+
158
+ # if all values are nan/NaT
159
+ if seen_val is False and seen_pdnat is True :
160
+ return ' datetime'
161
+ # float/object nan is handled in latter logic
162
+
163
+ if util.is_datetime64_object(val):
150
164
if is_datetime64_array(values):
151
165
return ' datetime64'
152
166
elif is_timedelta_or_timedelta64_array(values):
153
167
return ' timedelta'
154
168
169
+ elif is_timedelta(val):
170
+ if is_timedelta_or_timedelta64_array(values):
171
+ return ' timedelta'
172
+
155
173
elif util.is_integer_object(val):
156
174
# a timedelta will show true here as well
157
175
if is_timedelta(val):
@@ -200,17 +218,15 @@ def infer_dtype(object _values):
200
218
if is_bytes_array(values):
201
219
return ' bytes'
202
220
203
- elif is_timedelta(val):
204
- if is_timedelta_or_timedelta64_array(values):
205
- return ' timedelta'
206
-
207
221
elif is_period(val):
208
222
if is_period_array(values):
209
223
return ' period'
210
224
211
225
for i in range (n):
212
226
val = util.get_value_1d(values, i)
213
- if util.is_integer_object(val):
227
+ if (util.is_integer_object(val) and
228
+ not util.is_timedelta64_object(val) and
229
+ not util.is_datetime64_object(val)):
214
230
return ' mixed-integer'
215
231
216
232
return ' mixed'
@@ -237,20 +253,46 @@ def is_possible_datetimelike_array(object arr):
237
253
return False
238
254
return seen_datetime or seen_timedelta
239
255
256
+
240
257
cdef inline bint is_null_datetimelike(v):
241
258
# determine if we have a null for a timedelta/datetime (or integer versions)x
242
259
if util._checknull(v):
243
260
return True
261
+ elif v is NaT:
262
+ return True
244
263
elif util.is_timedelta64_object(v):
245
264
return v.view(' int64' ) == iNaT
246
265
elif util.is_datetime64_object(v):
247
266
return v.view(' int64' ) == iNaT
248
267
elif util.is_integer_object(v):
249
268
return v == iNaT
269
+ return False
270
+
271
+
272
+ cdef inline bint is_null_datetime64(v):
273
+ # determine if we have a null for a datetime (or integer versions)x,
274
+ # excluding np.timedelta64('nat')
275
+ if util._checknull(v):
276
+ return True
277
+ elif v is NaT:
278
+ return True
279
+ elif util.is_datetime64_object(v):
280
+ return v.view(' int64' ) == iNaT
281
+ return False
282
+
283
+
284
+ cdef inline bint is_null_timedelta64(v):
285
+ # determine if we have a null for a timedelta (or integer versions)x,
286
+ # excluding np.datetime64('nat')
287
+ if util._checknull(v):
288
+ return True
250
289
elif v is NaT:
251
290
return True
291
+ elif util.is_timedelta64_object(v):
292
+ return v.view(' int64' ) == iNaT
252
293
return False
253
294
295
+
254
296
cdef inline bint is_datetime(object o):
255
297
return PyDateTime_Check(o)
256
298
@@ -420,7 +462,7 @@ def is_datetime_array(ndarray[object] values):
420
462
# return False for all nulls
421
463
for i in range (n):
422
464
v = values[i]
423
- if is_null_datetimelike (v):
465
+ if is_null_datetime64 (v):
424
466
# we are a regular null
425
467
if util._checknull(v):
426
468
null_count += 1
@@ -437,7 +479,7 @@ def is_datetime64_array(ndarray values):
437
479
# return False for all nulls
438
480
for i in range (n):
439
481
v = values[i]
440
- if is_null_datetimelike (v):
482
+ if is_null_datetime64 (v):
441
483
# we are a regular null
442
484
if util._checknull(v):
443
485
null_count += 1
@@ -481,7 +523,7 @@ def is_timedelta_array(ndarray values):
481
523
return False
482
524
for i in range (n):
483
525
v = values[i]
484
- if is_null_datetimelike (v):
526
+ if is_null_timedelta64 (v):
485
527
# we are a regular null
486
528
if util._checknull(v):
487
529
null_count += 1
@@ -496,7 +538,7 @@ def is_timedelta64_array(ndarray values):
496
538
return False
497
539
for i in range (n):
498
540
v = values[i]
499
- if is_null_datetimelike (v):
541
+ if is_null_timedelta64 (v):
500
542
# we are a regular null
501
543
if util._checknull(v):
502
544
null_count += 1
@@ -512,7 +554,7 @@ def is_timedelta_or_timedelta64_array(ndarray values):
512
554
return False
513
555
for i in range (n):
514
556
v = values[i]
515
- if is_null_datetimelike (v):
557
+ if is_null_timedelta64 (v):
516
558
# we are a regular null
517
559
if util._checknull(v):
518
560
null_count += 1
0 commit comments