@@ -427,86 +427,91 @@ cdef extern from "parse_helper.h":
427
427
cdef double fINT64_MAX = < double > INT64_MAX
428
428
cdef double fINT64_MIN = < double > INT64_MIN
429
429
430
- def maybe_convert_numeric (ndarray[object] values , set na_values ,
431
- convert_empty = True , coerce_numeric = False ):
430
+
431
+ def maybe_convert_numeric (object[:] values , set na_values ,
432
+ bint convert_empty = True , bint coerce_numeric = False ):
432
433
'''
433
434
Type inference function-- convert strings to numeric (potentially) and
434
435
convert to proper dtype array
435
436
'''
436
437
cdef:
437
438
int status
438
- Py_ssize_t i, n
439
- ndarray[float64_t] floats
440
- ndarray[complex128_t] complexes
441
- ndarray[int64_t] ints
442
- bint seen_float = 0
443
- bint seen_complex = 0
439
+ Py_ssize_t i, n = values.size
440
+ ndarray[float64_t] floats = np.empty(n, dtype = ' f8' )
441
+ ndarray[complex128_t] complexes = np.empty(n, dtype = ' c16' )
442
+ ndarray[int64_t] ints = np.empty(n, dtype = ' i8' )
443
+ ndarray[uint8_t] bools = np.empty(n, dtype = ' u1' )
444
+ bint seen_float = False
445
+ bint seen_complex = False
446
+ bint seen_int = False
447
+ bint seen_bool = False
444
448
object val
445
449
float64_t fval
446
450
447
- n = len (values)
448
-
449
- floats = np.empty(n, dtype = ' f8' )
450
- complexes = np.empty(n, dtype = ' c16' )
451
- ints = np.empty(n, dtype = ' i8' )
452
-
453
- for i from 0 <= i < n:
451
+ for i in range (n):
454
452
val = values[i]
455
453
456
454
if val in na_values:
457
455
floats[i] = complexes[i] = nan
458
- seen_float = 1
456
+ seen_float = True
459
457
elif util.is_float_object(val):
460
458
floats[i] = complexes[i] = val
461
- seen_float = 1
459
+ seen_float = True
462
460
elif util.is_integer_object(val):
463
461
floats[i] = ints[i] = val
464
- seen_int = 1
462
+ seen_int = True
463
+ elif util.is_bool_object(val):
464
+ floats[i] = ints[i] = bools[i] = val
465
+ seen_bool = True
465
466
elif val is None :
466
467
floats[i] = complexes[i] = nan
467
- seen_float = 1
468
- elif hasattr (val,' __len__' ) and len (val) == 0 :
468
+ seen_float = True
469
+ elif hasattr (val, ' __len__' ) and len (val) == 0 :
469
470
if convert_empty or coerce_numeric:
470
471
floats[i] = complexes[i] = nan
471
- seen_float = 1
472
+ seen_float = True
472
473
else :
473
474
raise ValueError (' Empty string encountered' )
474
475
elif util.is_complex_object(val):
475
476
complexes[i] = val
476
- seen_complex = 1
477
+ seen_complex = True
477
478
else :
478
479
try :
479
480
status = floatify(val, & fval)
480
481
floats[i] = fval
481
482
if not seen_float:
482
483
if ' .' in val or fval == INF or fval == NEGINF:
483
- seen_float = 1
484
+ seen_float = True
484
485
elif ' inf' in val: # special case to handle +/-inf
485
- seen_float = 1
486
+ seen_float = True
486
487
elif fval < fINT64_MAX and fval > fINT64_MIN:
487
488
try :
488
489
ints[i] = int (val)
489
490
except ValueError :
490
491
ints[i] = < int64_t> fval
491
492
else :
492
- seen_float = 1
493
+ seen_float = True
493
494
except :
494
495
if not coerce_numeric:
495
496
raise
496
497
497
498
floats[i] = nan
498
- seen_float = 1
499
-
499
+ seen_float = True
500
500
501
501
if seen_complex:
502
502
return complexes
503
503
elif seen_float:
504
504
return floats
505
- else :
505
+ elif seen_int :
506
506
return ints
507
+ elif seen_bool:
508
+ return bools.view(np.bool_)
509
+ return ints
510
+
507
511
508
512
def maybe_convert_objects (ndarray[object] objects , bint try_float = 0 ,
509
- bint safe = 0 , bint convert_datetime = 0 , bint convert_timedelta = 0 ):
513
+ bint safe = 0 , bint convert_datetime = 0 ,
514
+ bint convert_timedelta = 0 ):
510
515
'''
511
516
Type inference function-- convert object array to proper dtype
512
517
'''
0 commit comments