Skip to content

Commit e91901d

Browse files
committed
DOC: better docs on infer_type
1 parent 7e8432d commit e91901d

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

doc/source/whatsnew/v0.20.0.txt

+11-6
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,17 @@ will be removed from the ``*.common`` locations in a future release. (:issue:`15
8686

8787
The following are now part of this API:
8888

89-
.. ipython:: python
90-
91-
import pprint
92-
from pandas import errors
93-
excs = [ e for e in dir(errors) if not e.startswith('_') ]
94-
pprint.pprint(excs)
89+
.. code-block:: python
90+
91+
['AmbiguousIndexError',
92+
'DtypeWarning',
93+
'EmptyDataError',
94+
'OutOfBoundsDatetime',
95+
'ParserError',
96+
'ParserWarning',
97+
'PerformanceWarning',
98+
'UnsortedIndexError',
99+
'UnsupportedFunctionCall']
95100

96101
.. _whatsnew_0200.enhancements.groupby_access:
97102

pandas/_libs/src/inference.pyx

+47-14
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,42 @@ cdef _try_infer_map(v):
218218
return None
219219

220220

221-
def infer_dtype(object _values):
221+
def infer_dtype(object value):
222222
"""
223-
we are coercing to an ndarray here
223+
Effeciently infer the type of a passed val, or list-like
224+
array of values. Return a string describing the type.
225+
226+
Parameters
227+
----------
228+
value : scalar, list, ndarray, or pandas type
229+
230+
Returns
231+
-------
232+
string describing the common type of the input data.
233+
Results can include:
234+
- floating
235+
- integer
236+
- mixed-integer
237+
- mixed-integer-float
238+
- complex
239+
- categorical
240+
- boolean
241+
- datetime64
242+
- datetime
243+
- date
244+
- timedelta64
245+
- timedelta
246+
- time
247+
- period
248+
- string
249+
- unicode
250+
- bytes
251+
- mixed
252+
253+
Raises
254+
------
255+
TypeError if ndarray-like but cannot infer the dtype
256+
224257
"""
225258

226259
cdef:
@@ -229,27 +262,27 @@ def infer_dtype(object _values):
229262
ndarray values
230263
bint seen_pdnat = False, seen_val = False
231264

232-
if isinstance(_values, np.ndarray):
233-
values = _values
234-
elif hasattr(_values, 'dtype'):
265+
if isinstance(value, np.ndarray):
266+
values = value
267+
elif hasattr(value, 'dtype'):
235268

236269
# this will handle ndarray-like
237270
# e.g. categoricals
238271
try:
239-
values = getattr(_values, '_values', getattr(
240-
_values, 'values', _values))
272+
values = getattr(value, '_values', getattr(
273+
value, 'values', value))
241274
except:
242-
val = _try_infer_map(_values)
243-
if val is not None:
244-
return val
275+
value = _try_infer_map(value)
276+
if value is not None:
277+
return value
245278

246279
# its ndarray like but we can't handle
247-
raise ValueError("cannot infer type for {0}".format(type(_values)))
280+
raise ValueError("cannot infer type for {0}".format(type(value)))
248281

249282
else:
250-
if not isinstance(_values, list):
251-
_values = list(_values)
252-
values = list_to_object_array(_values)
283+
if not isinstance(value, list):
284+
value = list(value)
285+
values = list_to_object_array(value)
253286

254287
values = getattr(values, 'values', values)
255288
val = _try_infer_map(values)

0 commit comments

Comments
 (0)