🔍 Code Extractor

function is_series

Maturity: 36

Checks whether the supplied data object is a pandas Series or dask Series type, with lazy loading support for dask.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1488 - 1496
Complexity:
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

  • sys
  • pandas
  • dask.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)

Optional

Usage 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

    Checks whether the supplied data object is a pandas DataFrame or a Dask DataFrame, with support for lazy imports of both libraries.

    From: /tf/active/vicechatdev/patches/util.py
  • function is_dask_array 68.4% similar

    Checks whether the provided data object is a Dask array by conditionally importing dask.array and performing an isinstance check.

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

    Checks whether the provided data object is a CuPy ndarray by conditionally importing CuPy and performing an isinstance check.

    From: /tf/active/vicechatdev/patches/util.py
  • function isdatetime 50.5% 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 47.8% 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