surveyweathertool.src.survey.helper

Module Contents

Functions

non_missing_stats(→ pandas.DataFrame)

Returns non-missing percentages in a dictionary for each column (passed in list) from the df

export_fn(df, output_filepath, file_extension, *kwargs)

Helper utility for exporting a dataframe to a specific format

dataframe_reader(→ pandas.DataFrame)

Read data from various file formats and return a DataFrame.

sortable_key_value_dictionary(→ dict)

Sorts a dictionary by either keys or values in ascending or descending order.

filter_dicts_by_keys(→ List[Dict[str, any]])

Filters a list of dictionaries by keeping only the specified keys of interest.

xml_to_json_converter(→ None)

Converts an XML file to JSON format and saves it to a file.

json_reader(→ dict)

Reads a JSON file and returns its content as a dictionary.

reorder_columns(dataframe)

Reorders the columns of a DataFrame based on the specified list of columns to appear first.

saving_data_formatter(→ None)

Save the given DataFrame to a file in different formats based on the extension.

surveyweathertool.src.survey.helper.non_missing_stats(df, list_of_cols) pandas.DataFrame

Returns non-missing percentages in a dictionary for each column (passed in list) from the df

surveyweathertool.src.survey.helper.export_fn(df: pandas.DataFrame, output_filepath: str, file_extension: str, *kwargs)

Helper utility for exporting a dataframe to a specific format

Currently supports ‘.csv’, ‘.pickle’, ‘.parquet’

Can pass arguments to the ‘to_X’ function as kwargs

surveyweathertool.src.survey.helper.dataframe_reader(file_path: pathlib.Path, use_columns: List[str] | None = None, reset_index=False, *kwargs) pandas.DataFrame

Read data from various file formats and return a DataFrame.

Parameters:

file_path (Path): The path to the input file. use_columns (Optional[List[str]]): A list of column names to include in the DataFrame.

If None, all columns will be included. Default is None.

reset_index (bool): Flag if want to reset index from original file. *kwargs (Optional[List]): Any keyword arguments to be passed to pandas reader function

Returns:

pd.DataFrame: A DataFrame containing the data from the input file.

Raises:

ValueError: If the file type is not supported.

Note:

Supported file formats: ‘.csv’, ‘.sav’, ‘.dta’, ‘.pickle’.

surveyweathertool.src.survey.helper.sortable_key_value_dictionary(dictionary: dict, by_keys: bool = True, reverse: bool = False) dict

Sorts a dictionary by either keys or values in ascending or descending order.

Args:

dictionary (dict): The input dictionary to be sorted. by_keys (bool, optional): Indicates whether to sort by keys (True) or values (False). Defaults to True. reverse (bool, optional): Indicates whether to sort in reverse order (descending). Defaults to False.

Returns:

dict: The sorted dictionary.

surveyweathertool.src.survey.helper.filter_dicts_by_keys(dicts: List[Dict[str, any]], keys_of_interest: List[str]) List[Dict[str, any]]

Filters a list of dictionaries by keeping only the specified keys of interest.

Args:

dicts (List[Dict[str, any]]): The list of dictionaries to filter. keys_of_interest (List[str]): The list of keys to keep in each dictionary.

Returns:

List[Dict[str, any]]: The list of dictionaries with only the specified keys.

surveyweathertool.src.survey.helper.xml_to_json_converter(xml_file_path: str, json_file_path: str) None

Converts an XML file to JSON format and saves it to a file.

Args:

xml_file_path (str): The path of the XML file to convert. json_file_path (str): The path of the JSON file to save.

Returns:

None

surveyweathertool.src.survey.helper.json_reader(path: str) dict

Reads a JSON file and returns its content as a dictionary.

Args:

path (str): The path of the JSON file to read.

Returns:

dict: The content of the JSON file as a dictionary.

surveyweathertool.src.survey.helper.reorder_columns(dataframe)

Reorders the columns of a DataFrame based on the specified list of columns to appear first.

Parameters:

dataframe (pd.DataFrame): The DataFrame whose columns need to be reordered.

Returns:

pd.DataFrame: The DataFrame with columns reordered.

surveyweathertool.src.survey.helper.saving_data_formatter(data: pandas.DataFrame, name: str, extension: str = 'csv', index: bool = False) None

Save the given DataFrame to a file in different formats based on the extension.

Args:

data (pd.DataFrame): The DataFrame to be saved. name (str): The name of the file without extension. extension (str, optional): The file format to save the DataFrame. Default is ‘csv’. index (bool, optional): Whether to include the index in the saved file. Default is False.

Returns:

None