Skip to content

Commit 568812d

Browse files
committed
DOC: document pandas.types.common
Closes pandas-devgh-15895.
1 parent d9e00d2 commit 568812d

File tree

1 file changed

+90
-4
lines changed

1 file changed

+90
-4
lines changed

pandas/types/common.py

+90-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@
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 : 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+
3447
if issubclass(arr.dtype.type, (np.integer, np.bool_)):
3548
arr = arr.astype(float)
3649
return arr
@@ -46,6 +59,19 @@ def _ensure_float(arr):
4659

4760

4861
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+
4975
if not is_categorical(arr):
5076
from pandas import Categorical
5177
arr = Categorical(arr)
@@ -220,10 +246,21 @@ def is_datetime_or_timedelta_dtype(arr_or_dtype):
220246

221247
def _is_unorderable_exception(e):
222248
"""
223-
return a boolean if we an unorderable exception error message
249+
Check if the exception raised is an unorderable exception.
224250
225-
These are different error message for PY>=3<=3.5 and PY>=3.6
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.
257+
258+
Returns
259+
-------
260+
is_orderable_exc : Whether or not the exception raised is an
261+
unorderable exception.
226262
"""
263+
227264
if PY36:
228265
return "'>' not supported between instances of" in str(e)
229266

@@ -346,7 +383,22 @@ def is_complex_dtype(arr_or_dtype):
346383

347384

348385
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+
350402
if is_categorical_dtype(dtype):
351403
dtype = CategoricalDtype()
352404
elif is_datetime64tz_dtype(dtype):
@@ -359,8 +411,27 @@ def _coerce_to_dtype(dtype):
359411

360412

361413
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+
362433
if arr_or_dtype is None:
363-
raise TypeError
434+
raise TypeError("Cannot deduce dtype from null object")
364435
if isinstance(arr_or_dtype, np.dtype):
365436
return arr_or_dtype
366437
elif isinstance(arr_or_dtype, type):
@@ -385,6 +456,21 @@ def _get_dtype(arr_or_dtype):
385456

386457

387458
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+
388474
if isinstance(arr_or_dtype, np.dtype):
389475
return arr_or_dtype.type
390476
elif isinstance(arr_or_dtype, type):

0 commit comments

Comments
 (0)