Last active
January 20, 2022 08:58
-
-
Save toyo97/3f876f3e079b96752cd1ed0f7f03d8b6 to your computer and use it in GitHub Desktop.
Giacenza media annua conto revolut. Richiede pandas
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
# python3 rev-gma.py <nome-file> <anno> | |
# gist github: https://gist.github.com/toyo97/3f876f3e079b96752cd1ed0f7f03d8b6 | |
import sys | |
import pandas as pd | |
from datetime import datetime, date | |
file_name = sys.argv[1] | |
year = int(sys.argv[2]) | |
cols = ['Completed Date', 'Currency', 'Balance'] | |
date_format = '%Y-%m-%d %H:%M:%S' | |
data = pd.read_csv(file_name, sep=',')[cols].dropna() | |
data['Completed Date'] = pd.to_datetime(data['Completed Date']) | |
data = data[data['Completed Date'].apply(lambda x: x.year) == year] | |
currencies = data['Currency'].unique() | |
for curr in currencies: | |
df = data[data['Currency'] == curr].sort_values(by='Completed Date') | |
acc = 0 | |
print(f'=== {curr} ===') | |
end = date(year, 1, 1) | |
first_date = end | |
for i in range(len(df) - 1): | |
end = df.iloc[i+1, 0] | |
start = df.iloc[i, 0] | |
end = date(end.year, end.month, end.day) | |
start = date(start.year, start.month, start.day) | |
if i == 0: | |
first_date = start | |
delta = (end - start).days # in days | |
if delta == 0: | |
continue | |
curr_balance = df.iloc[i, 2] | |
inc = delta * curr_balance | |
print(f'{start} to {end}: {curr_balance:.2f} x {delta} = {inc:.2f}') | |
acc += inc | |
last_balance = df.iloc[-1, 2] | |
acc += last_balance * (date(year, 12, 31) - end).days | |
tot_days = (date(year, 12, 31) - first_date).days | |
print(f'GMA: {acc / 365:.2f} {curr}, saldo al 31/12/{year}: {last_balance} {curr}\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment