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:
It immediately checks that
missingis one of the supported policies:"error","drop", or"allow".Calls
pd.read_csv(...)with:Path(path)as the file pathsepdecimalcommentskipinitialspace=skip_initial_space
If
rename_columnsis provided, it renames theDataFramecolumns.If
required_columnsis provided, it checks that all required columns are present after any renaming.Applies the missing-value policy:
with
missing="drop"it returnsdf.dropna()with
missing="error"it checks whether at least oneNaNexists and, if so, raisesValueErrorwith
missing="allow"it leaves theDataFrameunchanged
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.
pathis the only positional parameter: all the others are keyword-only.Validation of
missinghappens before reading the file. If the policy is not supported, the function fails immediately.required_columnsis checked afterrename_columns. This allows you to request columns by their final name, not their original file name.commentandskip_initial_spacedo not introduce additional logic: they are controlled passthroughs topandas.read_csv.With
missing="error", the check forNaNvalues takes place on the loadedDataFrame, 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 entireDataFramewithdf.isna().any().any(). If it finds at least one missing value, it raisesValueError.missing="drop": loading does not fail due to the presence ofNaN. Incomplete lines are deleted withDataFrame.dropna(), and the returnedDataFramemay therefore have fewer lines than the original file.missing="allow": the function does not remove anything and does not raise errors for theNaN. The missing values remain in theDataFrameand are managed downstream if necessary.
From an operational point of view:
erroris useful when the file needs to be complete before moving to analysis.dropis useful when you want a conservative but not blocking behavior.allowis 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 contentif
comment="#", lines starting with#are treated as comments byread_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/datacolumns are renamed immediately after loading
the
required_columnscheck verifies the final namesn,lunghezza, andsigmaany 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.