function is_series
Checks whether the supplied data object is a pandas Series or dask Series type, with lazy loading support for dask.
/tf/active/vicechatdev/patches/util.py
1488 - 1496
simple
Purpose
This utility function provides a unified way to check if data is a Series type across both pandas and dask dataframe libraries. It handles optional dask dependency gracefully by checking if dask.dataframe is already loaded in sys.modules before attempting to import it, making it safe to use in environments where dask may or may not be available.
Source Code
def is_series(data):
"""
Checks whether the supplied data is of Series type.
"""
dd = None
if 'dask.dataframe' in sys.modules:
import dask.dataframe as dd
return((pd is not None and isinstance(data, pd.Series)) or
(dd is not None and isinstance(data, dd.Series)))
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
data |
- | - | positional_or_keyword |
Parameter Details
data: Any Python object to be checked. Typically expected to be a pandas Series, dask Series, or other data structure. No type constraints are enforced - the function will return False for non-Series types.
Return Value
Returns a boolean value: True if the data is an instance of pandas.Series or dask.dataframe.Series (when dask is available), False otherwise. The function uses short-circuit evaluation, checking pandas first, then dask if available.
Dependencies
syspandasdask.dataframe
Required Imports
import sys
import pandas as pd
Conditional/Optional Imports
These imports are only needed under specific conditions:
import dask.dataframe as dd
Condition: only if dask.dataframe is already loaded in sys.modules (lazy import within function)
OptionalUsage Example
import sys
import pandas as pd
# Assuming is_series function is available
# Test with pandas Series
pd_series = pd.Series([1, 2, 3, 4, 5])
print(is_series(pd_series)) # Output: True
# Test with pandas DataFrame
pd_df = pd.DataFrame({'a': [1, 2, 3]})
print(is_series(pd_df)) # Output: False
# Test with list
my_list = [1, 2, 3]
print(is_series(my_list)) # Output: False
# Test with dask Series (if dask is available)
try:
import dask.dataframe as dd
dask_series = dd.from_pandas(pd_series, npartitions=2)
print(is_series(dask_series)) # Output: True
except ImportError:
print("Dask not available")
Best Practices
- This function uses lazy importing for dask to avoid forcing dask as a hard dependency - only checks for dask Series if dask.dataframe is already loaded
- The function assumes 'pd' is already imported as pandas in the global scope - ensure pandas is imported before using this function
- Returns False for any non-Series types rather than raising exceptions, making it safe for defensive programming
- Use this function when you need to handle both pandas and dask Series uniformly in code that may work with either library
- The function checks sys.modules to determine if dask is available, which is more efficient than try-except import blocks
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function is_dataframe 82.3% similar
-
function is_dask_array 68.4% similar
-
function is_cupy_array 57.1% similar
-
function isdatetime 50.5% similar
-
function isscalar 47.8% similar