🔍 Code Extractor

function is_int

Maturity: 47

Checks if an object is an integer type, supporting native Python integers, NumPy integer types, and optionally float types with integer values.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1280 - 1294
Complexity:
simple

Purpose

This utility function provides a robust way to determine if an object represents an integer value. It handles multiple data types including Python's built-in int, NumPy integer types (signed and unsigned), and optionally float-like objects that have integer values (e.g., 1.0). This is particularly useful when working with mixed data types from different libraries (NumPy, Pandas, etc.) where type checking needs to be consistent across different integer representations.

Source Code

def is_int(obj, int_like=False):
    """
    Checks for int types including the native Python type and NumPy-like objects

    Args:
        obj: Object to check for integer type
        int_like (boolean): Check for float types with integer value

    Returns:
        Boolean indicating whether the supplied value is of integer type.
    """
    real_int = isinstance(obj, int) or getattr(getattr(obj, 'dtype', None), 'kind', 'o') in 'ui'
    if real_int or (int_like and hasattr(obj, 'is_integer') and obj.is_integer()):
        return True
    return False

Parameters

Name Type Default Kind
obj - - positional_or_keyword
int_like - False positional_or_keyword

Parameter Details

obj: The object to check for integer type. Can be any Python object, including native Python types, NumPy arrays/scalars, Pandas objects, or other numeric types. No type restrictions are enforced.

int_like: Boolean flag (default: False) that when set to True, extends the check to include float-like objects that have integer values (e.g., 1.0, 2.0). The object must have an 'is_integer()' method that returns True for this to work. Commonly applies to Python floats and NumPy float types.

Return Value

Returns a Boolean value: True if the object is an integer type (or an integer-valued float when int_like=True), False otherwise. The function returns True for: Python int types, NumPy integer types (both signed 'i' and unsigned 'u' dtype kinds), and when int_like=True, any numeric type with an is_integer() method that returns True.

Dependencies

  • numpy

Required Imports

import numpy as np

Usage Example

import numpy as np

# Check native Python integer
result = is_int(5)
print(result)  # True

# Check Python float
result = is_int(5.5)
print(result)  # False

# Check float with integer value (without int_like)
result = is_int(5.0)
print(result)  # False

# Check float with integer value (with int_like)
result = is_int(5.0, int_like=True)
print(result)  # True

# Check NumPy integer
result = is_int(np.int32(10))
print(result)  # True

# Check NumPy unsigned integer
result = is_int(np.uint64(100))
print(result)  # True

# Check NumPy float with integer value
result = is_int(np.float64(3.0), int_like=True)
print(result)  # True

# Check string
result = is_int('5')
print(result)  # False

Best Practices

  • Use int_like=True when you need to accept float values that represent whole numbers (e.g., 1.0, 2.0) as valid integers
  • This function is particularly useful when working with mixed data sources (NumPy, Pandas, native Python) where integer types may vary
  • The function checks dtype.kind for 'u' (unsigned) and 'i' (signed) to catch all NumPy integer variants
  • Be aware that int_like=True requires the object to have an is_integer() method, which is available on Python floats and NumPy float types but may not be available on all numeric types
  • This function does not perform value conversion; it only checks type. Use int() or similar functions for actual conversion
  • The function returns False for string representations of integers (e.g., '5'); use appropriate parsing if needed

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function is_number 60.7% similar

    Determines whether an object is a number or behaves like a number, with special handling for numpy types and numeric-like classes.

    From: /tf/active/vicechatdev/patches/util.py
  • function is_float 57.4% similar

    A type-checking utility function that determines whether a given object is a floating-point scalar value, supporting both Python's native float type and NumPy floating-point types.

    From: /tf/active/vicechatdev/patches/util.py
  • function isnumeric 57.1% similar

    Determines whether a given value can be converted to a numeric type (int or float), excluding strings and boolean types.

    From: /tf/active/vicechatdev/patches/util.py
  • function isdatetime 50.1% similar

    Determines whether a given value (array or scalar) is a recognized datetime type, checking both NumPy datetime64 arrays and Python datetime objects.

    From: /tf/active/vicechatdev/patches/util.py
  • function isscalar 49.1% similar

    Checks if a value is a scalar type, None, or a datetime-related type.

    From: /tf/active/vicechatdev/patches/util.py
← Back to Browse