Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import perfplot
import pandas as pd
import re
p1 = re.compile(r'\D')
p2 = re.compile(r'\d+')
def eumiro(df):
return df.assign(
result=df['result'].map(lambda x: x.lstrip('+-').rstrip('aAbBcC')))
def coder375(df):
return df.assign(
result=df['result'].replace(r'\D', r'', regex=True))
def monkeybutter(df):
return df.assign(result=df['result'].map(lambda x: x[1:-1]))
def wes(df):
return df.assign(result=df['result'].str.lstrip('+-').str.rstrip('aAbBcC'))
def cs1(df):
return df.assign(result=df['result'].str.replace(r'\D', ''))
def cs2_ted(df):
# `str.extract` based solution, similar to @Ted Petrou's. so timing together.
return df.assign(result=df['result'].str.extract(r'(\d+)', expand=False))
def cs1_listcomp(df):
return df.assign(result=[p1.sub('', x) for x in df['result']])
def cs2_listcomp(df):
return df.assign(result=[[0] for x in df['result']])
def cs_eumiro_listcomp(df):
return df.assign(
result=[x.lstrip('+-').rstrip('aAbBcC') for x in df['result']])
def cs_mb_listcomp(df):
return df.assign(result=[x[1:-1] for x in df['result']])
df_ = pd.DataFrame({
'time': ['09:00', '10:00', '11:00', '12:00', '13:00'],
'result': ['+52A', '+62B', '+44a', '+30b', '-110a']
kernels = [
eumiro, coder375, monkeybutter, wes, cs1, cs2_ted,
cs1_listcomp, cs2_listcomp, cs_eumiro_listcomp, cs_mb_listcomp
setup=lambda n: pd.concat([df_] * n, ignore_index=True),
labels=[k.__name__ for k in kernels],
n_range=[2**k for k in range(1, 15)],
equality_check=lambda x, y: (x == y).all(axis=None),)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment