[英]How do you wrap existing functions on conditional logic in Python?
I have a codebase where this pattern is very common:我有一个代码库,这种模式很常见:
df # Some pandas dataframe with columns userId, sessionId
def add_session_statistics(df):
df_statistics = get_session_statistics(df.sessionId.unique())
return df.merge(df_statistics, on='sessionId', how='left')
def add_user_statistics(df):
df_statistics = add_user_statistics(df.userId.unique())
return df.merge(df_statistics, on='sessionId', how='left')
# etc..
df_enriched = (df
.pipe(add_session_statistics)
.pipe(add_user_statistics)
)
However, in another part of the codebase I have 'userId', 'sessionId' as the index of the dataframe.但是,在代码库的另一部分中,我将“userId”、“sessionId”作为数据帧的索引。 Something like:
就像是:
X = df.set_index(['userId', 'sessionId'])
This means I can't use the add_{somthing}_statistics()
functions on X
without resetting the index each time.这意味着我不能在
X
上使用add_{somthing}_statistics()
函数而不每次都重置索引。
Is there any decorator I can add to the add_{somthing}_statistics()
to make them reset the index if they get a KeyError
when attempting the merge on a column that is not there?是否有任何装饰器可以添加到
add_{somthing}_statistics()
以使他们在尝试合并不存在的列时遇到KeyError
时重置索引?
This seems to work:这似乎有效:
def index_suspension_on_add(add_function):
def _helper(df):
try:
return df.pipe(add_function)
except Exception:
index_names = df.index.names
return (df
.reset_index()
.pipe(add_function)
.set_index(index_names)
)
return _helper
@index_suspension_on_add
def add_user_statistics(df):
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.