Last active
July 15, 2020 10:47
-
-
Save gautierdag/84aa55297babc5fb211bac67e08b00f7 to your computer and use it in GitHub Desktop.
Numpy Optimized Compress
This file contains 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
import numpy as np | |
#cast as object to be able to set np.nan and handle different types | |
arr = df[cols].values.astype(object) | |
# build shifted array | |
shifted = np.roll(arr, 1) | |
shifted[0] = np.nan | |
# choose indexes based on shift comparaison | |
indexes = np.where(~(arr == shifted).all(axis=1))[0] | |
compressed_df = df.loc[indexes].reset_index(drop=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment