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 : The array whose data type we want to enforce as float.
40
+
41
+ Returns
42
+ -------
43
+ float_arr : The original array cast to the float dtype if
44
+ possible. Otherwise, the original array is returned.
45
+ """
46
+
34
47
if issubclass (arr .dtype .type , (np .integer , np .bool_ )):
35
48
arr = arr .astype (float )
36
49
return arr
@@ -46,6 +59,19 @@ def _ensure_float(arr):
46
59
47
60
48
61
def _ensure_categorical (arr ):
62
+ """
63
+ Ensure that an array object is a Categorical (if not already).
64
+
65
+ Parameters
66
+ ----------
67
+ arr : The array that we want to convert into a Categorical.
68
+
69
+ Returns
70
+ -------
71
+ cat_arr : The original array cast as a Categorical. If it already
72
+ is a Categorical, we return as is.
73
+ """
74
+
49
75
if not is_categorical (arr ):
50
76
from pandas import Categorical
51
77
arr = Categorical (arr )
@@ -220,10 +246,21 @@ def is_datetime_or_timedelta_dtype(arr_or_dtype):
220
246
221
247
def _is_unorderable_exception (e ):
222
248
"""
223
- return a boolean if we an unorderable exception error message
249
+ Check if the exception raised is an unorderable exception.
250
+
251
+ The error message differs for 3 <= PY <= 3.5 and PY >= 3.6, so
252
+ we need to condition based on Python version.
253
+
254
+ Parameters
255
+ ----------
256
+ e : The error message to check.
224
257
225
- These are different error message for PY>=3<=3.5 and PY>=3.6
258
+ Returns
259
+ -------
260
+ is_orderable_exc : Whether or not the exception raised is an
261
+ unorderable exception.
226
262
"""
263
+
227
264
if PY36 :
228
265
return "'>' not supported between instances of" in str (e )
229
266
@@ -346,7 +383,22 @@ def is_complex_dtype(arr_or_dtype):
346
383
347
384
348
385
def _coerce_to_dtype (dtype ):
349
- """ coerce a string / np.dtype to a dtype """
386
+ """
387
+ Coerce a string or np.dtype to a pandas or numpy
388
+ dtype if possible.
389
+
390
+ If we cannot convert to a pandas dtype initially,
391
+ we convert to a numpy dtype.
392
+
393
+ Parameters
394
+ ----------
395
+ dtype : The dtype that we want to coerce.
396
+
397
+ Returns
398
+ -------
399
+ pd_or_np_dtype : The coerced dtype.
400
+ """
401
+
350
402
if is_categorical_dtype (dtype ):
351
403
dtype = CategoricalDtype ()
352
404
elif is_datetime64tz_dtype (dtype ):
@@ -359,8 +411,27 @@ def _coerce_to_dtype(dtype):
359
411
360
412
361
413
def _get_dtype (arr_or_dtype ):
414
+ """
415
+ Get the dtype instance associated with an array
416
+ or dtype object.
417
+
418
+ Parameters
419
+ ----------
420
+ arr_or_dtype : The array-like or dtype object
421
+ whose dtype we want to extract.
422
+
423
+ Returns
424
+ -------
425
+ obj_dtype : The extract dtype instance from the
426
+ passed in array or dtype object.
427
+
428
+ Raises
429
+ ------
430
+ TypeError : The passed in object is None.
431
+ """
432
+
362
433
if arr_or_dtype is None :
363
- raise TypeError
434
+ raise TypeError ( "Cannot deduce dtype from null object" )
364
435
if isinstance (arr_or_dtype , np .dtype ):
365
436
return arr_or_dtype
366
437
elif isinstance (arr_or_dtype , type ):
@@ -385,6 +456,21 @@ def _get_dtype(arr_or_dtype):
385
456
386
457
387
458
def _get_dtype_type (arr_or_dtype ):
459
+ """
460
+ Get the type (NOT dtype) instance associated with
461
+ an array or dtype object.
462
+
463
+ Parameters
464
+ ----------
465
+ arr_or_dtype : The array-like or dtype object
466
+ whose type we want to extract.
467
+
468
+ Returns
469
+ -------
470
+ obj_type : The extract type instance from the
471
+ passed in array or dtype object.
472
+ """
473
+
388
474
if isinstance (arr_or_dtype , np .dtype ):
389
475
return arr_or_dtype .type
390
476
elif isinstance (arr_or_dtype , type ):
@@ -410,16 +496,26 @@ def _get_dtype_type(arr_or_dtype):
410
496
411
497
412
498
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
499
+ """
500
+ Get a numpy dtype.type-style object for a dtype object.
415
501
416
- Notes
417
- -----
418
- If nothing can be found, returns ``object``.
502
+ This methods also includes handling of the datetime64[ns] and
503
+ datetime64[ns, TZ] objects.
504
+
505
+ If no dtype can be found, we return ``object``.
506
+
507
+ Parameters
508
+ ----------
509
+ dtype : The dtype object whose numpy dtype.type-style object
510
+ we want to extract.
511
+
512
+ Returns
513
+ -------
514
+ dtype_object : The extracted numpy dtype.type-style object.
419
515
"""
420
516
421
- # type object from a dtype
422
517
if isinstance (dtype , type ) and issubclass (dtype , np .generic ):
518
+ # Type object from a dtype
423
519
return dtype
424
520
elif is_categorical (dtype ):
425
521
return CategoricalDtype ().type
@@ -429,7 +525,7 @@ def _get_dtype_from_object(dtype):
429
525
try :
430
526
_validate_date_like_dtype (dtype )
431
527
except TypeError :
432
- # should still pass if we don't have a datelike
528
+ # Should still pass if we don't have a date-like
433
529
pass
434
530
return dtype .type
435
531
elif isinstance (dtype , string_types ):
@@ -444,10 +540,11 @@ def _get_dtype_from_object(dtype):
444
540
try :
445
541
return _get_dtype_from_object (getattr (np , dtype ))
446
542
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'
543
+ # Handles cases like _get_dtype(int) i.e.,
544
+ # Python objects that are valid dtypes
545
+ # (unlike user-defined types, in general)
546
+ #
547
+ # TypeError handles the float16 type code of 'e'
451
548
# further handle internal types
452
549
pass
453
550
0 commit comments