Last active
February 24, 2020 02:33
-
-
Save gshimansky/d38ae268cc39bf534b76bbc4a9ee1f46 to your computer and use it in GitHub Desktop.
Convert income and taxes from roubles to USD using official conversion rate table
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
#!/usr/bin/env python3 | |
import pandas as pd | |
import argparse | |
def main(): | |
# Parse command line | |
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, | |
description=""" | |
Convert RUR income and taxes, sums them and substracts. | |
Convesion courses can be found here https://www.cbr.ru/eng/currency_base/dynamics/ | |
Download XLS for the required period and then export it as a CSV.""", | |
epilog=""" | |
Input CSV files are accepted in the following form: | |
date,sum | |
30.11.2000,1000000 | |
31.12.2000,2000000 | |
""") | |
parser.add_argument("-c", required=True, dest="courses", help="Courses CSV file for the year") | |
parser.add_argument("-i", required=True, dest="income", help="Income CSV file with dates and sums in RUR") | |
parser.add_argument("-t", required=True, dest="taxes", help="Taxes CSV file with dates and sums in RUR") | |
parser.add_argument("-courses_format", choices=["ddmmyy", "mmddyy"], default="mmddyy", required=False, | |
help="Format of dates in courses data file") | |
parser.add_argument("-roubles_format", choices=["ddmmyy", "mmddyy"], default="mmddyy", required=False, | |
help="Format of dates in roubles income and taxes data files") | |
args = parser.parse_args() | |
# Read data | |
courses_names = ["nominal", "date", "rate", "currency"] | |
courses_types = { | |
"nominal": "int64", | |
"date": "str", | |
"rate": "float64", | |
"currency": "str" | |
} | |
courses = pd.read_csv(args.courses, header=0, names=courses_names, dtype=courses_types, | |
index_col=1, parse_dates=[1], dayfirst=(args.courses_format == "ddmmyy")) | |
roubles_names = ["date", "sum"] | |
roubles_types = { | |
"date": "str", | |
"sum": "float64" | |
} | |
income = pd.read_csv(args.income, header=0, names=roubles_names, dtype=roubles_types, | |
index_col=0, parse_dates=[0], dayfirst=(args.roubles_format == "ddmmyy")) | |
taxes = pd.read_csv(args.taxes, header=0, names=roubles_names, dtype=roubles_types, | |
index_col=0, parse_dates=[0], dayfirst=(args.roubles_format == "ddmmyy")) | |
# Do conversions | |
def convert(source, rates, name): | |
# Sum up all operations for one day | |
source = source.groupby(['date']).sum() | |
# Fill up missing dates looking up conversion rate for previous dates | |
rates_filled = rates.reindex(source.index, method='ffill') | |
usd = (source['sum'] / rates_filled.loc[source.index]['rate']).round(decimals=2) | |
print(name, "in USD is\n", usd) | |
return usd | |
usd_income = convert(income, courses, "Income") | |
usd_taxes = convert(taxes, courses, "Tax") | |
# Sum up income, substract taxes, output everything | |
total_income = usd_income.sum() | |
print("Total income in USD is", total_income) | |
total_taxes = usd_taxes.sum() | |
print("Total taxes in USD are", total_taxes) | |
print("Income minus taxes: ", total_income - total_taxes) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment