Skip to content

Instantly share code, notes, and snippets.

@LosantGists
Created July 23, 2021 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LosantGists/fe176d5dba2b4603bb4cc69b65ae9f2d to your computer and use it in GitHub Desktop.
Save LosantGists/fe176d5dba2b4603bb4cc69b65ae9f2d to your computer and use it in GitHub Desktop.
Automating Unit Tests Blog (9) 7.23.21
def is_row_before_charging(row):
return (row.battery_loss_next_row < 0) and (row.battery_loss_this_row > 0)
def remove_consecutive_same_values(df):
df['is_new_value'] = df.groupby(['ID'])['battery'].shift(1) != df['battery']
return df.loc[df['is_new_value']]
def get_rows_before_charging(df):
# sorting not necessary, but is much easier to follow during dev
# df.sort_values(by=['ID','Timestamp'],inplace=True)
df.loc[:] = remove_consecutive_same_values(df)
df['battery_loss_this_row'] = df.groupby(['ID'])['battery'].shift(1) - df['battery']
df['battery_loss_next_row'] = df['battery'] - df.groupby(['ID'])['battery'].shift(-1)
df.loc[:, 'is_low_before_charge'] = df.apply(is_row_before_charging, axis=1)
return df[df['is_low_before_charge']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment