forked from PSLmodels/Tax-Calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_taxcalc_output.py
124 lines (115 loc) · 4.59 KB
/
process_taxcalc_output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Translates tc --dump output file into file formatted like TAXSIM-35 output.
"""
# CODING-STYLE CHECKS:
# pycodestyle process_tc_output.py
# pylint --disable=locally-disabled process_tc_output.py
import argparse
import os
import sys
import pandas as pd
def main(input_file_name, output_file_name):
"""
Translates tc --dump output file into an output file that is
formatted like the first 28 variables in TAXSIM-35 output. The INPUT
file contains the output generated by running tc with the --dump
option. Any pre-existing OUTPUT file contents will be overwritten.
For details on Internet TAXSIM version 35 OUTPUT format, go to
https://users.nber.org/~taxsim/taxsim35/
Args:
input_file_name (string): name of file with taxcalc formatted output
output_file_name (string): name of file with taxsim formatted output
Returns:
None
"""
# check INPUT filename
if input_file_name == "":
sys.stderr.write("ERROR: must specify INPUT file name\n")
sys.stderr.write("USAGE: {}\n".format(usage_str))
assert False
if not os.path.isfile(input_file_name):
emsg = "INPUT file named {} does not exist".format(args.INPUT)
sys.stderr.write("ERROR: {}\n".format(emsg))
assert False
# check OUTPUT filename
if output_file_name == "":
sys.stderr.write("ERROR: must specify OUTPUT file name\n")
sys.stderr.write("USAGE: {}\n".format(usage_str))
assert False
if os.path.isfile(output_file_name):
os.remove(output_file_name)
# read INPUT file into a pandas DataFrame
tcvar = pd.read_csv(input_file_name)
# write OUTPUT file using the pandas DataFrame
write_taxsim_formatted_output(output_file_name, tcvar)
# return no-error exit code
return 0
# end of main function code
def write_taxsim_formatted_output(filename, tcvar):
"""
Write contents of tcvar pandas DataFrame to filename using
Internet-TAXSIM 9.3 output format containing 28 variables.
"""
assert isinstance(tcvar, pd.DataFrame)
# with open(filename, 'w') as output_file:
# for idx in range(0, len(tcvar.index)):
# odict4idx = extract_output(tcvar.xs(idx))
# outline = construct_output_line(odict4idx)
# output_file.write(outline)
tcvar["state"] = 0 # state code is always zero
tcvar["statetax"] = 0.0 # no state income tax calculation
tcvar["mtr_state"] = 0.0 # no state income tax calculation
tcvar["zero_bracket_amount"] = 0.0 # always set zero-bracket amount to zero
pre_phase_out_pe = tcvar["pre_c04600"].values
post_phase_out_pe = tcvar["c04600"].values
phased_out_pe = pre_phase_out_pe - post_phase_out_pe
tcvar["post_phase_out_pe"] = post_phase_out_pe # post-phase-out personal exemption
tcvar["phased_out_pe"] = phased_out_pe # personal exemption that is phased out
tcvar["exemption_surtax"] = 0.0 # always set exemption surtax to zero
tcvar["gen_tax_credit"] = 0.0 # always set general tax credit to zero
tcvar["non_refundable_child_odep_credit"] = (
tcvar["c07220"] + tcvar["odc"] + tcvar["ctc_new"]
) # non-refundable child+odep credit
tcvar["amt_liability"] = tcvar["c09600"] # federal AMT liability
# var28 from TAXSIM-35 is federal income tax before credits; the Tax-Calculator
# tcvar['c05800'] is this concept but includes AMT liability
# while Internet-TAXSIM tcvar[28] explicitly excludes AMT liability, so
# we have the following:
tcvar["iitax_before_credits_ex_AMT"] = tcvar["c05800"] - tcvar["amt_liability"]
tcvar = tcvar[
[
"RECID",
"FLPDYR",
"state",
"iitax",
"statetax",
"payrolltax",
"mtr_inctax",
"mtr_state",
# 'mtr_paytax',
"c00100",
"e02300",
"c02500",
# 'zero_bracket_amount',
"post_phase_out_pe",
"phased_out_pe",
"c21040",
"c04470",
"c04800",
"taxbc",
"exemption_surtax",
"gen_tax_credit",
"non_refundable_child_odep_credit",
"c11070",
"c07180",
"eitc",
"c62100",
"amt_liability",
"iitax_before_credits_ex_AMT",
"recovery_rebate_credit"
]
]
# better mapping of to how TAXSIM-35 handles refundalbe credits in 2021
tcvar.loc[tcvar["FLPDYR"] == 2021, "c11070"] = tcvar.loc[tcvar["FLPDYR"] == 2021, "non_refundable_child_odep_credit"]
tcvar.round(decimals=2)
tcvar.to_csv(filename)