Created
October 13, 2019 17:32
-
-
Save solalatus/9a3fc5330e7c0cd83e61094db75d2dc3 to your computer and use it in GitHub Desktop.
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 pandas as pd | |
import numpy as np | |
class DiffNormalizer(): | |
def __init__(self, diff_len=1): | |
self.diff_len = diff_len | |
self.fitted = False | |
self.first_values = None | |
def fit_transform(self,in_df, dropna, columns=[]): | |
assert type(in_df)==pd.DataFrame | |
assert set(columns).issubset(set(in_df.columns)) or columns == [] | |
self.first_values = in_df.iloc[0,:] | |
self.fitted=True | |
if columns: | |
in_df = in_df[columns].diff(self.diff_len) | |
else: | |
in_df = in_df.diff(self.diff_len) | |
if dropna: | |
in_df.dropna(inplace=True) | |
return in_df | |
def inverse_transform(self, in_df, restorena, columns=[]): | |
if not self.fitted: | |
raise Exception('Trying to use inverse_transform but there was no fitting before!') | |
if not columns: | |
columns = in_df.columns | |
if restorena: | |
in_df = pd.concat([pd.DataFrame({k:[0] for k in columns}, columns=columns),in_df]) | |
for col in columns: | |
print("col",col) | |
cumsum = in_df[col].cumsum() | |
in_df[col] = cumsum.fillna(0) + self.first_values[col] | |
return in_df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment