Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/pandas/core/dtypes/inference.py: 39%
75 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1""" basic inference routines """
3from __future__ import annotations
5from collections import abc
6from numbers import Number
7import re
8from typing import Pattern
9import warnings
11import numpy as np
13from pandas._libs import lib
14from pandas._typing import ArrayLike
15from pandas.util._exceptions import find_stack_level
17is_bool = lib.is_bool
19is_integer = lib.is_integer
21is_float = lib.is_float
23is_complex = lib.is_complex
25is_scalar = lib.is_scalar
27is_decimal = lib.is_decimal
29is_interval = lib.is_interval
31is_list_like = lib.is_list_like
33is_iterator = lib.is_iterator
36def is_number(obj) -> bool:
37 """
38 Check if the object is a number.
40 Returns True when the object is a number, and False if is not.
42 Parameters
43 ----------
44 obj : any type
45 The object to check if is a number.
47 Returns
48 -------
49 is_number : bool
50 Whether `obj` is a number or not.
52 See Also
53 --------
54 api.types.is_integer: Checks a subgroup of numbers.
56 Examples
57 --------
58 >>> from pandas.api.types import is_number
59 >>> is_number(1)
60 True
61 >>> is_number(7.15)
62 True
64 Booleans are valid because they are int subclass.
66 >>> is_number(False)
67 True
69 >>> is_number("foo")
70 False
71 >>> is_number("5")
72 False
73 """
74 return isinstance(obj, (Number, np.number))
77def iterable_not_string(obj) -> bool:
78 """
79 Check if the object is an iterable but not a string.
81 Parameters
82 ----------
83 obj : The object to check.
85 Returns
86 -------
87 is_iter_not_string : bool
88 Whether `obj` is a non-string iterable.
90 Examples
91 --------
92 >>> iterable_not_string([1, 2, 3])
93 True
94 >>> iterable_not_string("foo")
95 False
96 >>> iterable_not_string(1)
97 False
98 """
99 return isinstance(obj, abc.Iterable) and not isinstance(obj, str)
102def is_file_like(obj) -> bool:
103 """
104 Check if the object is a file-like object.
106 For objects to be considered file-like, they must
107 be an iterator AND have either a `read` and/or `write`
108 method as an attribute.
110 Note: file-like objects must be iterable, but
111 iterable objects need not be file-like.
113 Parameters
114 ----------
115 obj : The object to check
117 Returns
118 -------
119 is_file_like : bool
120 Whether `obj` has file-like properties.
122 Examples
123 --------
124 >>> import io
125 >>> buffer = io.StringIO("data")
126 >>> is_file_like(buffer)
127 True
128 >>> is_file_like([1, 2, 3])
129 False
130 """
131 if not (hasattr(obj, "read") or hasattr(obj, "write")):
132 return False
134 return bool(hasattr(obj, "__iter__"))
137def is_re(obj) -> bool:
138 """
139 Check if the object is a regex pattern instance.
141 Parameters
142 ----------
143 obj : The object to check
145 Returns
146 -------
147 is_regex : bool
148 Whether `obj` is a regex pattern.
150 Examples
151 --------
152 >>> is_re(re.compile(".*"))
153 True
154 >>> is_re("foo")
155 False
156 """
157 return isinstance(obj, Pattern)
160def is_re_compilable(obj) -> bool:
161 """
162 Check if the object can be compiled into a regex pattern instance.
164 Parameters
165 ----------
166 obj : The object to check
168 Returns
169 -------
170 is_regex_compilable : bool
171 Whether `obj` can be compiled as a regex pattern.
173 Examples
174 --------
175 >>> is_re_compilable(".*")
176 True
177 >>> is_re_compilable(1)
178 False
179 """
180 try:
181 re.compile(obj)
182 except TypeError:
183 return False
184 else:
185 return True
188def is_array_like(obj) -> bool:
189 """
190 Check if the object is array-like.
192 For an object to be considered array-like, it must be list-like and
193 have a `dtype` attribute.
195 Parameters
196 ----------
197 obj : The object to check
199 Returns
200 -------
201 is_array_like : bool
202 Whether `obj` has array-like properties.
204 Examples
205 --------
206 >>> is_array_like(np.array([1, 2, 3]))
207 True
208 >>> is_array_like(pd.Series(["a", "b"]))
209 True
210 >>> is_array_like(pd.Index(["2016-01-01"]))
211 True
212 >>> is_array_like([1, 2, 3])
213 False
214 >>> is_array_like(("a", "b"))
215 False
216 """
217 return is_list_like(obj) and hasattr(obj, "dtype")
220def is_nested_list_like(obj) -> bool:
221 """
222 Check if the object is list-like, and that all of its elements
223 are also list-like.
225 Parameters
226 ----------
227 obj : The object to check
229 Returns
230 -------
231 is_list_like : bool
232 Whether `obj` has list-like properties.
234 Examples
235 --------
236 >>> is_nested_list_like([[1, 2, 3]])
237 True
238 >>> is_nested_list_like([{1, 2, 3}, {1, 2, 3}])
239 True
240 >>> is_nested_list_like(["foo"])
241 False
242 >>> is_nested_list_like([])
243 False
244 >>> is_nested_list_like([[1, 2, 3], 1])
245 False
247 Notes
248 -----
249 This won't reliably detect whether a consumable iterator (e. g.
250 a generator) is a nested-list-like without consuming the iterator.
251 To avoid consuming it, we always return False if the outer container
252 doesn't define `__len__`.
254 See Also
255 --------
256 is_list_like
257 """
258 return (
259 is_list_like(obj)
260 and hasattr(obj, "__len__")
261 and len(obj) > 0
262 and all(is_list_like(item) for item in obj)
263 )
266def is_dict_like(obj) -> bool:
267 """
268 Check if the object is dict-like.
270 Parameters
271 ----------
272 obj : The object to check
274 Returns
275 -------
276 is_dict_like : bool
277 Whether `obj` has dict-like properties.
279 Examples
280 --------
281 >>> is_dict_like({1: 2})
282 True
283 >>> is_dict_like([1, 2, 3])
284 False
285 >>> is_dict_like(dict)
286 False
287 >>> is_dict_like(dict())
288 True
289 """
290 dict_like_attrs = ("__getitem__", "keys", "__contains__")
291 return (
292 all(hasattr(obj, attr) for attr in dict_like_attrs)
293 # [GH 25196] exclude classes
294 and not isinstance(obj, type)
295 )
298def is_named_tuple(obj) -> bool:
299 """
300 Check if the object is a named tuple.
302 Parameters
303 ----------
304 obj : The object to check
306 Returns
307 -------
308 is_named_tuple : bool
309 Whether `obj` is a named tuple.
311 Examples
312 --------
313 >>> from collections import namedtuple
314 >>> Point = namedtuple("Point", ["x", "y"])
315 >>> p = Point(1, 2)
316 >>>
317 >>> is_named_tuple(p)
318 True
319 >>> is_named_tuple((1, 2))
320 False
321 """
322 return isinstance(obj, abc.Sequence) and hasattr(obj, "_fields")
325def is_hashable(obj) -> bool:
326 """
327 Return True if hash(obj) will succeed, False otherwise.
329 Some types will pass a test against collections.abc.Hashable but fail when
330 they are actually hashed with hash().
332 Distinguish between these and other types by trying the call to hash() and
333 seeing if they raise TypeError.
335 Returns
336 -------
337 bool
339 Examples
340 --------
341 >>> import collections
342 >>> a = ([],)
343 >>> isinstance(a, collections.abc.Hashable)
344 True
345 >>> is_hashable(a)
346 False
347 """
348 # Unfortunately, we can't use isinstance(obj, collections.abc.Hashable),
349 # which can be faster than calling hash. That is because numpy scalars
350 # fail this test.
352 # Reconsider this decision once this numpy bug is fixed:
353 # https://github.com/numpy/numpy/issues/5562
355 try:
356 hash(obj)
357 except TypeError:
358 return False
359 else:
360 return True
363def is_sequence(obj) -> bool:
364 """
365 Check if the object is a sequence of objects.
366 String types are not included as sequences here.
368 Parameters
369 ----------
370 obj : The object to check
372 Returns
373 -------
374 is_sequence : bool
375 Whether `obj` is a sequence of objects.
377 Examples
378 --------
379 >>> l = [1, 2, 3]
380 >>>
381 >>> is_sequence(l)
382 True
383 >>> is_sequence(iter(l))
384 False
385 """
386 try:
387 iter(obj) # Can iterate over it.
388 len(obj) # Has a length associated with it.
389 return not isinstance(obj, (str, bytes))
390 except (TypeError, AttributeError):
391 return False
394def is_dataclass(item):
395 """
396 Checks if the object is a data-class instance
398 Parameters
399 ----------
400 item : object
402 Returns
403 --------
404 is_dataclass : bool
405 True if the item is an instance of a data-class,
406 will return false if you pass the data class itself
408 Examples
409 --------
410 >>> from dataclasses import dataclass
411 >>> @dataclass
412 ... class Point:
413 ... x: int
414 ... y: int
416 >>> is_dataclass(Point)
417 False
418 >>> is_dataclass(Point(0,2))
419 True
421 """
422 try:
423 from dataclasses import is_dataclass
425 return is_dataclass(item) and not isinstance(item, type)
426 except ImportError:
427 return False
430def is_inferred_bool_dtype(arr: ArrayLike) -> bool:
431 """
432 Check if this is a ndarray[bool] or an ndarray[object] of bool objects.
434 Parameters
435 ----------
436 arr : np.ndarray or ExtensionArray
438 Returns
439 -------
440 bool
442 Notes
443 -----
444 This does not include the special treatment is_bool_dtype uses for
445 Categorical.
446 """
447 if not isinstance(arr, np.ndarray):
448 return False
450 dtype = arr.dtype
451 if dtype == np.dtype(bool):
452 return True
453 elif dtype == np.dtype("object"):
454 result = lib.is_bool_array(arr)
455 if result:
456 # GH#46188
457 warnings.warn(
458 "In a future version, object-dtype columns with all-bool values "
459 "will not be included in reductions with bool_only=True. "
460 "Explicitly cast to bool dtype instead.",
461 FutureWarning,
462 stacklevel=find_stack_level(),
463 )
464 return result
466 return False