Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stricten parsing of malformed parameter csv's with pandas>=1.4 #414

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions ecl2df/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import json
import logging
import warnings
from pathlib import Path
from typing import Any, Dict, List, Union

Expand Down Expand Up @@ -65,9 +66,23 @@ def load_parameterstxt(filename: Union[str, Path]) -> Dict[str, Any]:
filename: file containing one key-value pair pr. line,
separated by whitespace
"""
dframe = pd.read_csv(
filename, comment="#", sep=r"\s", engine="python", names=["KEY", "VALUE"]
)
with warnings.catch_warnings(record=True):
# From pandas 1.4, too many columns result in a ParserWarning for dropped
# data. This is risky, and therefore catching the warning and raising a
# ParserError instead.
warnings.filterwarnings("error")
asnyv marked this conversation as resolved.
Show resolved Hide resolved
try:
dframe = pd.read_csv(
filename,
comment="#",
sep=r"\s",
engine="python",
names=["KEY", "VALUE"],
index_col=False,
)
except pd.errors.ParserWarning as txt_exc:
raise pd.errors.ParserError(txt_exc)

return dframe.set_index("KEY")["VALUE"].to_dict()


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"ecl",
"numpy",
"opm>=2020.10.2", # NB: Pypi versions.
"pandas<1.4.0",
"pandas",
"pyarrow",
"pyyaml>=5.1",
"treelib",
Expand Down