Created
July 23, 2021 19:26
-
-
Save LosantGists/fe176d5dba2b4603bb4cc69b65ae9f2d to your computer and use it in GitHub Desktop.
Automating Unit Tests Blog (9) 7.23.21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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