function is_ibis_expr
Checks whether a given data object is an Ibis column expression by verifying if the ibis module is loaded and if the data is an instance of ibis.expr.types.ColumnExpr.
/tf/active/vicechatdev/patches/util.py
1513 - 1517
simple
Purpose
This function provides a safe way to determine if data is an Ibis expression without requiring ibis as a hard dependency. It first checks if ibis is already imported in sys.modules before attempting to use it, making it suitable for libraries that optionally support Ibis but don't require it. This pattern allows for graceful degradation when ibis is not available.
Source Code
def is_ibis_expr(data):
if 'ibis' in sys.modules:
import ibis
return isinstance(data, ibis.expr.types.ColumnExpr)
return False
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
data |
- | - | positional_or_keyword |
Parameter Details
data: Any Python object to be checked. Typically expected to be a data structure that might be an Ibis column expression, but can be any type. No constraints on input type.
Return Value
Returns a boolean value. True if the ibis module is loaded in sys.modules AND the data parameter is an instance of ibis.expr.types.ColumnExpr. Returns False if ibis is not loaded or if data is not an Ibis column expression.
Dependencies
sysibis
Required Imports
import sys
Conditional/Optional Imports
These imports are only needed under specific conditions:
import ibis
Condition: only imported if 'ibis' is already present in sys.modules (lazy import within function)
OptionalUsage Example
import sys
import ibis
# Example 1: With ibis loaded and valid expression
conn = ibis.connect('duckdb://')
table = conn.table('my_table')
column_expr = table.column_name
result = is_ibis_expr(column_expr)
print(result) # True
# Example 2: With non-ibis data
result = is_ibis_expr([1, 2, 3])
print(result) # False
# Example 3: When ibis is not imported
# (assuming ibis was never imported)
result = is_ibis_expr(some_data)
print(result) # False
Best Practices
- This function uses lazy importing to avoid forcing ibis as a hard dependency, which is a good pattern for optional features
- The function checks sys.modules before importing to avoid ImportError exceptions
- Use this function when you need to handle both ibis and non-ibis data types in a unified interface
- This pattern is thread-safe as sys.modules is a shared dictionary managed by Python's import system
- Consider using this function in type dispatch or polymorphic functions that need to handle multiple data backend types
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function is_dataframe 45.6% similar
-
function is_cupy_array 44.1% similar
-
function is_series 42.3% similar
-
function is_dask_array 41.7% similar
-
function identify_variables 38.9% similar