function unpack_group
Unpacks a pandas DataFrame group by iterating over rows and yielding tuples of keys and objects, with special handling for objects with 'kdims' attribute.
/tf/active/vicechatdev/patches/util.py
1802 - 1809
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class ndmapping_groupby 59.4% similar
-
function group_select 55.8% similar
-
function get_unique_keys 53.1% similar
-
function get_param_values 52.6% similar
-
function layer_groups 47.8% similar