31
31
32
32
33
33
def _ensure_float (arr ):
34
+ """
35
+ Ensure that an array object has a float dtype if possible.
36
+
37
+ Parameters
38
+ ----------
39
+ arr : ndarray, Series
40
+ The array whose data type we want to enforce as float.
41
+
42
+ Returns
43
+ -------
44
+ float_arr : The original array cast to the float dtype if
45
+ possible. Otherwise, the original array is returned.
46
+ """
47
+
34
48
if issubclass (arr .dtype .type , (np .integer , np .bool_ )):
35
49
arr = arr .astype (float )
36
50
return arr
@@ -46,6 +60,20 @@ def _ensure_float(arr):
46
60
47
61
48
62
def _ensure_categorical (arr ):
63
+ """
64
+ Ensure that an array-like object is a Categorical (if not already).
65
+
66
+ Parameters
67
+ ----------
68
+ arr : array-like
69
+ The array that we want to convert into a Categorical.
70
+
71
+ Returns
72
+ -------
73
+ cat_arr : The original array cast as a Categorical. If it already
74
+ is a Categorical, we return as is.
75
+ """
76
+
49
77
if not is_categorical (arr ):
50
78
from pandas import Categorical
51
79
arr = Categorical (arr )
@@ -116,8 +144,40 @@ def is_categorical_dtype(arr_or_dtype):
116
144
117
145
118
146
def is_string_dtype (arr_or_dtype ):
119
- dtype = _get_dtype (arr_or_dtype )
120
- return dtype .kind in ('O' , 'S' , 'U' ) and not is_period_dtype (dtype )
147
+ """
148
+ Check whether the provided array or dtype is of the string dtype.
149
+
150
+ Parameters
151
+ ----------
152
+ arr_or_dtype : ndarray, dtype, type
153
+ The array or dtype to check.
154
+
155
+ Returns
156
+ -------
157
+ boolean : Whether or not the array or dtype is of the string dtype.
158
+
159
+ Examples
160
+ --------
161
+ >>> is_string_dtype(str)
162
+ True
163
+ >>> is_string_dtype(object)
164
+ True
165
+ >>> is_string_dtype(int)
166
+ False
167
+ >>>
168
+ >>> is_string_dtype(np.array(['a', 'b']))
169
+ True
170
+ >>> is_string_dtype(np.array([1, 2]))
171
+ False
172
+ """
173
+
174
+ # TODO: gh-15585: consider making the checks stricter.
175
+
176
+ try :
177
+ dtype = _get_dtype (arr_or_dtype )
178
+ return dtype .kind in ('O' , 'S' , 'U' ) and not is_period_dtype (dtype )
179
+ except TypeError :
180
+ return False
121
181
122
182
123
183
def is_period_arraylike (arr ):
@@ -209,8 +269,40 @@ def is_datetime64_ns_dtype(arr_or_dtype):
209
269
210
270
211
271
def is_timedelta64_ns_dtype (arr_or_dtype ):
212
- tipo = _get_dtype (arr_or_dtype )
213
- return tipo == _TD_DTYPE
272
+ """
273
+ Check whether the provided array or dtype is of the timedelta64[ns] dtype.
274
+
275
+ This is a very specific dtype, so generic ones like `np.timedelta64`
276
+ will return False if passed into this function.
277
+
278
+ Parameters
279
+ ----------
280
+ arr_or_dtype : ndarray, dtype, type
281
+ The array or dtype to check.
282
+
283
+ Returns
284
+ -------
285
+ boolean : Whether or not the array or dtype
286
+ is of the timedelta64[ns] dtype.
287
+
288
+ Examples
289
+ --------
290
+ >>> is_timedelta64_ns_dtype(np.dtype('m8[ns]')
291
+ True
292
+ >>> is_timedelta64_ns_dtype(np.dtype('m8[ps]') # Wrong frequency
293
+ False
294
+ >>>
295
+ >>> is_timedelta64_ns_dtype(np.array([1, 2], dtype='m8[ns]'))
296
+ True
297
+ >>> is_timedelta64_ns_dtype(np.array([1, 2], dtype=np.timedelta64))
298
+ False
299
+ """
300
+
301
+ try :
302
+ tipo = _get_dtype (arr_or_dtype )
303
+ return tipo == _TD_DTYPE
304
+ except TypeError :
305
+ return False
214
306
215
307
216
308
def is_datetime_or_timedelta_dtype (arr_or_dtype ):
@@ -220,10 +312,21 @@ def is_datetime_or_timedelta_dtype(arr_or_dtype):
220
312
221
313
def _is_unorderable_exception (e ):
222
314
"""
223
- return a boolean if we an unorderable exception error message
315
+ Check if the exception raised is an unorderable exception.
224
316
225
- These are different error message for PY>=3<=3.5 and PY>=3.6
317
+ The error message differs for 3 <= PY <= 3.5 and PY >= 3.6, so
318
+ we need to condition based on Python version.
319
+
320
+ Parameters
321
+ ----------
322
+ e : Exception or sub-class
323
+ The exception object to check.
324
+
325
+ Returns
326
+ -------
327
+ boolean : Whether or not the exception raised is an unorderable exception.
226
328
"""
329
+
227
330
if PY36 :
228
331
return "'>' not supported between instances of" in str (e )
229
332
@@ -302,9 +405,39 @@ def is_numeric_dtype(arr_or_dtype):
302
405
303
406
304
407
def is_string_like_dtype (arr_or_dtype ):
305
- # exclude object as its a mixed dtype
306
- dtype = _get_dtype (arr_or_dtype )
307
- return dtype .kind in ('S' , 'U' )
408
+ """
409
+ Check whether the provided array or dtype is of a string-like dtype.
410
+
411
+ Unlike `is_string_dtype`, the object dtype is excluded because it
412
+ is a mixed dtype.
413
+
414
+ Parameters
415
+ ----------
416
+ arr_or_dtype : ndarray, dtype, type
417
+ The array or dtype to check.
418
+
419
+ Returns
420
+ -------
421
+ boolean : Whether or not the array or dtype is of the string dtype.
422
+
423
+ Examples
424
+ --------
425
+ >>> is_string_like_dtype(str)
426
+ True
427
+ >>> is_string_like_dtype(object)
428
+ False
429
+ >>>
430
+ >>> is_string_like_dtype(np.array(['a', 'b']))
431
+ True
432
+ >>> is_string_like_dtype(np.array([1, 2]))
433
+ False
434
+ """
435
+
436
+ try :
437
+ dtype = _get_dtype (arr_or_dtype )
438
+ return dtype .kind in ('S' , 'U' )
439
+ except TypeError :
440
+ return False
308
441
309
442
310
443
def is_float_dtype (arr_or_dtype ):
@@ -346,7 +479,22 @@ def is_complex_dtype(arr_or_dtype):
346
479
347
480
348
481
def _coerce_to_dtype (dtype ):
349
- """ coerce a string / np.dtype to a dtype """
482
+ """
483
+ Coerce a string or np.dtype to a pandas or numpy
484
+ dtype if possible.
485
+
486
+ If we cannot convert to a pandas dtype initially,
487
+ we convert to a numpy dtype.
488
+
489
+ Parameters
490
+ ----------
491
+ dtype : The dtype that we want to coerce.
492
+
493
+ Returns
494
+ -------
495
+ pd_or_np_dtype : The coerced dtype.
496
+ """
497
+
350
498
if is_categorical_dtype (dtype ):
351
499
dtype = CategoricalDtype ()
352
500
elif is_datetime64tz_dtype (dtype ):
@@ -359,8 +507,27 @@ def _coerce_to_dtype(dtype):
359
507
360
508
361
509
def _get_dtype (arr_or_dtype ):
510
+ """
511
+ Get the dtype instance associated with an array
512
+ or dtype object.
513
+
514
+ Parameters
515
+ ----------
516
+ arr_or_dtype : ndarray, Series, dtype, type
517
+ The array-like or dtype object whose dtype we want to extract.
518
+
519
+ Returns
520
+ -------
521
+ obj_dtype : The extract dtype instance from the
522
+ passed in array or dtype object.
523
+
524
+ Raises
525
+ ------
526
+ TypeError : The passed in object is None.
527
+ """
528
+
362
529
if arr_or_dtype is None :
363
- raise TypeError
530
+ raise TypeError ( "Cannot deduce dtype from null object" )
364
531
if isinstance (arr_or_dtype , np .dtype ):
365
532
return arr_or_dtype
366
533
elif isinstance (arr_or_dtype , type ):
@@ -385,6 +552,21 @@ def _get_dtype(arr_or_dtype):
385
552
386
553
387
554
def _get_dtype_type (arr_or_dtype ):
555
+ """
556
+ Get the type (NOT dtype) instance associated with
557
+ an array or dtype object.
558
+
559
+ Parameters
560
+ ----------
561
+ arr_or_dtype : ndarray, Series, dtype, type
562
+ The array-like or dtype object whose type we want to extract.
563
+
564
+ Returns
565
+ -------
566
+ obj_type : The extract type instance from the
567
+ passed in array or dtype object.
568
+ """
569
+
388
570
if isinstance (arr_or_dtype , np .dtype ):
389
571
return arr_or_dtype .type
390
572
elif isinstance (arr_or_dtype , type ):
@@ -410,16 +592,27 @@ def _get_dtype_type(arr_or_dtype):
410
592
411
593
412
594
def _get_dtype_from_object (dtype ):
413
- """Get a numpy dtype.type-style object. This handles the datetime64[ns]
414
- and datetime64[ns, TZ] compat
595
+ """
596
+ Get a numpy dtype.type-style object for a dtype object.
415
597
416
- Notes
417
- -----
418
- If nothing can be found, returns ``object``.
598
+ This methods also includes handling of the datetime64[ns] and
599
+ datetime64[ns, TZ] objects.
600
+
601
+ If no dtype can be found, we return ``object``.
602
+
603
+ Parameters
604
+ ----------
605
+ dtype : dtype, type
606
+ The dtype object whose numpy dtype.type-style
607
+ object we want to extract.
608
+
609
+ Returns
610
+ -------
611
+ dtype_object : The extracted numpy dtype.type-style object.
419
612
"""
420
613
421
- # type object from a dtype
422
614
if isinstance (dtype , type ) and issubclass (dtype , np .generic ):
615
+ # Type object from a dtype
423
616
return dtype
424
617
elif is_categorical (dtype ):
425
618
return CategoricalDtype ().type
@@ -429,7 +622,7 @@ def _get_dtype_from_object(dtype):
429
622
try :
430
623
_validate_date_like_dtype (dtype )
431
624
except TypeError :
432
- # should still pass if we don't have a datelike
625
+ # Should still pass if we don't have a date-like
433
626
pass
434
627
return dtype .type
435
628
elif isinstance (dtype , string_types ):
@@ -444,17 +637,33 @@ def _get_dtype_from_object(dtype):
444
637
try :
445
638
return _get_dtype_from_object (getattr (np , dtype ))
446
639
except (AttributeError , TypeError ):
447
- # handles cases like _get_dtype(int)
448
- # i.e., python objects that are valid dtypes (unlike user-defined
449
- # types, in general)
450
- # TypeError handles the float16 typecode of 'e'
640
+ # Handles cases like _get_dtype(int) i.e.,
641
+ # Python objects that are valid dtypes
642
+ # (unlike user-defined types, in general)
643
+ #
644
+ # TypeError handles the float16 type code of 'e'
451
645
# further handle internal types
452
646
pass
453
647
454
648
return _get_dtype_from_object (np .dtype (dtype ))
455
649
456
650
457
651
def _validate_date_like_dtype (dtype ):
652
+ """
653
+ Check whether the dtype is a date-like dtype. Raises an error if invalid.
654
+
655
+ Parameters
656
+ ----------
657
+ dtype : dtype, type
658
+ The dtype to check.
659
+
660
+ Raises
661
+ ------
662
+ TypeError : The dtype could not be casted to a date-like dtype.
663
+ ValueError : The dtype is an illegal date-like dtype (e.g. the
664
+ the frequency provided is too specific)
665
+ """
666
+
458
667
try :
459
668
typ = np .datetime_data (dtype )[0 ]
460
669
except ValueError as e :
0 commit comments