Skip to content

Commit

Permalink
Fix issue when REAL is already present in loaded CSV files (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
berland authored and perolavsvendsen committed Oct 31, 2019
1 parent 6617aa1 commit 34f4889
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/fmu/ensemble/realization.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,15 @@ def load_csv(self, localpath, convert_numeric=True, force_reread=False):
else:
dtype = str
dframe = pd.read_csv(fullpath, dtype=dtype)
if "REAL" in dframe:
dframe.rename(columns={"REAL": "REAL_ORIG"}, inplace=True)
logger.warning(
(
"Loaded file %s already had the column REAL, "
"this was renamed to REAL_ORIG"
),
fullpath,
)
except pd.errors.EmptyDataError:
dframe = None # or empty dataframe?

Expand Down
7 changes: 7 additions & 0 deletions tests/test_realization.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ def test_single_realization():
# test basename and no extension:
assert isinstance(real.get_df("simulator_volume_fipnum"), pd.DataFrame)

# Some CSV files might already contain the REAL column, this is not allowed
foo_df = pd.DataFrame(columns=["REAL", "FOOBAR"], data=[[0, 1], [2, 3]])
foo_df.to_csv(os.path.join(realdir, "foo-real.csv"), index=False)
real.load_csv("foo-real.csv") # A warning will be issued.
assert "REAL" not in real.get_df("foo-real")
assert "FOOBAR" in real.get_df("foo-real")

with pytest.raises(ValueError):
real.get_df("notexisting.csv")

Expand Down

0 comments on commit 34f4889

Please sign in to comment.