🔍 Code Extractor

function unpack_group

Maturity: 27

Unpacks a pandas DataFrame group by iterating over rows and yielding tuples of keys and objects, with special handling for objects with 'kdims' attribute.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1802 - 1809
Complexity:
moderate

Purpose

This function is designed to process grouped pandas DataFrame data, extracting key-value pairs where keys are transformed using a getter function. It handles two types of objects differently: those with a 'kdims' attribute (likely HoloViews/Panel objects) are yielded with their key directly, while others have their keys wrapped in a tuple using wrap_tuple(). This is typically used in data visualization or processing pipelines where grouped data needs to be unpacked and transformed.

Source Code

def unpack_group(group, getter):
    for k, v in group.iterrows():
        obj = v.values[0]
        key = getter(k)
        if hasattr(obj, 'kdims'):
            yield (key, obj)
        else:
            yield (wrap_tuple(key), obj)

Parameters

Name Type Default Kind
group - - positional_or_keyword
getter - - positional_or_keyword

Parameter Details

group: A pandas DataFrame or DataFrame group (typically from a groupby operation). Expected to have an iterrows() method that yields (index, Series) pairs where each Series contains at least one value.

getter: A callable function that transforms the row index/key. Takes the index from iterrows() as input and returns a transformed key value. This allows flexible key extraction and transformation logic.

Return Value

A generator that yields tuples of (key, object) pairs. For objects with 'kdims' attribute, yields (transformed_key, object). For objects without 'kdims', yields (wrapped_tuple_key, object) where the key is wrapped using wrap_tuple(). The generator produces one tuple per row in the input group.

Dependencies

  • pandas

Required Imports

import pandas as pd

Usage Example

import pandas as pd

# Assuming wrap_tuple is defined elsewhere
def wrap_tuple(x):
    return (x,) if not isinstance(x, tuple) else x

# Create sample data
df = pd.DataFrame({'value': [obj1, obj2, obj3]}, index=['a', 'b', 'c'])

# Define a getter function
def my_getter(key):
    return key.upper()

# Unpack the group
for key, obj in unpack_group(df, my_getter):
    print(f"Key: {key}, Object: {obj}")

# With grouped data
grouped = df.groupby(level=0)
for name, group in grouped:
    for key, obj in unpack_group(group, lambda k: k):
        process(key, obj)

Best Practices

  • Ensure the getter function can handle the index types present in your DataFrame group
  • The wrap_tuple function must be available in scope before calling this function
  • This function expects each row to have at least one value (v.values[0]), so ensure your DataFrame structure matches this expectation
  • The function checks for 'kdims' attribute using hasattr, suggesting it's designed to work with HoloViews or similar visualization objects
  • Consider the memory implications when working with large DataFrames as this creates a generator
  • The function assumes the first value in each row (v.values[0]) is the object of interest

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class ndmapping_groupby 59.4% similar

    A parameterized function class that performs groupby operations on NdMapping objects, automatically using pandas for improved performance when available, falling back to pure Python implementation otherwise.

    From: /tf/active/vicechatdev/patches/util.py
  • function group_select 55.8% similar

    Recursively groups a list of key tuples into a nested dictionary structure to optimize indexing operations by avoiding duplicate key lookups.

    From: /tf/active/vicechatdev/patches/util.py
  • function get_unique_keys 53.1% similar

    Extracts unique key values from an ndmapping object for specified dimensions, returning an iterator of unique tuples.

    From: /tf/active/vicechatdev/patches/util.py
  • function get_param_values 52.6% similar

    Extracts parameter values from a data object, including key dimensions (kdims), value dimensions (vdims), label, and optionally the group if it differs from the default.

    From: /tf/active/vicechatdev/patches/util.py
  • function layer_groups 47.8% similar

    Groups elements from an ordering list into a dictionary based on a slice of each element's specification, using the first 'length' items as the grouping key.

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