Created
March 3, 2014 18:10
-
-
Save acmisiti/9331044 to your computer and use it in GitHub Desktop.
Pandas bug + creating a list of dataframes from paystubs
This file contains hidden or 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
@login_required | |
@json_view | |
def api_generate_941_report(request): | |
""" Generate 941 report """ | |
params = {} | |
params['success'] = True | |
params['is_valid'] = True | |
company_id = request.REQUEST.get("company_id") | |
if company_id and request.user.is_superuser: | |
company = MCompany.objects.get(id=company_id) | |
else: | |
company = request.user.get_company() | |
try: | |
start = parse(request.REQUEST.get("start")) | |
end = parse(request.REQUEST.get("end")) | |
except: | |
return dict(success=False,error="Must specify start and end dates") | |
paystubs = MPaystub.objects.filter( Q(cid=company.id) & | |
Q(chck_dt__lte=end) & | |
Q(chck_dt__gte=start) & | |
Q(is_submitted=True)) | |
if not paystubs.count(): | |
return { | |
'success' : True, | |
'is_valid' : False | |
} | |
# aggregate paystub dictionaries for earnings, general deductions + garnishments | |
employee_earnings_results = pandas.concat( [pandas.DataFrame( stub.employee_earnings ) for stub in paystubs ] ) | |
employee_deductions_results = pandas.concat( [pandas.DataFrame( stub.employee_deductions, index=[0] ) for stub in paystubs if stub.employee_deductions ] ) | |
employee_garnishments_results = pandas.concat( [pandas.DataFrame( stub.employee_garnishments, index=[0] ) for stub in paystubs ] ) | |
employee_taxes_results = pandas.concat( [pandas.DataFrame( stub.employee_taxes ) for stub in paystubs ] ) | |
re_fit_reg = "00-000-0000-FIT-000-Res-Reg" | |
re_fit_sup = "00-000-0000-FIT-000-Res-Sup" | |
gross_compensation = round(employee_earnings_results['ste_gross']['ctd'].sum(),2) | |
federal_withholding = round(employee_taxes_results[re_fit_reg]['ctd'].sum() + employee_taxes_results[re_fit_sup]['ctd'].sum() ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you need to format the data frame so all the pay stub attributes are columns, and the different paystubs are rows, and then you can do something like this
df = pd.Dataframe(paystubs formatted as rows)
rows = df.start_date >= and df.end_date <= ....... more filters
results = df[rows].groupby(df[some attribute]).sum()
return results.to_json(orient='records') <--- returns json for display
or
return results.to_csv("path to csv", sep=",") <--- dumps to csv
The point is, the concatenation of paystubs should be done in mongo, and then returned and loaded into the DataFrame as a list of python dictionaries.