zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
""" basic inference routines """
 
from __future__ import annotations
 
from collections import abc
from numbers import Number
import re
from typing import Pattern
 
import numpy as np
 
from pandas._libs import lib
 
is_bool = lib.is_bool
 
is_integer = lib.is_integer
 
is_float = lib.is_float
 
is_complex = lib.is_complex
 
is_scalar = lib.is_scalar
 
is_decimal = lib.is_decimal
 
is_interval = lib.is_interval
 
is_list_like = lib.is_list_like
 
is_iterator = lib.is_iterator
 
 
def is_number(obj) -> bool:
    """
    Check if the object is a number.
 
    Returns True when the object is a number, and False if is not.
 
    Parameters
    ----------
    obj : any type
        The object to check if is a number.
 
    Returns
    -------
    bool
        Whether `obj` is a number or not.
 
    See Also
    --------
    api.types.is_integer: Checks a subgroup of numbers.
 
    Examples
    --------
    >>> from pandas.api.types import is_number
    >>> is_number(1)
    True
    >>> is_number(7.15)
    True
 
    Booleans are valid because they are int subclass.
 
    >>> is_number(False)
    True
 
    >>> is_number("foo")
    False
    >>> is_number("5")
    False
    """
    return isinstance(obj, (Number, np.number))
 
 
def iterable_not_string(obj) -> bool:
    """
    Check if the object is an iterable but not a string.
 
    Parameters
    ----------
    obj : The object to check.
 
    Returns
    -------
    is_iter_not_string : bool
        Whether `obj` is a non-string iterable.
 
    Examples
    --------
    >>> iterable_not_string([1, 2, 3])
    True
    >>> iterable_not_string("foo")
    False
    >>> iterable_not_string(1)
    False
    """
    return isinstance(obj, abc.Iterable) and not isinstance(obj, str)
 
 
def is_file_like(obj) -> bool:
    """
    Check if the object is a file-like object.
 
    For objects to be considered file-like, they must
    be an iterator AND have either a `read` and/or `write`
    method as an attribute.
 
    Note: file-like objects must be iterable, but
    iterable objects need not be file-like.
 
    Parameters
    ----------
    obj : The object to check
 
    Returns
    -------
    bool
        Whether `obj` has file-like properties.
 
    Examples
    --------
    >>> import io
    >>> from pandas.api.types import is_file_like
    >>> buffer = io.StringIO("data")
    >>> is_file_like(buffer)
    True
    >>> is_file_like([1, 2, 3])
    False
    """
    if not (hasattr(obj, "read") or hasattr(obj, "write")):
        return False
 
    return bool(hasattr(obj, "__iter__"))
 
 
def is_re(obj) -> bool:
    """
    Check if the object is a regex pattern instance.
 
    Parameters
    ----------
    obj : The object to check
 
    Returns
    -------
    bool
        Whether `obj` is a regex pattern.
 
    Examples
    --------
    >>> from pandas.api.types import is_re
    >>> import re
    >>> is_re(re.compile(".*"))
    True
    >>> is_re("foo")
    False
    """
    return isinstance(obj, Pattern)
 
 
def is_re_compilable(obj) -> bool:
    """
    Check if the object can be compiled into a regex pattern instance.
 
    Parameters
    ----------
    obj : The object to check
 
    Returns
    -------
    bool
        Whether `obj` can be compiled as a regex pattern.
 
    Examples
    --------
    >>> from pandas.api.types import is_re_compilable
    >>> is_re_compilable(".*")
    True
    >>> is_re_compilable(1)
    False
    """
    try:
        re.compile(obj)
    except TypeError:
        return False
    else:
        return True
 
 
def is_array_like(obj) -> bool:
    """
    Check if the object is array-like.
 
    For an object to be considered array-like, it must be list-like and
    have a `dtype` attribute.
 
    Parameters
    ----------
    obj : The object to check
 
    Returns
    -------
    is_array_like : bool
        Whether `obj` has array-like properties.
 
    Examples
    --------
    >>> is_array_like(np.array([1, 2, 3]))
    True
    >>> is_array_like(pd.Series(["a", "b"]))
    True
    >>> is_array_like(pd.Index(["2016-01-01"]))
    True
    >>> is_array_like([1, 2, 3])
    False
    >>> is_array_like(("a", "b"))
    False
    """
    return is_list_like(obj) and hasattr(obj, "dtype")
 
 
def is_nested_list_like(obj) -> bool:
    """
    Check if the object is list-like, and that all of its elements
    are also list-like.
 
    Parameters
    ----------
    obj : The object to check
 
    Returns
    -------
    is_list_like : bool
        Whether `obj` has list-like properties.
 
    Examples
    --------
    >>> is_nested_list_like([[1, 2, 3]])
    True
    >>> is_nested_list_like([{1, 2, 3}, {1, 2, 3}])
    True
    >>> is_nested_list_like(["foo"])
    False
    >>> is_nested_list_like([])
    False
    >>> is_nested_list_like([[1, 2, 3], 1])
    False
 
    Notes
    -----
    This won't reliably detect whether a consumable iterator (e. g.
    a generator) is a nested-list-like without consuming the iterator.
    To avoid consuming it, we always return False if the outer container
    doesn't define `__len__`.
 
    See Also
    --------
    is_list_like
    """
    return (
        is_list_like(obj)
        and hasattr(obj, "__len__")
        and len(obj) > 0
        and all(is_list_like(item) for item in obj)
    )
 
 
def is_dict_like(obj) -> bool:
    """
    Check if the object is dict-like.
 
    Parameters
    ----------
    obj : The object to check
 
    Returns
    -------
    bool
        Whether `obj` has dict-like properties.
 
    Examples
    --------
    >>> from pandas.api.types import is_dict_like
    >>> is_dict_like({1: 2})
    True
    >>> is_dict_like([1, 2, 3])
    False
    >>> is_dict_like(dict)
    False
    >>> is_dict_like(dict())
    True
    """
    dict_like_attrs = ("__getitem__", "keys", "__contains__")
    return (
        all(hasattr(obj, attr) for attr in dict_like_attrs)
        # [GH 25196] exclude classes
        and not isinstance(obj, type)
    )
 
 
def is_named_tuple(obj) -> bool:
    """
    Check if the object is a named tuple.
 
    Parameters
    ----------
    obj : The object to check
 
    Returns
    -------
    bool
        Whether `obj` is a named tuple.
 
    Examples
    --------
    >>> from collections import namedtuple
    >>> from pandas.api.types import is_named_tuple
    >>> Point = namedtuple("Point", ["x", "y"])
    >>> p = Point(1, 2)
    >>>
    >>> is_named_tuple(p)
    True
    >>> is_named_tuple((1, 2))
    False
    """
    return isinstance(obj, abc.Sequence) and hasattr(obj, "_fields")
 
 
def is_hashable(obj) -> bool:
    """
    Return True if hash(obj) will succeed, False otherwise.
 
    Some types will pass a test against collections.abc.Hashable but fail when
    they are actually hashed with hash().
 
    Distinguish between these and other types by trying the call to hash() and
    seeing if they raise TypeError.
 
    Returns
    -------
    bool
 
    Examples
    --------
    >>> import collections
    >>> from pandas.api.types import is_hashable
    >>> a = ([],)
    >>> isinstance(a, collections.abc.Hashable)
    True
    >>> is_hashable(a)
    False
    """
    # Unfortunately, we can't use isinstance(obj, collections.abc.Hashable),
    # which can be faster than calling hash. That is because numpy scalars
    # fail this test.
 
    # Reconsider this decision once this numpy bug is fixed:
    # https://github.com/numpy/numpy/issues/5562
 
    try:
        hash(obj)
    except TypeError:
        return False
    else:
        return True
 
 
def is_sequence(obj) -> bool:
    """
    Check if the object is a sequence of objects.
    String types are not included as sequences here.
 
    Parameters
    ----------
    obj : The object to check
 
    Returns
    -------
    is_sequence : bool
        Whether `obj` is a sequence of objects.
 
    Examples
    --------
    >>> l = [1, 2, 3]
    >>>
    >>> is_sequence(l)
    True
    >>> is_sequence(iter(l))
    False
    """
    try:
        iter(obj)  # Can iterate over it.
        len(obj)  # Has a length associated with it.
        return not isinstance(obj, (str, bytes))
    except (TypeError, AttributeError):
        return False
 
 
def is_dataclass(item):
    """
    Checks if the object is a data-class instance
 
    Parameters
    ----------
    item : object
 
    Returns
    --------
    is_dataclass : bool
        True if the item is an instance of a data-class,
        will return false if you pass the data class itself
 
    Examples
    --------
    >>> from dataclasses import dataclass
    >>> @dataclass
    ... class Point:
    ...     x: int
    ...     y: int
 
    >>> is_dataclass(Point)
    False
    >>> is_dataclass(Point(0,2))
    True
 
    """
    try:
        import dataclasses
 
        return dataclasses.is_dataclass(item) and not isinstance(item, type)
    except ImportError:
        return False