Skip to content

Instantly share code, notes, and snippets.

@alessandrocucci
Created March 23, 2022 13:31
Show Gist options
  • Save alessandrocucci/1aa34a53eabde6453237e994dafba671 to your computer and use it in GitHub Desktop.
Save alessandrocucci/1aa34a53eabde6453237e994dafba671 to your computer and use it in GitHub Desktop.
Giacenza Media Revolut
"""
Script Python per il calcolo della giacenza media di un conto Revolut.
- scaricare la lista movimenti dell'anno da calcolare in CSV
- mettere il file nella stessa cartella di questo script python
- lanciare il comando (default anno precedente):
python revolut.py -d nomefile.csv
NB. Serve Pandas.
"""
import optparse
import pandas as pd
from datetime import date, timedelta
def daterange(start_date, end_date):
for n in range(int((end_date - start_date).days)):
yield start_date + timedelta(n)
def compute_giacenza(data_file, year):
start_date = date(year, 1, 1)
end_date = date(year, 12, 31)
df = pd.read_csv(data_file)
df['Completed Date'] = pd.to_datetime(df['Completed Date'])
df = df.groupby(df["Completed Date"].dt.date)["Balance"].max().reset_index()
balance = 0
giacenza = []
count = 0
for single_date in daterange(start_date, end_date):
count += 1
if (single_date in df['Completed Date'].values):
balance = df.loc[df['Completed Date'] == single_date].Balance.values[0]
balance = balance.replace(',', '.')
balance = float(balance)
giacenza.append(balance)
else:
giacenza.append(balance)
giacenza_media = sum(giacenza) / count
print("Giacenza Media = {}".format(giacenza_media))
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-d', '--data', help="CSV Data File")
parser.add_option('-y', '--year', help="Year", type="int", default=date.today().year - 1)
options, args = parser.parse_args()
compute_giacenza(options.data, options.year)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment