pumpwood_communication.misc

Miscelaneus function to help in development.

 1"""Miscelaneus function to help in development."""
 2import math
 3import copy
 4import pandas as pd
 5import numpy as np
 6from typing import List, Literal
 7from pumpwood_communication.exceptions import PumpWoodException
 8
 9
10def unpack_dict_columns(df: pd.DataFrame, columns: List[str]) -> pd.DataFrame:
11    """Unpack dictinary columns at a dataframe.
12
13    Return a copy of the dataframe with 'columns' unpacked and removed
14    from result.
15
16    Args:
17        df (pd.DataFrame):
18            Dataframe to unpack the columns.
19        columns (List[str]):
20            List of columns to unpack in columns.
21
22    Return:
23        Return a dataframe with dict columns unpacked.
24    """
25    list_unpacked_results = []
26    for c in columns:
27        list_unpacked_results.append(df[c].apply(pd.Series))
28    return pd.concat(
29        [df.drop(columns=columns)] + list_unpacked_results,
30        axis=1)
31
32
33def extract_dict_subset(data: dict, keys: List[str],
34                        raise_not_present: Literal[
35                            'raise', 'ignore', 'add_none'] = 'raise'):
36    """Extract a subset of data from dictonary.
37
38    Args:
39        data (dict):
40            Dictionary data from which the subset will be extracted.
41        keys (List[str]):
42            Keys that will be extracted.
43        raise_not_present (str):
44            Control if an erros should be raised when key is not present.
45            - 'raise': Raise error
46            - 'ignore': Does not return the missing key on function result.
47            - 'add_none': Return key with None value.
48
49    Returns:
50        Return a dictonary with a copy of subset of the keys of original
51        dictonary.
52    """
53    if raise_not_present not in ['raise', 'ignore', 'add_none']:
54        msg = (
55            'raise_not_present must be in [raise, ignore, add_none].'
56            'raise_not_present={raise_not_present}')
57        raise PumpWoodException(
58            message=msg, payload={
59                'raise_not_present': raise_not_present})
60
61    return_dict = {}
62    for key in keys:
63        temp_data = data.get(key)
64        if temp_data is None:
65            if raise_not_present == 'raise':
66                msg = (
67                    'key [{key}] not found on dictonary and raise_not_present '
68                    'arg is set as [{raise_not_present}]')
69                raise PumpWoodException(
70                    message=msg, payload={
71                        'key': key, 'raise_not_present': raise_not_present})
72            if raise_not_present == 'ignore':
73                continue
74            if raise_not_present == 'add_none':
75                return_dict[key] = None
76        else:
77            return_dict[key] = copy.deepcopy(temp_data)
78    return return_dict
79
80
81def break_in_chunks(df_to_break: pd.DataFrame,
82                    chunksize: int = 1000) -> List[pd.DataFrame]:
83    """Break a dataframe in chunks of chunksize.
84
85    Args:
86        df_to_break: Dataframe to be break in chunks of `chunksize` size.
87        chunksize: Length of each chuck of the breaks of `df_to_break`.
88
89    Returns:
90        Return a list dataframes with length chunksize of data from
91        `df_to_break`.
92    """
93    return [
94        df_to_break.iloc[i : i + chunksize]
95        for i in range(0, len(df_to_break), chunksize)
96    ]
def unpack_dict_columns(df: pandas.DataFrame, columns: List[str]) -> pandas.DataFrame:
11def unpack_dict_columns(df: pd.DataFrame, columns: List[str]) -> pd.DataFrame:
12    """Unpack dictinary columns at a dataframe.
13
14    Return a copy of the dataframe with 'columns' unpacked and removed
15    from result.
16
17    Args:
18        df (pd.DataFrame):
19            Dataframe to unpack the columns.
20        columns (List[str]):
21            List of columns to unpack in columns.
22
23    Return:
24        Return a dataframe with dict columns unpacked.
25    """
26    list_unpacked_results = []
27    for c in columns:
28        list_unpacked_results.append(df[c].apply(pd.Series))
29    return pd.concat(
30        [df.drop(columns=columns)] + list_unpacked_results,
31        axis=1)

Unpack dictinary columns at a dataframe.

Return a copy of the dataframe with 'columns' unpacked and removed from result.

Arguments:
  • df (pd.DataFrame): Dataframe to unpack the columns.
  • columns (List[str]): List of columns to unpack in columns.
Return:

Return a dataframe with dict columns unpacked.

def extract_dict_subset( data: dict, keys: List[str], raise_not_present: Literal['raise', 'ignore', 'add_none'] = 'raise'):
34def extract_dict_subset(data: dict, keys: List[str],
35                        raise_not_present: Literal[
36                            'raise', 'ignore', 'add_none'] = 'raise'):
37    """Extract a subset of data from dictonary.
38
39    Args:
40        data (dict):
41            Dictionary data from which the subset will be extracted.
42        keys (List[str]):
43            Keys that will be extracted.
44        raise_not_present (str):
45            Control if an erros should be raised when key is not present.
46            - 'raise': Raise error
47            - 'ignore': Does not return the missing key on function result.
48            - 'add_none': Return key with None value.
49
50    Returns:
51        Return a dictonary with a copy of subset of the keys of original
52        dictonary.
53    """
54    if raise_not_present not in ['raise', 'ignore', 'add_none']:
55        msg = (
56            'raise_not_present must be in [raise, ignore, add_none].'
57            'raise_not_present={raise_not_present}')
58        raise PumpWoodException(
59            message=msg, payload={
60                'raise_not_present': raise_not_present})
61
62    return_dict = {}
63    for key in keys:
64        temp_data = data.get(key)
65        if temp_data is None:
66            if raise_not_present == 'raise':
67                msg = (
68                    'key [{key}] not found on dictonary and raise_not_present '
69                    'arg is set as [{raise_not_present}]')
70                raise PumpWoodException(
71                    message=msg, payload={
72                        'key': key, 'raise_not_present': raise_not_present})
73            if raise_not_present == 'ignore':
74                continue
75            if raise_not_present == 'add_none':
76                return_dict[key] = None
77        else:
78            return_dict[key] = copy.deepcopy(temp_data)
79    return return_dict

Extract a subset of data from dictonary.

Arguments:
  • data (dict): Dictionary data from which the subset will be extracted.
  • keys (List[str]): Keys that will be extracted.
  • raise_not_present (str): Control if an erros should be raised when key is not present.
    • 'raise': Raise error
    • 'ignore': Does not return the missing key on function result.
    • 'add_none': Return key with None value.
Returns:

Return a dictonary with a copy of subset of the keys of original dictonary.

def break_in_chunks( df_to_break: pandas.DataFrame, chunksize: int = 1000) -> List[pandas.DataFrame]:
82def break_in_chunks(df_to_break: pd.DataFrame,
83                    chunksize: int = 1000) -> List[pd.DataFrame]:
84    """Break a dataframe in chunks of chunksize.
85
86    Args:
87        df_to_break: Dataframe to be break in chunks of `chunksize` size.
88        chunksize: Length of each chuck of the breaks of `df_to_break`.
89
90    Returns:
91        Return a list dataframes with length chunksize of data from
92        `df_to_break`.
93    """
94    return [
95        df_to_break.iloc[i : i + chunksize]
96        for i in range(0, len(df_to_break), chunksize)
97    ]

Break a dataframe in chunks of chunksize.

Arguments:
  • df_to_break: Dataframe to be break in chunks of chunksize size.
  • chunksize: Length of each chuck of the breaks of df_to_break.
Returns:

Return a list dataframes with length chunksize of data from df_to_break.