Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to create diff tables #12

Merged
merged 1 commit into from
Sep 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions taxcrunch/multi_cruncher.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,40 @@ def create_table(self, reform_file=None):
df_res.index = range(self.rows)
return df_res

def create_diff_table(self, reform_file, baseline=None):
"""
Creates a table that displays differences between baseline and reform.

Baseline default is current law.

The reform_file argument can be the name of a reform file in the
Tax-Calculator reforms folder, a file path to a custom JSON
reform file, or a dictionary with a policy reform.

Returns:
df_diff_id: a Pandas dataframe.
"""
if baseline is None:
t_base = self.create_table()
else:
t_base = self.create_table(baseline)
t_reform = self.create_table(reform_file)
df_all = pd.merge(t_reform, t_base, on='ID')
df_ids = df_all['ID']
cols = len(t_base.columns)
df_diff = df_all.diff(periods= -(cols - 1),
axis=1).iloc[:, 1:cols]
df_diff_id = pd.concat([df_ids, df_diff], axis=1)
# new column labels that have "Diff" at the end
diff_labels = ['ID']
for label in self.labels:
if label == 'ID':
pass
else:
diff_labels.append(label + " Diff")
df_diff_id.columns = diff_labels
return df_diff_id

def write_output_file(self, output_filename=None, reform_file=None):
"""
Writes an output table as a csv. Like the create_table() method, default is current
Expand All @@ -169,6 +203,15 @@ def write_output_file(self, output_filename=None, reform_file=None):
assert isinstance(df_res, pd.DataFrame)
df_res.to_csv(output_filename, index=False, float_format='%.2f')

def write_diff_file(self, reform_file, baseline=None, output_filename=None):
if output_filename is None:
today = date.today()
today_str = today.strftime("%m-%d-%Y")
output_filename = "cruncher-diff-" + today_str + ".csv"
df_res = self.create_diff_table(reform_file=reform_file, baseline=baseline)
assert isinstance(df_res, pd.DataFrame)
df_res.to_csv(output_filename, index=False, float_format='%.2f')

def get_pol(self, reform_file):
"""
Reads the specified reform and implements it
Expand Down