🔍 Code Extractor

function isscalar

Maturity: 30

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

File:
/tf/active/vicechatdev/patches/util.py
Lines:
798 - 802
Complexity:
simple

Purpose

This utility function determines whether a given value is scalar-like, which includes None, NumPy scalar types, or datetime-related types. It's commonly used for type validation and data processing pipelines where distinguishing between scalar and non-scalar (array-like, collection) values is necessary.

Source Code

def isscalar(val):
    """
    Value is scalar or None
    """
    return val is None or np.isscalar(val) or isinstance(val, datetime_types)

Parameters

Name Type Default Kind
val - - positional_or_keyword

Parameter Details

val: The value to check. Can be of any type including None, scalar types (int, float, str, bool), NumPy scalars, datetime objects, or non-scalar types like lists, arrays, or DataFrames.

Return Value

Returns a boolean value: True if the input is None, a NumPy scalar (as determined by np.isscalar()), or an instance of datetime_types; False otherwise for non-scalar types like lists, arrays, or DataFrames.

Dependencies

  • numpy

Required Imports

import numpy as np

Conditional/Optional Imports

These imports are only needed under specific conditions:

datetime_types variable must be defined

Condition: The function references 'datetime_types' which must be defined in the module scope, typically including datetime.datetime, datetime.date, datetime.time, datetime.timedelta, and potentially cftime types

Required (conditional)

Usage Example

import numpy as np
import datetime

# Define datetime_types (normally defined at module level)
datetime_types = (datetime.datetime, datetime.date, datetime.time, datetime.timedelta)

def isscalar(val):
    return val is None or np.isscalar(val) or isinstance(val, datetime_types)

# Test with various inputs
print(isscalar(None))  # True
print(isscalar(5))  # True
print(isscalar(3.14))  # True
print(isscalar('hello'))  # True
print(isscalar(datetime.datetime.now()))  # True
print(isscalar([1, 2, 3]))  # False
print(isscalar(np.array([1, 2, 3])))  # False
print(isscalar({'key': 'value'}))  # False

Best Practices

  • Ensure 'datetime_types' is properly defined in the module scope before using this function
  • This function is useful for input validation before performing operations that require scalar values
  • Consider that strings are treated as scalars by np.isscalar(), which may or may not be desired depending on your use case
  • The function treats None as a scalar, which is a design choice that may need consideration in your application logic
  • When using in a data processing pipeline, combine with other type checks if you need more specific type validation

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function isdatetime 76.4% 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 is_float 68.8% 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 61.0% 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 isnat 60.4% similar

    Checks if a value is NaT (Not-a-Time), a special marker for missing or invalid datetime/timedelta values in NumPy and pandas.

    From: /tf/active/vicechatdev/patches/util.py
  • function isfinite 60.3% similar

    Extended version of numpy.isfinite that handles additional data types including None, strings, datetime objects, masked arrays, and dask arrays.

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