Skip to content

Commit 8e8b236

Browse files
committed
DOC: document pandas.types.common
Partially addresses pandas-devgh-15895.
1 parent d9e00d2 commit 8e8b236

File tree

1 file changed

+126
-15
lines changed

1 file changed

+126
-15
lines changed

pandas/types/common.py

+126-15
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):
@@ -410,16 +496,26 @@ def _get_dtype_type(arr_or_dtype):
410496

411497

412498
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.
501+
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``.
415506
416-
Notes
417-
-----
418-
If nothing can be found, returns ``object``.
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.
419515
"""
420516

421-
# type object from a dtype
422517
if isinstance(dtype, type) and issubclass(dtype, np.generic):
518+
# Type object from a dtype
423519
return dtype
424520
elif is_categorical(dtype):
425521
return CategoricalDtype().type
@@ -429,7 +525,7 @@ def _get_dtype_from_object(dtype):
429525
try:
430526
_validate_date_like_dtype(dtype)
431527
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
433529
pass
434530
return dtype.type
435531
elif isinstance(dtype, string_types):
@@ -444,17 +540,32 @@ def _get_dtype_from_object(dtype):
444540
try:
445541
return _get_dtype_from_object(getattr(np, dtype))
446542
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'
451548
# further handle internal types
452549
pass
453550

454551
return _get_dtype_from_object(np.dtype(dtype))
455552

456553

457554
def _validate_date_like_dtype(dtype):
555+
"""
556+
Check whether the dtype is a date-like dtype. Raises an error if invalid.
557+
558+
Parameters
559+
----------
560+
dtype : The dtype to check.
561+
562+
Raises
563+
------
564+
TypeError : The dtype could not be casted to a date-like dtype.
565+
ValueError : The dtype is an illegal date-like dtype (e.g. the
566+
the frequency provided is too specific)
567+
"""
568+
458569
try:
459570
typ = np.datetime_data(dtype)[0]
460571
except ValueError as e:

0 commit comments

Comments
 (0)