I have a function that takes an indexed pandas.Series
of things and a dataframe of stuff that I want to use the things on group by group. It is common for the dataframe to contain groups for which there is no matching thing, so a simple list comprehension will often throw exceptions.
My Python is pretty rusty. Is code like this considered normal? It strikes me that it might be replicating some library function that I should use instead. Or just that it might be bad practice compared to some other way. My justification for the local function is that it has no use aside from being a helper function for the list comprehension, so why make it global?
def use_things(things, stuff_to_use_things_on):
stuff_grouped = stuff_to_use_things_on.groupby(things.index.names)
# find and use the right thing, or else move on
# local closure since this function has no real use outside this context
def use_thing(name, group):
try:
return things.loc[(name)].do_something(group)
except:
return None
# stuff might contain groups that there is no thing for
results = [use_thing(name, group) for (name, group) in stuff_grouped]
return pd.concat(results)