Skip to content

Commit 327d561

Browse files
committed
DOC: document internal methods in types/common.py
Partially addresses gh-15895.
1 parent 5d17a94 commit 327d561

File tree

1 file changed

+131
-15
lines changed

1 file changed

+131
-15
lines changed

pandas/types/common.py

+131-15
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@
3131

3232

3333
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+
3448
if issubclass(arr.dtype.type, (np.integer, np.bool_)):
3549
arr = arr.astype(float)
3650
return arr
@@ -46,6 +60,20 @@ def _ensure_float(arr):
4660

4761

4862
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+
4977
if not is_categorical(arr):
5078
from pandas import Categorical
5179
arr = Categorical(arr)
@@ -220,10 +248,22 @@ def is_datetime_or_timedelta_dtype(arr_or_dtype):
220248

221249
def _is_unorderable_exception(e):
222250
"""
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.
224260
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.
226265
"""
266+
227267
if PY36:
228268
return "'>' not supported between instances of" in str(e)
229269

@@ -346,7 +386,22 @@ def is_complex_dtype(arr_or_dtype):
346386

347387

348388
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+
350405
if is_categorical_dtype(dtype):
351406
dtype = CategoricalDtype()
352407
elif is_datetime64tz_dtype(dtype):
@@ -359,8 +414,27 @@ def _coerce_to_dtype(dtype):
359414

360415

361416
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+
362436
if arr_or_dtype is None:
363-
raise TypeError
437+
raise TypeError("Cannot deduce dtype from null object")
364438
if isinstance(arr_or_dtype, np.dtype):
365439
return arr_or_dtype
366440
elif isinstance(arr_or_dtype, type):
@@ -385,6 +459,21 @@ def _get_dtype(arr_or_dtype):
385459

386460

387461
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+
388477
if isinstance(arr_or_dtype, np.dtype):
389478
return arr_or_dtype.type
390479
elif isinstance(arr_or_dtype, type):
@@ -410,16 +499,27 @@ def _get_dtype_type(arr_or_dtype):
410499

411500

412501
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.
415515
416-
Notes
417-
-----
418-
If nothing can be found, returns ``object``.
516+
Returns
517+
-------
518+
dtype_object : The extracted numpy dtype.type-style object.
419519
"""
420520

421-
# type object from a dtype
422521
if isinstance(dtype, type) and issubclass(dtype, np.generic):
522+
# Type object from a dtype
423523
return dtype
424524
elif is_categorical(dtype):
425525
return CategoricalDtype().type
@@ -429,7 +529,7 @@ def _get_dtype_from_object(dtype):
429529
try:
430530
_validate_date_like_dtype(dtype)
431531
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
433533
pass
434534
return dtype.type
435535
elif isinstance(dtype, string_types):
@@ -444,17 +544,33 @@ def _get_dtype_from_object(dtype):
444544
try:
445545
return _get_dtype_from_object(getattr(np, dtype))
446546
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'
451552
# further handle internal types
452553
pass
453554

454555
return _get_dtype_from_object(np.dtype(dtype))
455556

456557

457558
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+
458574
try:
459575
typ = np.datetime_data(dtype)[0]
460576
except ValueError as e:

0 commit comments

Comments
 (0)