On this page Execution sequence

API Reference / mespy.load_csv

Implementation

This page describes the internal flow of load_csv and the order in which checks are applied. Unlike Overview, the goal here is not to repeat the parameter list, but to clarify how the function builds and validates the DataFrame.

Execution sequence

The implementation follows a simple and predictable sequence:

  1. It immediately checks that missing is one of the supported policies: "error", "drop", or "allow".

  2. Calls pd.read_csv(...) with:

    • Path(path) as the file path

    • sep

    • decimal

    • comment

    • skipinitialspace=skip_initial_space

  3. If rename_columns is provided, it renames the DataFrame columns.

  4. If required_columns is provided, it checks that all required columns are present after any renaming.

  5. Applies the missing-value policy:

    • with missing="drop" it returns df.dropna()

    • with missing="error" it checks whether at least one NaN exists and, if so, raises ValueError

    • with missing="allow" it leaves the DataFrame unchanged

  6. Returns the final DataFrame.

In compact form, the behavior is equivalent to this scheme:

if missing not in {"error", "drop", "allow"}:
    raise ValueError(...)

df = pd.read_csv(
    Path(path),
    sep=sep,
    decimal=decimal,
    comment=comment,
    skipinitialspace=skip_initial_space,
)

if rename_columns:
    df = df.rename(columns=rename_columns)

if required_columns:
    ...

if missing == "drop":
    return df.dropna()

if missing == "error" and df.isna().any().any():
    raise ValueError(...)

return df

Order of checks

The order of the steps is part of the practical contract of the function.

  • path is the only positional parameter: all the others are keyword-only.

  • Validation of missing happens before reading the file. If the policy is not supported, the function fails immediately.

  • required_columns is checked after rename_columns. This allows you to request columns by their final name, not their original file name.

  • comment and skip_initial_space do not introduce additional logic: they are controlled passthroughs to pandas.read_csv.

  • With missing="error", the check for NaN values takes place on the loaded DataFrame, rather than during parsing.

This order makes the behavior easy to predict in notebooks and scripts: first you import the file, then you normalize the names, then you check the structure, and only at the end you decide how to treat the missing values.

Missing-value policy

The management of the NaN is explicit and depends entirely on the missing parameter.

  • missing="error": this is the default. After loading, the function controls the entire DataFrame with df.isna().any().any(). If it finds at least one missing value, it raises ValueError.

  • missing="drop": loading does not fail due to the presence of NaN. Incomplete lines are deleted with DataFrame.dropna(), and the returned DataFrame may therefore have fewer lines than the original file.

  • missing="allow": the function does not remove anything and does not raise errors for the NaN. The missing values remain in the DataFrame and are managed downstream if necessary.

From an operational point of view:

  • error is useful when the file needs to be complete before moving to analysis.

  • drop is useful when you want a conservative but not blocking behavior.

  • allow is useful when missing-data handling is delegated to later steps.

If missing receives a value other than "error", "drop" or "allow", the function immediately raises ValueError without attempting to read the CSV.

Interactions between parameters

Some combinations of parameters define the most important practical behavior of the function.

Renaming and required columns

Renaming takes place before the check on required_columns. For example, if the file contains a column x and you pass rename_columns={"x": "alfa"}, then required_columns=["alfa"] is valid. This avoids having to reason about original names and final names at the same time.

If a required column is still missing after renaming, the function raises ValueError with the list of missing columns.

Delimiter and decimal separator

sep and decimal are forwarded directly to pandas.read_csv. This allows both “classic” CSV files with , and . and files in a format more common in Italy, such as ; as the column separator and , as the decimal separator, to be read without extra logic.

The function does not perform manual number conversions: it relies on standard pandas behavior.

Explicit comments

The comment parameter is optional and has opt-in behavior:

  • if comment=None, characters like # remain part of the text content

  • if comment="#", lines starting with # are treated as comments by read_csv

This detail is important to prevent symbols in the data from being interpreted as comments without an explicit request.

Spaces after the delimiter

skip_initial_space=True is translated into skipinitialspace=True in the pandas call. In practice, the spaces immediately following the separator are ignored already in the reading phase, without additional cleaning steps in the DataFrame.

Commented example

from mespy import load_csv

df = load_csv(
    "docs/source/examples/data/test_misure.csv",
    rename_columns={"misura_n": "n", "lunghezza_mm": "lunghezza", "sigma_mm": "sigma"},
    required_columns=["n", "lunghezza", "sigma"],
    missing="drop",
)

In this example:

  • the sample file is read from the folder docs/source/examples/data

  • columns are renamed immediately after loading

  • the required_columns check verifies the final names n, lunghezza, and sigma

  • any rows with missing values are dropped before returning

The final result is a DataFrame ready to be passed to the package’s analysis and plotting functions.