-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwithdraw_functions.py
87 lines (58 loc) · 2.75 KB
/
withdraw_functions.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
import datetime
from typing import List
import gspread
import pandas as pd
import streamlit as st
from gspread_dataframe import get_as_dataframe
def load_withdraws():
sheet_credentials = st.secrets["sheet_credentials"]
gc = gspread.service_account_from_dict(sheet_credentials)
pepper_workbook = gc.open_by_key(st.secrets["sheet_key"])
deposits_sheet = pepper_workbook.worksheet("Withdraws")
withdraw_df = get_as_dataframe(deposits_sheet, parse_dates=True)
withdraw_df.drop(columns=["Timestamp"], inplace=True)
withdraw_df['Date'] = pd.to_datetime(withdraw_df['Date'], format='%d/%m/%y')
result = withdraw_df.dropna(how='all')
return result
def format_date(input_date):
# Convert the input string to a datetime object
# date_object = datetime.strptime(input_date, '%d/%m/%y')
# Format the datetime object as required (8/Oct/2023)
formatted_date = input_date.strftime('%d/%b/%Y')
return formatted_date
def process_withdraw_month(withdraw_df, month):
# Ensure the Date column is in datetime format
withdraw_df['Date'] = pd.to_datetime(withdraw_df['Date'], format='%d/%b/%Y')
withdraw_df['Month'] = withdraw_df['Date'].dt.month_name()
# Filter the DataFrame for the given month
filtered_df = withdraw_df[withdraw_df['Month'] == month]
# Sort the filtered DataFrame by Date in descending order
filtered_df = filtered_df.sort_values(by='Date', ascending=False)
# Reset the index and drop the old index
filtered_df = filtered_df.reset_index(drop=True)
filtered_df.index += 1
# Drop the temporary 'Month' column
filtered_df = filtered_df.drop(columns=['Month'])
filtered_df["Date"] = filtered_df["Date"].apply(format_date)
return filtered_df
def display_expander(month, month_df):
ttl_withdraw = month_df["Amount"].sum()
formatted_ttl_withdraw = "{:,.0f}".format(ttl_withdraw)
with st.expander(f'{month} - {formatted_ttl_withdraw} ugx'):
st.dataframe(month_df, use_container_width=True)
def filter_data(data: pd.DataFrame, filter_name: str, values: List[str]) -> pd.DataFrame:
if not values:
return data
if filter_name == "years":
data = data[data['Date'].dt.year.isin(values)]
if filter_name == "months":
data = data[data['Date'].dt.month.isin(values)]
if filter_name == "start_date":
date_string = str(values)
formatted_start_date = datetime.datetime.strptime(date_string, "%Y-%m-%d").strftime("%d/%m/%Y")
data = data[data['Date'] >= formatted_start_date]
if filter_name == "end_date":
date_string = str(values)
formatted_start_date = datetime.datetime.strptime(date_string, "%Y-%m-%d").strftime("%d/%m/%Y")
data = data[data['Date'] <= formatted_start_date]
return data