forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinference.pyx
1411 lines (1149 loc) · 37.4 KB
/
inference.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
from decimal import Decimal
cimport util
from tslib import NaT, get_timezone
from datetime import datetime, timedelta
iNaT = util.get_nat()
cdef bint PY2 = sys.version_info[0] == 2
from util cimport (UINT8_MAX, UINT16_MAX, UINT32_MAX, UINT64_MAX,
INT8_MIN, INT8_MAX, INT16_MIN, INT16_MAX,
INT32_MAX, INT32_MIN, INT64_MAX, INT64_MIN)
# core.common import for fast inference checks
cpdef bint is_float(object obj):
return util.is_float_object(obj)
cpdef bint is_integer(object obj):
return util.is_integer_object(obj)
cpdef bint is_bool(object obj):
return util.is_bool_object(obj)
cpdef bint is_complex(object obj):
return util.is_complex_object(obj)
cpdef bint is_decimal(object obj):
return isinstance(obj, Decimal)
cpdef bint is_period(object val):
""" Return a boolean if this is a Period object """
return util.is_period_object(val)
_TYPE_MAP = {
'categorical': 'categorical',
'category': 'categorical',
'int8': 'integer',
'int16': 'integer',
'int32': 'integer',
'int64': 'integer',
'i': 'integer',
'uint8': 'integer',
'uint16': 'integer',
'uint32': 'integer',
'uint64': 'integer',
'u': 'integer',
'float32': 'floating',
'float64': 'floating',
'f': 'floating',
'complex128': 'complex',
'c': 'complex',
'string': 'string' if PY2 else 'bytes',
'S': 'string' if PY2 else 'bytes',
'unicode': 'unicode' if PY2 else 'string',
'U': 'unicode' if PY2 else 'string',
'bool': 'boolean',
'b': 'boolean',
'datetime64[ns]': 'datetime64',
'M': 'datetime64',
'timedelta64[ns]': 'timedelta64',
'm': 'timedelta64',
}
# types only exist on certain platform
try:
np.float128
_TYPE_MAP['float128'] = 'floating'
except AttributeError:
pass
try:
np.complex256
_TYPE_MAP['complex256'] = 'complex'
except AttributeError:
pass
try:
np.float16
_TYPE_MAP['float16'] = 'floating'
except AttributeError:
pass
cdef _try_infer_map(v):
""" if its in our map, just return the dtype """
cdef:
object attr, val
for attr in ['name', 'kind', 'base']:
val = getattr(v.dtype, attr)
if val in _TYPE_MAP:
return _TYPE_MAP[val]
return None
def infer_dtype(object _values):
"""
we are coercing to an ndarray here
"""
cdef:
Py_ssize_t i, n
object val
ndarray values
bint seen_pdnat = False, seen_val = False
if isinstance(_values, np.ndarray):
values = _values
elif hasattr(_values, 'dtype'):
# this will handle ndarray-like
# e.g. categoricals
try:
values = getattr(_values, '_values', getattr(
_values, 'values', _values))
except:
val = _try_infer_map(_values)
if val is not None:
return val
# its ndarray like but we can't handle
raise ValueError("cannot infer type for {0}".format(type(_values)))
else:
if not isinstance(_values, list):
_values = list(_values)
values = list_to_object_array(_values)
values = getattr(values, 'values', values)
val = _try_infer_map(values)
if val is not None:
return val
if values.dtype != np.object_:
values = values.astype('O')
n = len(values)
if n == 0:
return 'empty'
# make contiguous
values = values.ravel()
# try to use a valid value
for i from 0 <= i < n:
val = util.get_value_1d(values, i)
# do not use is_nul_datetimelike to keep
# np.datetime64('nat') and np.timedelta64('nat')
if util._checknull(val):
pass
elif val is NaT:
seen_pdnat = True
else:
seen_val = True
break
# if all values are nan/NaT
if seen_val is False and seen_pdnat is True:
return 'datetime'
# float/object nan is handled in latter logic
if util.is_datetime64_object(val):
if is_datetime64_array(values):
return 'datetime64'
elif is_timedelta_or_timedelta64_array(values):
return 'timedelta'
elif is_timedelta(val):
if is_timedelta_or_timedelta64_array(values):
return 'timedelta'
elif util.is_integer_object(val):
# a timedelta will show true here as well
if is_timedelta(val):
if is_timedelta_or_timedelta64_array(values):
return 'timedelta'
if is_integer_array(values):
return 'integer'
elif is_integer_float_array(values):
return 'mixed-integer-float'
elif is_timedelta_or_timedelta64_array(values):
return 'timedelta'
return 'mixed-integer'
elif is_datetime(val):
if is_datetime_array(values):
return 'datetime'
elif is_date(val):
if is_date_array(values):
return 'date'
elif is_time(val):
if is_time_array(values):
return 'time'
elif util.is_float_object(val):
if is_float_array(values):
return 'floating'
elif is_integer_float_array(values):
return 'mixed-integer-float'
elif util.is_bool_object(val):
if is_bool_array(values):
return 'boolean'
elif PyString_Check(val):
if is_string_array(values):
return 'string'
elif PyUnicode_Check(val):
if is_unicode_array(values):
return 'unicode'
elif PyBytes_Check(val):
if is_bytes_array(values):
return 'bytes'
elif is_period(val):
if is_period_array(values):
return 'period'
for i in range(n):
val = util.get_value_1d(values, i)
if (util.is_integer_object(val) and
not util.is_timedelta64_object(val) and
not util.is_datetime64_object(val)):
return 'mixed-integer'
return 'mixed'
cpdef bint is_possible_datetimelike_array(object arr):
# determine if we have a possible datetimelike (or null-like) array
cdef:
Py_ssize_t i, n = len(arr)
bint seen_timedelta = 0, seen_datetime = 0
object v
for i in range(n):
v = arr[i]
if util.is_string_object(v):
continue
elif util._checknull(v):
continue
elif is_datetime(v):
seen_datetime=1
elif is_timedelta(v):
seen_timedelta=1
else:
return False
return seen_datetime or seen_timedelta
cdef inline bint is_null_datetimelike(v):
# determine if we have a null for a timedelta/datetime (or integer
# versions)x
if util._checknull(v):
return True
elif v is NaT:
return True
elif util.is_timedelta64_object(v):
return v.view('int64') == iNaT
elif util.is_datetime64_object(v):
return v.view('int64') == iNaT
elif util.is_integer_object(v):
return v == iNaT
return False
cdef inline bint is_null_datetime64(v):
# determine if we have a null for a datetime (or integer versions),
# excluding np.timedelta64('nat')
if util._checknull(v):
return True
elif v is NaT:
return True
elif util.is_datetime64_object(v):
return v.view('int64') == iNaT
return False
cdef inline bint is_null_timedelta64(v):
# determine if we have a null for a timedelta (or integer versions),
# excluding np.datetime64('nat')
if util._checknull(v):
return True
elif v is NaT:
return True
elif util.is_timedelta64_object(v):
return v.view('int64') == iNaT
return False
cdef inline bint is_null_period(v):
# determine if we have a null for a Period (or integer versions),
# excluding np.datetime64('nat') and np.timedelta64('nat')
if util._checknull(v):
return True
elif v is NaT:
return True
return False
cdef inline bint is_datetime(object o):
return PyDateTime_Check(o)
cdef inline bint is_date(object o):
return PyDate_Check(o)
cdef inline bint is_time(object o):
return PyTime_Check(o)
cdef inline bint is_timedelta(object o):
return PyDelta_Check(o) or util.is_timedelta64_object(o)
cpdef bint is_bool_array(ndarray values):
cdef:
Py_ssize_t i, n = len(values)
ndarray[object] objbuf
if issubclass(values.dtype.type, np.bool_):
return True
elif values.dtype == np.object_:
objbuf = values
if n == 0:
return False
for i in range(n):
if not util.is_bool_object(objbuf[i]):
return False
return True
else:
return False
cpdef bint is_integer_array(ndarray values):
cdef:
Py_ssize_t i, n = len(values)
ndarray[object] objbuf
if issubclass(values.dtype.type, np.integer):
return True
elif values.dtype == np.object_:
objbuf = values
if n == 0:
return False
for i in range(n):
if not util.is_integer_object(objbuf[i]):
return False
return True
else:
return False
cpdef bint is_integer_float_array(ndarray values):
cdef:
Py_ssize_t i, n = len(values)
ndarray[object] objbuf
if issubclass(values.dtype.type, np.integer):
return True
elif values.dtype == np.object_:
objbuf = values
if n == 0:
return False
for i in range(n):
if not (util.is_integer_object(objbuf[i]) or
util.is_float_object(objbuf[i])):
return False
return True
else:
return False
cpdef bint is_float_array(ndarray values):
cdef:
Py_ssize_t i, n = len(values)
ndarray[object] objbuf
if issubclass(values.dtype.type, np.floating):
return True
elif values.dtype == np.object_:
objbuf = values
if n == 0:
return False
for i in range(n):
if not util.is_float_object(objbuf[i]):
return False
return True
else:
return False
cpdef bint is_string_array(ndarray values):
cdef:
Py_ssize_t i, n = len(values)
ndarray[object] objbuf
if ((PY2 and issubclass(values.dtype.type, np.string_)) or
not PY2 and issubclass(values.dtype.type, np.unicode_)):
return True
elif values.dtype == np.object_:
objbuf = values
if n == 0:
return False
for i in range(n):
if not PyString_Check(objbuf[i]):
return False
return True
else:
return False
cpdef bint is_unicode_array(ndarray values):
cdef:
Py_ssize_t i, n = len(values)
ndarray[object] objbuf
if issubclass(values.dtype.type, np.unicode_):
return True
elif values.dtype == np.object_:
objbuf = values
if n == 0:
return False
for i in range(n):
if not PyUnicode_Check(objbuf[i]):
return False
return True
else:
return False
cpdef bint is_bytes_array(ndarray values):
cdef:
Py_ssize_t i, n = len(values)
ndarray[object] objbuf
if issubclass(values.dtype.type, np.bytes_):
return True
elif values.dtype == np.object_:
objbuf = values
if n == 0:
return False
for i in range(n):
if not PyBytes_Check(objbuf[i]):
return False
return True
else:
return False
cpdef bint is_datetime_array(ndarray[object] values):
cdef Py_ssize_t i, null_count = 0, n = len(values)
cdef object v
if n == 0:
return False
# return False for all nulls
for i in range(n):
v = values[i]
if is_null_datetime64(v):
# we are a regular null
if util._checknull(v):
null_count += 1
elif not is_datetime(v):
return False
return null_count != n
cpdef bint is_datetime64_array(ndarray values):
cdef Py_ssize_t i, null_count = 0, n = len(values)
cdef object v
if n == 0:
return False
# return False for all nulls
for i in range(n):
v = values[i]
if is_null_datetime64(v):
# we are a regular null
if util._checknull(v):
null_count += 1
elif not util.is_datetime64_object(v):
return False
return null_count != n
cpdef bint is_datetime_with_singletz_array(ndarray[object] values):
"""
Check values have the same tzinfo attribute.
Doesn't check values are datetime-like types.
"""
cdef Py_ssize_t i, j, n = len(values)
cdef object base_val, base_tz, val, tz
if n == 0:
return False
for i in range(n):
base_val = values[i]
if base_val is not NaT:
base_tz = get_timezone(getattr(base_val, 'tzinfo', None))
for j in range(i, n):
val = values[j]
if val is not NaT:
tz = getattr(val, 'tzinfo', None)
if base_tz != tz and base_tz != get_timezone(tz):
return False
break
return True
cpdef bint is_timedelta_array(ndarray values):
cdef Py_ssize_t i, null_count = 0, n = len(values)
cdef object v
if n == 0:
return False
for i in range(n):
v = values[i]
if is_null_timedelta64(v):
# we are a regular null
if util._checknull(v):
null_count += 1
elif not PyDelta_Check(v):
return False
return null_count != n
cpdef bint is_timedelta64_array(ndarray values):
cdef Py_ssize_t i, null_count = 0, n = len(values)
cdef object v
if n == 0:
return False
for i in range(n):
v = values[i]
if is_null_timedelta64(v):
# we are a regular null
if util._checknull(v):
null_count += 1
elif not util.is_timedelta64_object(v):
return False
return null_count != n
cpdef bint is_timedelta_or_timedelta64_array(ndarray values):
""" infer with timedeltas and/or nat/none """
cdef Py_ssize_t i, null_count = 0, n = len(values)
cdef object v
if n == 0:
return False
for i in range(n):
v = values[i]
if is_null_timedelta64(v):
# we are a regular null
if util._checknull(v):
null_count += 1
elif not is_timedelta(v):
return False
return null_count != n
cpdef bint is_date_array(ndarray[object] values):
cdef Py_ssize_t i, n = len(values)
if n == 0:
return False
for i in range(n):
if not is_date(values[i]):
return False
return True
cpdef bint is_time_array(ndarray[object] values):
cdef Py_ssize_t i, n = len(values)
if n == 0:
return False
for i in range(n):
if not is_time(values[i]):
return False
return True
cpdef bint is_period_array(ndarray[object] values):
cdef Py_ssize_t i, null_count = 0, n = len(values)
cdef object v
if n == 0:
return False
# return False for all nulls
for i in range(n):
v = values[i]
if is_null_period(v):
# we are a regular null
if util._checknull(v):
null_count += 1
elif not is_period(v):
return False
return null_count != n
cdef extern from "parse_helper.h":
inline int floatify(object, double *result, int *maybe_int) except -1
cdef int64_t iINT64_MAX = <int64_t> INT64_MAX
cdef int64_t iINT64_MIN = <int64_t> INT64_MIN
def maybe_convert_numeric(object[:] values, set na_values,
bint convert_empty=True, bint coerce_numeric=False):
"""
Type inference function-- convert strings to numeric (potentially) and
convert to proper dtype array
"""
cdef:
int status, maybe_int
Py_ssize_t i, n = values.size
ndarray[float64_t] floats = np.empty(n, dtype='f8')
ndarray[complex128_t] complexes = np.empty(n, dtype='c16')
ndarray[int64_t] ints = np.empty(n, dtype='i8')
ndarray[uint8_t] bools = np.empty(n, dtype='u1')
bint seen_float = False
bint seen_complex = False
bint seen_int = False
bint seen_bool = False
object val
float64_t fval
for i in range(n):
val = values[i]
if val.__hash__ is not None and val in na_values:
floats[i] = complexes[i] = nan
seen_float = True
elif util.is_float_object(val):
floats[i] = complexes[i] = val
seen_float = True
elif util.is_integer_object(val):
floats[i] = ints[i] = val
seen_int = True
elif util.is_bool_object(val):
floats[i] = ints[i] = bools[i] = val
seen_bool = True
elif val is None:
floats[i] = complexes[i] = nan
seen_float = True
elif hasattr(val, '__len__') and len(val) == 0:
if convert_empty or coerce_numeric:
floats[i] = complexes[i] = nan
seen_float = True
else:
raise ValueError('Empty string encountered')
elif util.is_complex_object(val):
complexes[i] = val
seen_complex = True
elif is_decimal(val):
floats[i] = complexes[i] = val
seen_float = True
else:
try:
status = floatify(val, &fval, &maybe_int)
if fval in na_values:
floats[i] = complexes[i] = nan
seen_float = True
else:
floats[i] = fval
if not seen_float:
if maybe_int:
as_int = int(val)
if as_int <= iINT64_MAX and as_int >= iINT64_MIN:
ints[i] = as_int
else:
raise ValueError('integer out of range')
else:
seen_float = True
except (TypeError, ValueError) as e:
if not coerce_numeric:
raise type(e)(str(e) + ' at position {}'.format(i))
floats[i] = nan
seen_float = True
if seen_complex:
return complexes
elif seen_float:
return floats
elif seen_int:
return ints
elif seen_bool:
return bools.view(np.bool_)
return ints
def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
bint safe=0, bint convert_datetime=0,
bint convert_timedelta=0):
"""
Type inference function-- convert object array to proper dtype
"""
cdef:
Py_ssize_t i, n
ndarray[float64_t] floats
ndarray[complex128_t] complexes
ndarray[int64_t] ints
ndarray[uint8_t] bools
ndarray[int64_t] idatetimes
ndarray[int64_t] itimedeltas
bint seen_float = 0
bint seen_complex = 0
bint seen_datetime = 0
bint seen_datetimetz = 0
bint seen_timedelta = 0
bint seen_int = 0
bint seen_bool = 0
bint seen_object = 0
bint seen_null = 0
bint seen_numeric = 0
object val, onan
float64_t fval, fnan
n = len(objects)
floats = np.empty(n, dtype='f8')
complexes = np.empty(n, dtype='c16')
ints = np.empty(n, dtype='i8')
bools = np.empty(n, dtype=np.uint8)
if convert_datetime:
datetimes = np.empty(n, dtype='M8[ns]')
idatetimes = datetimes.view(np.int64)
if convert_timedelta:
timedeltas = np.empty(n, dtype='m8[ns]')
itimedeltas = timedeltas.view(np.int64)
onan = np.nan
fnan = np.nan
for i from 0 <= i < n:
val = objects[i]
if val is None:
seen_null = 1
floats[i] = complexes[i] = fnan
elif val is NaT:
if convert_datetime:
idatetimes[i] = iNaT
seen_datetime = 1
if convert_timedelta:
itimedeltas[i] = iNaT
seen_timedelta = 1
if not (convert_datetime or convert_timedelta):
seen_object = 1
elif util.is_bool_object(val):
seen_bool = 1
bools[i] = val
elif util.is_float_object(val):
floats[i] = complexes[i] = val
seen_float = 1
elif util.is_datetime64_object(val):
if convert_datetime:
idatetimes[i] = convert_to_tsobject(
val, None, None, 0, 0).value
seen_datetime = 1
else:
seen_object = 1
# objects[i] = val.astype('O')
break
elif is_timedelta(val):
if convert_timedelta:
itimedeltas[i] = convert_to_timedelta64(val, 'ns')
seen_timedelta = 1
else:
seen_object = 1
break
elif util.is_integer_object(val):
seen_int = 1
floats[i] = <float64_t> val
complexes[i] = <double complex> val
if not seen_null:
try:
ints[i] = val
except OverflowError:
seen_object = 1
break
elif util.is_complex_object(val):
complexes[i] = val
seen_complex = 1
elif PyDateTime_Check(val) or util.is_datetime64_object(val):
# if we have an tz's attached then return the objects
if convert_datetime:
if getattr(val, 'tzinfo', None) is not None:
seen_datetimetz = 1
break
else:
seen_datetime = 1
idatetimes[i] = convert_to_tsobject(
val, None, None, 0, 0).value
else:
seen_object = 1
break
elif try_float and not util.is_string_object(val):
# this will convert Decimal objects
try:
floats[i] = float(val)
complexes[i] = complex(val)
seen_float = 1
except Exception:
seen_object = 1
break
else:
seen_object = 1
break
seen_numeric = seen_complex or seen_float or seen_int
# we try to coerce datetime w/tz but must all have the same tz
if seen_datetimetz:
if len(set([ getattr(val, 'tz', None) for val in objects ])) == 1:
from pandas import DatetimeIndex
return DatetimeIndex(objects)
seen_object = 1
if not seen_object:
if not safe:
if seen_null:
if not seen_bool and not seen_datetime and not seen_timedelta:
if seen_complex:
return complexes
elif seen_float or seen_int:
return floats
else:
if not seen_bool:
if seen_datetime:
if not seen_numeric:
return datetimes
elif seen_timedelta:
if not seen_numeric:
return timedeltas
else:
if seen_complex:
return complexes
elif seen_float:
return floats
elif seen_int:
return ints
elif (not seen_datetime and not seen_numeric
and not seen_timedelta):
return bools.view(np.bool_)
else:
# don't cast int to float, etc.
if seen_null:
if not seen_bool and not seen_datetime and not seen_timedelta:
if seen_complex:
if not seen_int:
return complexes
elif seen_float:
if not seen_int:
return floats
else:
if not seen_bool:
if seen_datetime:
if not seen_numeric:
return datetimes
elif seen_timedelta:
if not seen_numeric:
return timedeltas
else:
if seen_complex:
if not seen_int:
return complexes
elif seen_float:
if not seen_int:
return floats
elif seen_int:
return ints
elif (not seen_datetime and not seen_numeric
and not seen_timedelta):
return bools.view(np.bool_)
return objects
def convert_sql_column(x):
return maybe_convert_objects(x, try_float=1)
def try_parse_dates(ndarray[object] values, parser=None,
dayfirst=False, default=None):
cdef:
Py_ssize_t i, n
ndarray[object] result
n = len(values)
result = np.empty(n, dtype='O')
if parser is None:
if default is None: # GH2618
date=datetime.now()
default=datetime(date.year, date.month, 1)
try:
from dateutil.parser import parse
parse_date = lambda x: parse(x, dayfirst=dayfirst, default=default)
except ImportError: # pragma: no cover
def parse_date(s):
try:
return datetime.strptime(s, '%m/%d/%Y')
except Exception:
return s
# EAFP here
try:
for i from 0 <= i < n:
if values[i] == '':
result[i] = np.nan
else:
result[i] = parse_date(values[i])
except Exception:
# failed
return values
else:
parse_date = parser
try:
for i from 0 <= i < n:
if values[i] == '':
result[i] = np.nan
else:
result[i] = parse_date(values[i])
except Exception:
# raise if passed parser and it failed
raise
return result
def try_parse_date_and_time(ndarray[object] dates, ndarray[object] times,
date_parser=None, time_parser=None,
dayfirst=False, default=None):
cdef:
Py_ssize_t i, n
ndarray[object] result
from datetime import date, time, datetime, timedelta
n = len(dates)
if len(times) != n:
raise ValueError('Length of dates and times must be equal')
result = np.empty(n, dtype='O')
if date_parser is None:
if default is None: # GH2618
date=datetime.now()
default=datetime(date.year, date.month, 1)
try:
from dateutil.parser import parse
parse_date = lambda x: parse(x, dayfirst=dayfirst, default=default)
except ImportError: # pragma: no cover
def parse_date(s):
try:
return date.strptime(s, '%m/%d/%Y')
except Exception:
return s
else:
parse_date = date_parser
if time_parser is None:
try:
from dateutil.parser import parse
parse_time = lambda x: parse(x)