-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*Branch is behind from main by many commits, which are needed for the imputation wrapper
- Loading branch information
Showing
23 changed files
with
275 additions
and
300 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import pandas as pd | ||
|
||
from src.utils.hdfs_mods import hdfs_load_json as read_json | ||
|
||
# TODO: read from config | ||
folder_path = "/dapsen/workspace_zone/mbs-results/" | ||
file_name = "snapshot-202212-002-2156d36b-e61f-42f1-a0f1-61d1f8568b8e.json" | ||
file_path = folder_path + file_name | ||
|
||
snapshot = read_json(file_path) | ||
|
||
contributors = pd.DataFrame(snapshot["contributors"]) | ||
responses = pd.DataFrame(snapshot["responses"]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ black | |
isort | ||
nbstripout | ||
nbqa | ||
#research_and_development==1.0.0 | ||
pre_commit_hooks | ||
flake8 | ||
pandas==1.1.5 | ||
|
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import pandas as pd | ||
|
||
|
||
def shift_by_strata_period( | ||
df: pd.DataFrame, | ||
target: str, | ||
period: str, | ||
strata: str, | ||
reference: str, | ||
time_difference: int, | ||
new_col: str, | ||
**kwargs | ||
) -> pd.DataFrame: | ||
""" | ||
It will perform the usual shift by desired time_difference for each value | ||
in strata and for consecutive period. | ||
Parameters | ||
---------- | ||
df : pd.DataFrame | ||
Pandas dataframe of original data | ||
target : str | ||
Column name containing target variable to be shifted. | ||
period : str | ||
Column name containing time period. | ||
strata : str | ||
Column name containing strata information (sic). | ||
reference : str | ||
Column name containing business reference id. | ||
time_difference : int | ||
Number of periods to shift. Can be positive or negative. | ||
new_col : str | ||
Column name containing the shifted values. | ||
kwargs : mapping, optional | ||
A dictionary of keyword arguments passed into func. | ||
Returns | ||
------- | ||
df : pd.DataFrame | ||
Pandas dataframe of original data with a new column containing the | ||
shifted values. | ||
""" | ||
|
||
df.sort_values([reference, strata, period], inplace=True) | ||
|
||
df[new_col] = df.groupby( | ||
( | ||
(df[period] - pd.DateOffset(months=1) != df.shift(1)[period]) | ||
| (df[strata].diff(1) != 0) | ||
| (df[reference].diff(1) != 0) | ||
).cumsum() | ||
).shift(time_difference)[target] | ||
|
||
return df |
Oops, something went wrong.