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 )
@@ -220,10 +248,22 @@ def is_datetime_or_timedelta_dtype(arr_or_dtype):
220
248
221
249
def _is_unorderable_exception (e ):
222
250
"""
223
- return a boolean if we an unorderable exception error message
251
+ Check if the exception raised is an unorderable exception.
252
+
253
+ The error message differs for 3 <= PY <= 3.5 and PY >= 3.6, so
254
+ we need to condition based on Python version.
255
+
256
+ Parameters
257
+ ----------
258
+ e : Exception or sub-class
259
+ The exception object to check.
224
260
225
- These are different error message for PY>=3<=3.5 and PY>=3.6
261
+ Returns
262
+ -------
263
+ is_orderable_exc : Whether or not the exception raised is an
264
+ unorderable exception.
226
265
"""
266
+
227
267
if PY36 :
228
268
return "'>' not supported between instances of" in str (e )
229
269
@@ -346,7 +386,22 @@ def is_complex_dtype(arr_or_dtype):
346
386
347
387
348
388
def _coerce_to_dtype (dtype ):
349
- """ coerce a string / np.dtype to a dtype """
389
+ """
390
+ Coerce a string or np.dtype to a pandas or numpy
391
+ dtype if possible.
392
+
393
+ If we cannot convert to a pandas dtype initially,
394
+ we convert to a numpy dtype.
395
+
396
+ Parameters
397
+ ----------
398
+ dtype : The dtype that we want to coerce.
399
+
400
+ Returns
401
+ -------
402
+ pd_or_np_dtype : The coerced dtype.
403
+ """
404
+
350
405
if is_categorical_dtype (dtype ):
351
406
dtype = CategoricalDtype ()
352
407
elif is_datetime64tz_dtype (dtype ):
@@ -359,8 +414,27 @@ def _coerce_to_dtype(dtype):
359
414
360
415
361
416
def _get_dtype (arr_or_dtype ):
417
+ """
418
+ Get the dtype instance associated with an array
419
+ or dtype object.
420
+
421
+ Parameters
422
+ ----------
423
+ arr_or_dtype : ndarray, Series, dtype, type
424
+ The array-like or dtype object whose dtype we want to extract.
425
+
426
+ Returns
427
+ -------
428
+ obj_dtype : The extract dtype instance from the
429
+ passed in array or dtype object.
430
+
431
+ Raises
432
+ ------
433
+ TypeError : The passed in object is None.
434
+ """
435
+
362
436
if arr_or_dtype is None :
363
- raise TypeError
437
+ raise TypeError ( "Cannot deduce dtype from null object" )
364
438
if isinstance (arr_or_dtype , np .dtype ):
365
439
return arr_or_dtype
366
440
elif isinstance (arr_or_dtype , type ):
@@ -385,6 +459,21 @@ def _get_dtype(arr_or_dtype):
385
459
386
460
387
461
def _get_dtype_type (arr_or_dtype ):
462
+ """
463
+ Get the type (NOT dtype) instance associated with
464
+ an array or dtype object.
465
+
466
+ Parameters
467
+ ----------
468
+ arr_or_dtype : ndarray, Series, dtype, type
469
+ The array-like or dtype object whose type we want to extract.
470
+
471
+ Returns
472
+ -------
473
+ obj_type : The extract type instance from the
474
+ passed in array or dtype object.
475
+ """
476
+
388
477
if isinstance (arr_or_dtype , np .dtype ):
389
478
return arr_or_dtype .type
390
479
elif isinstance (arr_or_dtype , type ):
@@ -410,16 +499,27 @@ def _get_dtype_type(arr_or_dtype):
410
499
411
500
412
501
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
502
+ """
503
+ Get a numpy dtype.type-style object for a dtype object.
504
+
505
+ This methods also includes handling of the datetime64[ns] and
506
+ datetime64[ns, TZ] objects.
507
+
508
+ If no dtype can be found, we return ``object``.
509
+
510
+ Parameters
511
+ ----------
512
+ dtype : dtype, type
513
+ The dtype object whose numpy dtype.type-style
514
+ object we want to extract.
415
515
416
- Notes
417
- -----
418
- If nothing can be found, returns `` object`` .
516
+ Returns
517
+ -------
518
+ dtype_object : The extracted numpy dtype.type-style object.
419
519
"""
420
520
421
- # type object from a dtype
422
521
if isinstance (dtype , type ) and issubclass (dtype , np .generic ):
522
+ # Type object from a dtype
423
523
return dtype
424
524
elif is_categorical (dtype ):
425
525
return CategoricalDtype ().type
@@ -429,7 +529,7 @@ def _get_dtype_from_object(dtype):
429
529
try :
430
530
_validate_date_like_dtype (dtype )
431
531
except TypeError :
432
- # should still pass if we don't have a datelike
532
+ # Should still pass if we don't have a date-like
433
533
pass
434
534
return dtype .type
435
535
elif isinstance (dtype , string_types ):
@@ -444,17 +544,33 @@ def _get_dtype_from_object(dtype):
444
544
try :
445
545
return _get_dtype_from_object (getattr (np , dtype ))
446
546
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'
547
+ # Handles cases like _get_dtype(int) i.e.,
548
+ # Python objects that are valid dtypes
549
+ # (unlike user-defined types, in general)
550
+ #
551
+ # TypeError handles the float16 type code of 'e'
451
552
# further handle internal types
452
553
pass
453
554
454
555
return _get_dtype_from_object (np .dtype (dtype ))
455
556
456
557
457
558
def _validate_date_like_dtype (dtype ):
559
+ """
560
+ Check whether the dtype is a date-like dtype. Raises an error if invalid.
561
+
562
+ Parameters
563
+ ----------
564
+ dtype : dtype, type
565
+ The dtype to check.
566
+
567
+ Raises
568
+ ------
569
+ TypeError : The dtype could not be casted to a date-like dtype.
570
+ ValueError : The dtype is an illegal date-like dtype (e.g. the
571
+ the frequency provided is too specific)
572
+ """
573
+
458
574
try :
459
575
typ = np .datetime_data (dtype )[0 ]
460
576
except ValueError as e :
0 commit comments