Skip to content

Commit

Permalink
update typing information
Browse files Browse the repository at this point in the history
  • Loading branch information
andersy005 committed Nov 3, 2023
1 parent d13d8df commit b2693d4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
22 changes: 13 additions & 9 deletions offsets_db_data/credits.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@


@pf.register_dataframe_method
def filter_and_merge_transactions(df, arb_data, project_id_column: str = 'project_id'):
def filter_and_merge_transactions(
df: pd.DataFrame, arb_data: pd.DataFrame, project_id_column: str = 'project_id'
) -> pd.DataFrame:
if intersection_values := list(
set(df[project_id_column]).intersection(set(arb_data[project_id_column]))
):
Expand All @@ -26,7 +28,7 @@ def filter_and_merge_transactions(df, arb_data, project_id_column: str = 'projec


@pf.register_dataframe_method
def aggregate_issuance_transactions(df):
def aggregate_issuance_transactions(df: pd.DataFrame) -> pd.DataFrame:
# Check if 'transaction_type' exists in DataFrame columns
if 'transaction_type' not in df.columns:
raise KeyError("The column 'transaction_type' is missing.")
Expand All @@ -53,13 +55,13 @@ def aggregate_issuance_transactions(df):


@pf.register_dataframe_method
def handle_non_issuance_transactions(df):
def handle_non_issuance_transactions(df: pd.DataFrame) -> pd.DataFrame:
df = df.copy()
df_non_issuance = df[df['transaction_type'] != 'issuance']
return df_non_issuance


def calculate_verra_issuances(*, df):
def calculate_verra_issuances(*, df: pd.DataFrame) -> pd.DataFrame:
"""Logic to calculate verra transactions from prepocessed transaction data
Verra allows rolling/partial issuances. This requires inferring vintage issuance from `Total Vintage Quantity`
Expand All @@ -78,14 +80,14 @@ def calculate_verra_issuances(*, df):
return df_issuance


def calculate_verra_retirements(*, df):
def calculate_verra_retirements(*, df: pd.DataFrame) -> pd.DataFrame:
"""retirements + cancelations, but data doesnt allow us to distinguish the two"""
retirements = df[df['transaction_type'] != 'issuance']
retirements = retirements.rename(columns={'Quantity Issued': 'quantity'})
return retirements


def preprocess_verra_transactions(*, df):
def preprocess_verra_transactions(*, df: pd.DataFrame) -> pd.DataFrame:
"""Preprocess Verra transactions data"""

df = df.copy()
Expand All @@ -112,7 +114,7 @@ def preprocess_verra_transactions(*, df):
return df


def preprocess_gold_standard_transactions(*, df, download_type):
def preprocess_gold_standard_transactions(*, df: pd.DataFrame, download_type: str) -> pd.DataFrame:
"""Preprocess Gold Standard transactions data"""
df = df.copy()
df['project'] = df['project'].apply(lambda x: x if isinstance(x, dict) else ast.literal_eval(x))
Expand All @@ -137,7 +139,7 @@ def add_gcc_project_id(*, transactions, projects):
return transactions


def preprocess_gcc_transactions(*, df, download_type):
def preprocess_gcc_transactions(*, df: pd.DataFrame, download_type: str) -> pd.DataFrame:
"""Preprocess GCC transactions data"""
df = df.copy()

Expand Down Expand Up @@ -166,7 +168,9 @@ def preprocess_gcc_transactions(*, df, download_type):
return df


def preprocess_apx_transactions(*, df, download_type, registry_name):
def preprocess_apx_transactions(
*, df: pd.DataFrame, download_type: str, registry_name: str
) -> pd.DataFrame:
transaction_type_mapping = {
'issuances': 'issuance',
'retirements': 'retirement',
Expand Down
17 changes: 9 additions & 8 deletions offsets_db_data/projects.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import json
from collections import defaultdict

Expand Down Expand Up @@ -138,7 +139,7 @@ def add_category(df, protocol_mapping) -> pd.DataFrame:


@pf.register_dataframe_method
def add_is_compliance_flag(df) -> pd.DataFrame:
def add_is_compliance_flag(df: pd.DataFrame) -> pd.DataFrame:
"""Add is_arb flag"""
print('Adding is_compliance flag...')
df['is_compliance'] = df.apply(
Expand All @@ -150,7 +151,9 @@ def add_is_compliance_flag(df) -> pd.DataFrame:

@pf.register_dataframe_method
def map_protocol(
df, inverted_protocol_mapping, original_protocol_column: str = 'original_protocol'
df: pd.DataFrame,
inverted_protocol_mapping: dict,
original_protocol_column: str = 'original_protocol',
) -> pd.DataFrame:
"""Map protocol based on known string"""
print('Mapping protocol based on known string...')
Expand All @@ -166,13 +169,13 @@ def map_protocol(


@pf.register_dataframe_method
def harmonize_status_codes(df, status_column: str = 'status') -> pd.DataFrame:
def harmonize_status_codes(df: pd.DataFrame, status_column: str = 'status') -> pd.DataFrame:
"""Harmonize project status codes across registries
Excludes ACR, as it requires special treatment across two columns
"""
print('Harmonizing status codes')
try:
with contextlib.suppress(KeyError):
GCC_STATES = {
'VERIFICATION': 'listed',
'RFR CC INCOMPLETE': 'unknown',
Expand Down Expand Up @@ -210,8 +213,6 @@ def harmonize_status_codes(df, status_column: str = 'status') -> pd.DataFrame:

state_dict = GCC_STATES | CAR_STATES | VERRA_STATES | GS_STATES
df[status_column] = df[status_column].apply(lambda x: state_dict.get(x, 'unknown'))
except KeyError: # status not in raw data
pass
return df


Expand Down Expand Up @@ -254,7 +255,7 @@ def _get_category(protocol_str, protocol_mapping):
return [_get_category(protocol_str, protocol_mapping) for protocol_str in protocol_strs]


def harmonize_acr_status(row):
def harmonize_acr_status(row: pd.Series) -> str:
"""Derive single project status for CAR and ACR projects
Raw CAR and ACR data has two status columns -- one for compliance status, one for voluntary.
Expand Down Expand Up @@ -299,7 +300,7 @@ def load_protocol_mapping(path: upath.UPath = PROTOCOL_MAPPING_UPATH) -> dict:
return json.loads(path.read_text())


def load_inverted_protocol_mapping():
def load_inverted_protocol_mapping() -> dict:
protocol_mapping = load_protocol_mapping()
store = defaultdict(list)
for protocol_str, metadata in protocol_mapping.items():
Expand Down

0 comments on commit b2693d4

Please sign in to comment.