|
4 | 4 | from shutil import copytree, which |
5 | 5 |
|
6 | 6 | import numpy as np |
| 7 | +import pandas as pd |
7 | 8 | import pytest |
8 | 9 | from modflow_devtools.markers import requires_exe, requires_pkg |
9 | 10 | from modflow_devtools.misc import set_dir |
|
58 | 59 | ) |
59 | 60 | from flopy.mf6.data.mffileaccess import MFFileAccessArray |
60 | 61 | from flopy.mf6.data.mfstructure import MFDataItemStructure, MFDataStructure |
| 62 | +from flopy.mf6.mfbase import MFDataException |
61 | 63 | from flopy.mf6.mfsimbase import MFSimulationData |
62 | 64 | from flopy.mf6.modflow import ( |
63 | 65 | mfgwf, |
@@ -1450,6 +1452,28 @@ def test_get_set_data_record(function_tmpdir): |
1450 | 1452 | sim.write_simulation() |
1451 | 1453 |
|
1452 | 1454 |
|
| 1455 | +def test_set_data_dataframe_column_mismatch_error(function_tmpdir): |
| 1456 | + """ |
| 1457 | + DataFrame with a wrong column count should raise MFDataException. |
| 1458 | + Previously the error message code referenced len(data[0]), which on a |
| 1459 | + DataFrame accesses the column named 0 and raises KeyError instead. |
| 1460 | + """ |
| 1461 | + sim = MFSimulation(sim_ws=str(function_tmpdir), exe_name="mf6") |
| 1462 | + ModflowTdis(sim, nper=1, perioddata=[(1.0, 1, 1.0)]) |
| 1463 | + ModflowIms(sim) |
| 1464 | + gwf = ModflowGwf(sim, modelname="gwf") |
| 1465 | + ModflowGwfdis(gwf, nlay=1, nrow=10, ncol=10) |
| 1466 | + ModflowGwfic(gwf, strt=0.0) |
| 1467 | + ModflowGwfnpf(gwf) |
| 1468 | + ModflowGwfwel(gwf, stress_period_data={0: [[(0, 0, 0), -1.0]]}) |
| 1469 | + spd = gwf.get_package("WEL").stress_period_data |
| 1470 | + |
| 1471 | + # 3 columns: matches neither _header_names (4) nor _data_item_names (2) |
| 1472 | + df = pd.DataFrame({"a": [0], "b": [3], "c": [-500.0]}) |
| 1473 | + with pytest.raises(MFDataException): |
| 1474 | + spd.set_data({0: df}) |
| 1475 | + |
| 1476 | + |
1453 | 1477 | @requires_exe("mf6") |
1454 | 1478 | def test_output(function_tmpdir, example_data_path): |
1455 | 1479 | ex_name = "test001e_UZF_3lay" |
|
0 commit comments