Skip to content

Commit 5a8f8da

Browse files
committed
DOC: Document all of inference.py
Partially addresses gh-15895.
1 parent 81103f7 commit 5a8f8da

File tree

1 file changed

+281
-21
lines changed

1 file changed

+281
-21
lines changed

pandas/types/inference.py

+281-21
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,108 @@
2222

2323

2424
def is_number(obj):
25+
"""
26+
Check if the object is a number.
27+
28+
Parameters
29+
----------
30+
obj : The object to check.
31+
32+
Returns
33+
-------
34+
is_number : bool
35+
Whether `obj` is a number or not.
36+
37+
Examples
38+
--------
39+
>>> is_number(1)
40+
True
41+
>>> is_number("foo")
42+
False
43+
"""
44+
2545
return isinstance(obj, (Number, np.number))
2646

2747

2848
def is_string_like(obj):
49+
"""
50+
Check if the object is a string.
51+
52+
Parameters
53+
----------
54+
obj : The object to check.
55+
56+
Examples
57+
--------
58+
>>> is_string_like("foo")
59+
True
60+
>>> is_string_like(1)
61+
False
62+
63+
Returns
64+
-------
65+
is_str_like : bool
66+
Whether `obj` is a string or not.
67+
"""
68+
2969
return isinstance(obj, (text_type, string_types))
3070

3171

32-
def _iterable_not_string(x):
33-
return (isinstance(x, collections.Iterable) and
34-
not isinstance(x, string_types))
72+
def _iterable_not_string(obj):
73+
"""
74+
Check if the object is an iterable but not a string.
75+
76+
Parameters
77+
----------
78+
obj : The object to check.
79+
80+
Returns
81+
-------
82+
is_iter_not_string : bool
83+
Whether `obj` is a non-string iterable.
84+
85+
Examples
86+
--------
87+
>>> _iterable_not_string([1, 2, 3])
88+
True
89+
>>> _iterable_not_string("foo")
90+
False
91+
>>> _iterable_not_string(1)
92+
False
93+
"""
94+
95+
return (isinstance(obj, collections.Iterable) and
96+
not isinstance(obj, string_types))
3597

3698

3799
def is_iterator(obj):
100+
"""
101+
Check if the object is an iterator.
102+
103+
For example, lists are considered iterators
104+
but not strings or datetime objects.
105+
106+
Parameters
107+
----------
108+
obj : The object to check.
109+
110+
Returns
111+
-------
112+
is_iter : bool
113+
Whether `obj` is an iterator.
114+
115+
Examples
116+
--------
117+
>>> is_iterator([1, 2, 3])
118+
True
119+
>>> is_iterator(datetime(2017, 1, 1))
120+
False
121+
>>> is_iterator("foo")
122+
False
123+
>>> is_iterator(1)
124+
False
125+
"""
126+
38127
if not hasattr(obj, '__iter__'):
39128
return False
40129

@@ -47,6 +136,40 @@ def is_iterator(obj):
47136

48137

49138
def is_file_like(obj):
139+
"""
140+
Check if the object is a file-like object.
141+
142+
For objects to be considered file-like, they must
143+
be an iterator AND have the following four methods:
144+
145+
1) read
146+
2) write
147+
3) seek
148+
4) tell
149+
150+
Note: file-like objects must be iterable, but
151+
iterable objects need not be file-like.
152+
153+
.. versionadded:: 0.20.0
154+
155+
Parameters
156+
----------
157+
obj : The object to check.
158+
159+
Returns
160+
-------
161+
is_file_like : bool
162+
Whether `obj` has file-like properties.
163+
164+
Examples
165+
--------
166+
>>> buffer(StringIO("data"))
167+
>>> is_file_like(buffer)
168+
True
169+
>>> is_file_like([1, 2, 3])
170+
False
171+
"""
172+
50173
file_attrs = ('read', 'write', 'seek', 'tell')
51174

52175
for attr in file_attrs:
@@ -60,10 +183,50 @@ def is_file_like(obj):
60183

61184

62185
def is_re(obj):
186+
"""
187+
Check if the object is a regex pattern instance.
188+
189+
Parameters
190+
----------
191+
obj : The object to check.
192+
193+
Returns
194+
-------
195+
is_regex : bool
196+
Whether `obj` is a regex pattern.
197+
198+
Examples
199+
--------
200+
>>> is_re(re.compile(".*"))
201+
True
202+
>>> is_re("foo")
203+
False
204+
"""
205+
63206
return isinstance(obj, re._pattern_type)
64207

65208

66209
def is_re_compilable(obj):
210+
"""
211+
Check if the object can be compiled into a regex pattern instance.
212+
213+
Parameters
214+
----------
215+
obj : The object to check.
216+
217+
Returns
218+
-------
219+
is_regex_compilable : bool
220+
Whether `obj` can be compiled as a regex pattern.
221+
222+
Examples
223+
--------
224+
>>> is_re_compilable(".*")
225+
True
226+
>>> is_re_compilable(1)
227+
False
228+
"""
229+
67230
try:
68231
re.compile(obj)
69232
except TypeError:
@@ -72,21 +235,95 @@ def is_re_compilable(obj):
72235
return True
73236

74237

75-
def is_list_like(arg):
76-
return (hasattr(arg, '__iter__') and
77-
not isinstance(arg, string_and_binary_types))
238+
def is_list_like(obj):
239+
"""
240+
Check if the object is list-like.
78241
242+
Objects that are considered list-like are for example Python
243+
lists, tuples, sets, NumPy arrays, and Pandas Series.
79244
80-
def is_dict_like(arg):
81-
return hasattr(arg, '__getitem__') and hasattr(arg, 'keys')
245+
Strings and datetime objects, however, are not considered list-like.
82246
247+
Parameters
248+
----------
249+
obj : The object to check.
83250
84-
def is_named_tuple(arg):
85-
return isinstance(arg, tuple) and hasattr(arg, '_fields')
251+
Returns
252+
-------
253+
is_list_like : bool
254+
Whether `obj` has list-like properties.
86255
256+
Examples
257+
--------
258+
>>> is_list_like([1, 2, 3])
259+
True
260+
>>> is_list_like({1, 2, 3})
261+
True
262+
>>> is_list_like(datetime(2017, 1, 1))
263+
False
264+
>>> is_list_like("foo")
265+
False
266+
>>> is_list_like(1)
267+
False
268+
"""
269+
270+
return (hasattr(obj, '__iter__') and
271+
not isinstance(obj, string_and_binary_types))
87272

88-
def is_hashable(arg):
89-
"""Return True if hash(arg) will succeed, False otherwise.
273+
274+
def is_dict_like(obj):
275+
"""
276+
Check if the object is dict-like.
277+
278+
Parameters
279+
----------
280+
obj : The object to check.
281+
282+
Returns
283+
-------
284+
is_dict_like : bool
285+
Whether `obj` has dict-like properties.
286+
287+
Examples
288+
--------
289+
>>> is_dict_like({1: 2})
290+
True
291+
>>> is_dict_like([1, 2, 3])
292+
False
293+
"""
294+
295+
return hasattr(obj, '__getitem__') and hasattr(obj, 'keys')
296+
297+
298+
def is_named_tuple(obj):
299+
"""
300+
Check if the object is a named tuple.
301+
302+
Parameters
303+
----------
304+
obj : The object to check.
305+
306+
Returns
307+
-------
308+
is_named_tuple : bool
309+
Whether `obj` is a named tuple.
310+
311+
Examples
312+
--------
313+
>>> Point = namedtuple("Point", ["x", "y"])
314+
>>> p = Point(1, 2)
315+
>>>
316+
>>> is_named_tuple(p)
317+
True
318+
>>> is_named_tuple((1, 2))
319+
False
320+
"""
321+
322+
return isinstance(obj, tuple) and hasattr(obj, '_fields')
323+
324+
325+
def is_hashable(obj):
326+
"""Return True if hash(obj) will succeed, False otherwise.
90327
91328
Some types will pass a test against collections.Hashable but fail when they
92329
are actually hashed with hash().
@@ -102,25 +339,48 @@ def is_hashable(arg):
102339
>>> is_hashable(a)
103340
False
104341
"""
105-
# unfortunately, we can't use isinstance(arg, collections.Hashable), which
106-
# can be faster than calling hash, because numpy scalars on Python 3 fail
107-
# this test
342+
# Unfortunately, we can't use isinstance(obj, collections.Hashable), which
343+
# can be faster than calling hash. That is because numpy scalars on Python
344+
# 3 fail this test.
108345

109-
# reconsider this decision once this numpy bug is fixed:
346+
# Reconsider this decision once this numpy bug is fixed:
110347
# https://github.com/numpy/numpy/issues/5562
111348

112349
try:
113-
hash(arg)
350+
hash(obj)
114351
except TypeError:
115352
return False
116353
else:
117354
return True
118355

119356

120-
def is_sequence(x):
357+
def is_sequence(obj):
358+
"""
359+
Check if the object is a sequence of objects.
360+
String types are not included as sequences here.
361+
362+
Parameters
363+
----------
364+
obj : The object to check.
365+
366+
Returns
367+
-------
368+
is_sequence : bool
369+
Whether `obj` is a sequence of objects.
370+
371+
Examples
372+
--------
373+
>>> l = [1, 2, 3]
374+
>>>
375+
>>> is_sequence(l)
376+
True
377+
>>> is_sequence(iter(l))
378+
False
379+
"""
380+
121381
try:
122-
iter(x)
123-
len(x) # it has a length
124-
return not isinstance(x, string_and_binary_types)
382+
iter(obj) # Can iterate over it.
383+
len(obj) # Has a length associated with it.
384+
return not isinstance(obj, string_and_binary_types)
125385
except (TypeError, AttributeError):
126386
return False

0 commit comments

Comments
 (0)