From 303aa2ce7f0f98b9c695194afc0cbf3ffc65e1b9 Mon Sep 17 00:00:00 2001 From: Olexandr Balyk Date: Wed, 29 Nov 2023 01:43:44 -0500 Subject: [PATCH] Optimise imports in utils; address code warnings (#142) --- times_reader/transforms.py | 2 +- times_reader/utils.py | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/times_reader/transforms.py b/times_reader/transforms.py index da1b07b..4100d02 100644 --- a/times_reader/transforms.py +++ b/times_reader/transforms.py @@ -627,7 +627,7 @@ def process_flexible_import_tables( # Get a list of allowed values for each category. legal_values = { "limtype": {"LO", "UP", "FX"}, - "timeslice": utils.timeslices(tables), + "timeslice": utils.extract_timeslices(tables), "commodity-out": set( utils.merge_columns(tables, datatypes.Tag.fi_comm, "commodity") ), diff --git a/times_reader/utils.py b/times_reader/utils.py index cb170eb..d69786e 100644 --- a/times_reader/utils.py +++ b/times_reader/utils.py @@ -1,15 +1,13 @@ -from pandas.core.frame import DataFrame -import pandas as pd +import re from dataclasses import replace +from math import log10, floor from typing import Iterable, List -from more_itertools import locate, one -from itertools import groupby + import numpy -import re -import os -from concurrent.futures import ProcessPoolExecutor -from functools import reduce -from math import log10, floor +import pandas as pd +from more_itertools import one +from pandas.core.frame import DataFrame + from . import datatypes @@ -61,13 +59,13 @@ def explode(df, data_columns): names = pd.Series(data_columns * nrows, index=df.index, dtype=str) # Remove rows with no VALUE - filter = df[value_column].notna() - df = df[filter] - names = names[filter] + index = df[value_column].notna() + df = df[index] + names = names[index] return df, names -def timeslices(tables: List[datatypes.EmbeddedXlTable]): +def extract_timeslices(tables: List[datatypes.EmbeddedXlTable]): """ Given a list of tables with a unique table with a time slice tag, return a list with all the column names of that table + "ANNUAL". @@ -142,7 +140,7 @@ def apply_wildcards( wildcard_map = {} all_wildcards = df[wildcard_col].unique() for wildcard_string in all_wildcards: - if wildcard_string == None: + if wildcard_string is None: wildcard_map[wildcard_string] = None else: wildcard_list = wildcard_string.split(",") @@ -174,7 +172,7 @@ def missing_value_inherit(df: DataFrame, colname: str): """ last = None for index, value in df[colname].items(): - if value == None: + if value is None: df.loc[index, colname] = last else: last = value