Skip to content

Commit e9e2a7f

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

File tree

1 file changed

+55
-3
lines changed

1 file changed

+55
-3
lines changed

pandas/types/common.py

+55-3
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
388+
pandas or numpy dtype if possible.
389+
390+
If we cannot convert to a pandas dtype
391+
initially, 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):

0 commit comments

Comments
 (0)