Skip to content

Commit ba08e6d

Browse files
committed
DOC: Document all of inference.py
Partially addresses pandas-devgh-15895.
1 parent ac439f6 commit ba08e6d

File tree

1 file changed

+286
-21
lines changed

1 file changed

+286
-21
lines changed

pandas/types/inference.py

+286-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,45 @@ 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+
have the following four methods:
144+
145+
1) read
146+
2) write
147+
3) seek
148+
4) tell
149+
150+
In addition, the object must be iterable, as mock
151+
objects can easily have the four aforementioned
152+
properties. However, being truly iterable is much
153+
more difficult to do.
154+
155+
Note: file-like objects must be iterable, but
156+
iterable objects need not be file-like.
157+
158+
.. versionadded:: 0.20.0
159+
160+
Parameters
161+
----------
162+
obj : The object to check.
163+
164+
Returns
165+
-------
166+
is_file_like : bool
167+
Whether `obj` has file-like properties.
168+
169+
Examples
170+
--------
171+
>>> buffer(StringIO("data"))
172+
>>> is_file_like(buffer)
173+
True
174+
>>> is_file_like([1, 2, 3])
175+
False
176+
"""
177+
50178
file_attrs = ('read', 'write', 'seek', 'tell')
51179

52180
for attr in file_attrs:
@@ -60,10 +188,50 @@ def is_file_like(obj):
60188

61189

62190
def is_re(obj):
191+
"""
192+
Check if the object is a regex pattern instance.
193+
194+
Parameters
195+
----------
196+
obj : The object to check.
197+
198+
Returns
199+
-------
200+
is_regex : bool
201+
Whether `obj` is a regex pattern.
202+
203+
Examples
204+
--------
205+
>>> is_re(re.compile(".*"))
206+
True
207+
>>> is_re("foo")
208+
False
209+
"""
210+
63211
return isinstance(obj, re._pattern_type)
64212

65213

66214
def is_re_compilable(obj):
215+
"""
216+
Check if the object can be compiled into a regex pattern instance.
217+
218+
Parameters
219+
----------
220+
obj : The object to check.
221+
222+
Returns
223+
-------
224+
is_regex_compilable : bool
225+
Whether `obj` can be compiled as a regex pattern.
226+
227+
Examples
228+
--------
229+
>>> is_re_compilable(".*")
230+
True
231+
>>> is_re_compilable(1)
232+
False
233+
"""
234+
67235
try:
68236
re.compile(obj)
69237
except TypeError:
@@ -72,21 +240,95 @@ def is_re_compilable(obj):
72240
return True
73241

74242

75-
def is_list_like(arg):
76-
return (hasattr(arg, '__iter__') and
77-
not isinstance(arg, string_and_binary_types))
243+
def is_list_like(obj):
244+
"""
245+
Check if the object is list-like.
246+
247+
Objects that are considered list-like are for example Python
248+
lists, tuples, sets, NumPy arrays, and Pandas Series.
249+
250+
Strings and datetime objects, however, are not considered list-like.
251+
252+
Parameters
253+
----------
254+
obj : The object to check.
255+
256+
Returns
257+
-------
258+
is_list_like : bool
259+
Whether `obj` has list-like properties.
260+
261+
Examples
262+
--------
263+
>>> is_list_like([1, 2, 3])
264+
True
265+
>>> is_list_like({1, 2, 3})
266+
True
267+
>>> is_list_like(datetime(2017, 1, 1))
268+
False
269+
>>> is_list_like("foo")
270+
False
271+
>>> is_list_like(1)
272+
False
273+
"""
274+
275+
return (hasattr(obj, '__iter__') and
276+
not isinstance(obj, string_and_binary_types))
277+
278+
279+
def is_dict_like(obj):
280+
"""
281+
Check if the object is dict-like.
282+
283+
Parameters
284+
----------
285+
obj : The object to check.
286+
287+
Returns
288+
-------
289+
is_dict_like : bool
290+
Whether `obj` has dict-like properties.
78291
292+
Examples
293+
--------
294+
>>> is_dict_like({1: 2})
295+
True
296+
>>> is_dict_like([1, 2, 3])
297+
False
298+
"""
79299

80-
def is_dict_like(arg):
81-
return hasattr(arg, '__getitem__') and hasattr(arg, 'keys')
300+
return hasattr(obj, '__getitem__') and hasattr(obj, 'keys')
82301

83302

84-
def is_named_tuple(arg):
85-
return isinstance(arg, tuple) and hasattr(arg, '_fields')
303+
def is_named_tuple(obj):
304+
"""
305+
Check if the object is a named tuple.
86306
307+
Parameters
308+
----------
309+
obj : The object to check.
87310
88-
def is_hashable(arg):
89-
"""Return True if hash(arg) will succeed, False otherwise.
311+
Returns
312+
-------
313+
is_named_tuple : bool
314+
Whether `obj` is a named tuple.
315+
316+
Examples
317+
--------
318+
>>> Point = namedtuple("Point", ["x", "y"])
319+
>>> p = Point(1, 2)
320+
>>>
321+
>>> is_named_tuple(p)
322+
True
323+
>>> is_named_tuple((1, 2))
324+
False
325+
"""
326+
327+
return isinstance(obj, tuple) and hasattr(obj, '_fields')
328+
329+
330+
def is_hashable(obj):
331+
"""Return True if hash(obj) will succeed, False otherwise.
90332
91333
Some types will pass a test against collections.Hashable but fail when they
92334
are actually hashed with hash().
@@ -102,25 +344,48 @@ def is_hashable(arg):
102344
>>> is_hashable(a)
103345
False
104346
"""
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
347+
# Unfortunately, we can't use isinstance(obj, collections.Hashable), which
348+
# can be faster than calling hash. That is because numpy scalars on Python
349+
# 3 fail this test.
108350

109-
# reconsider this decision once this numpy bug is fixed:
351+
# Reconsider this decision once this numpy bug is fixed:
110352
# https://github.com/numpy/numpy/issues/5562
111353

112354
try:
113-
hash(arg)
355+
hash(obj)
114356
except TypeError:
115357
return False
116358
else:
117359
return True
118360

119361

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

0 commit comments

Comments
 (0)