Skip to content

Commit

Permalink
refactor: require pandas>=2.0.0 as core dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Aug 1, 2023
1 parent 7931112 commit ac77a9b
Show file tree
Hide file tree
Showing 30 changed files with 71 additions and 179 deletions.
1 change: 0 additions & 1 deletion .docs/Notebooks/sfrpackage_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@
raise ValueError("Failed to run.")

# ### Load SFR formated water balance output into pandas dataframe using the `SfrFile` class
# * requires the **pandas** library

sfr_outfile = os.path.join(
"..", "..", "examples", "data", "sfr_examples", "test1ss.flw"
Expand Down
10 changes: 5 additions & 5 deletions autotest/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def test_output_helper_shapefile_export(
)


@requires_pkg("pandas", "shapefile")
@requires_pkg("shapefile")
@pytest.mark.slow
def test_freyberg_export(function_tmpdir, example_data_path):
# steady state
Expand Down Expand Up @@ -254,7 +254,7 @@ def test_freyberg_export(function_tmpdir, example_data_path):
assert part.read_text() == wkt


@requires_pkg("pandas", "shapefile")
@requires_pkg("shapefile")
@pytest.mark.parametrize("missing_arrays", [True, False])
@pytest.mark.slow
def test_disu_export(function_tmpdir, missing_arrays):
Expand Down Expand Up @@ -485,7 +485,7 @@ def test_shapefile_ibound(function_tmpdir, example_data_path):
shape.close()


@requires_pkg("pandas", "shapefile")
@requires_pkg("shapefile")
@pytest.mark.slow
@pytest.mark.parametrize("namfile", namfiles())
def test_shapefile(function_tmpdir, namfile):
Expand All @@ -510,7 +510,7 @@ def test_shapefile(function_tmpdir, namfile):
), f"wrong number of records in shapefile {fnc_name}"


@requires_pkg("pandas", "shapefile")
@requires_pkg("shapefile")
@pytest.mark.slow
@pytest.mark.parametrize("namfile", namfiles())
def test_shapefile_export_modelgrid_override(function_tmpdir, namfile):
Expand Down Expand Up @@ -1446,7 +1446,7 @@ def test_vtk_vertex(function_tmpdir, example_data_path):


@requires_exe("mf2005")
@requires_pkg("pandas", "vtk")
@requires_pkg("vtk")
def test_vtk_pathline(function_tmpdir, example_data_path):
from vtkmodules.vtkIOLegacy import vtkUnstructuredGridReader

Expand Down
3 changes: 1 addition & 2 deletions autotest/test_grid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
import os
import re
import warnings
from warnings import warn

Expand All @@ -22,7 +22,6 @@
from flopy.utils.triangle import Triangle
from flopy.utils.voronoi import VoronoiGrid


HAS_PYPROJ = has_pkg("pyproj")
if HAS_PYPROJ:
import pyproj
Expand Down
32 changes: 12 additions & 20 deletions autotest/test_hydmodfile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import numpy as np
import pandas as pd
import pytest
from modflow_devtools.markers import requires_pkg
from modflow_devtools.misc import has_pkg
Expand Down Expand Up @@ -118,31 +119,22 @@ def test_hydmodfile_read(hydmod_model_path):
len(data.dtype.names) == nitems + 1
), f"data column length is not {len(nitems + 1)}"

if has_pkg("pandas"):
import pandas as pd

for idx in range(ntimes):
df = h.get_dataframe(idx=idx, timeunit="S")
assert isinstance(df, pd.DataFrame), "A DataFrame was not returned"
assert df.shape == (1, 9), "data shape is not (1, 9)"

for time in times:
df = h.get_dataframe(totim=time, timeunit="S")
assert isinstance(df, pd.DataFrame), "A DataFrame was not returned"
assert df.shape == (1, 9), "data shape is not (1, 9)"
for idx in range(ntimes):
df = h.get_dataframe(idx=idx, timeunit="S")
assert isinstance(df, pd.DataFrame), "A DataFrame was not returned"
assert df.shape == (1, 9), "data shape is not (1, 9)"

df = h.get_dataframe(timeunit="S")
for time in times:
df = h.get_dataframe(totim=time, timeunit="S")
assert isinstance(df, pd.DataFrame), "A DataFrame was not returned"
assert df.shape == (101, 9), "data shape is not (101, 9)"
else:
print("pandas not available...")
pass
assert df.shape == (1, 9), "data shape is not (1, 9)"

df = h.get_dataframe(timeunit="S")
assert isinstance(df, pd.DataFrame), "A DataFrame was not returned"
assert df.shape == (101, 9), "data shape is not (101, 9)"

@requires_pkg("pandas")
def test_mf6obsfile_read(mf6_obs_model_path):
import pandas as pd

def test_mf6obsfile_read(mf6_obs_model_path):
txt = "binary mf6 obs"
files = ["maw_obs.gitbin", "maw_obs.gitcsv"]
binfile = [True, False]
Expand Down
13 changes: 3 additions & 10 deletions autotest/test_listbudget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import warnings

import numpy as np
import pandas as pd
import pytest
from modflow_devtools.markers import requires_pkg
from modflow_devtools.misc import has_pkg
Expand Down Expand Up @@ -54,14 +55,9 @@ def test_mflistfile(example_data_path):
cum = mflist.get_cumulative(names="PERCENT_DISCREPANCY")
assert isinstance(cum, np.ndarray)

if not has_pkg("pandas"):
return

import pandas

df_flx, df_vol = mflist.get_dataframes(start_datetime=None)
assert isinstance(df_flx, pandas.DataFrame)
assert isinstance(df_vol, pandas.DataFrame)
assert isinstance(df_flx, pd.DataFrame)
assert isinstance(df_vol, pd.DataFrame)

# test get runtime
runtime = mflist.get_model_runtime(units="hours")
Expand Down Expand Up @@ -115,10 +111,7 @@ def test_mflist_reducedpumping_fail(example_data_path):
mflist.get_reduced_pumping()


@requires_pkg("pandas")
def test_mtlist(example_data_path):
import pandas as pd

mt_dir = example_data_path / "mt3d_test"
mt = MtListBudget(mt_dir / "mcomp.list")
df_gw, df_sw = mt.parse(forgive=False, diff=False, start_datetime=None)
Expand Down
3 changes: 1 addition & 2 deletions autotest/test_mnw.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import shutil

import numpy as np
import pandas as pd
import pytest
from modflow_devtools.markers import requires_pkg

Expand Down Expand Up @@ -293,13 +294,11 @@ def test_make_package(function_tmpdir):
)


@requires_pkg("pandas")
def test_mnw2_create_file(function_tmpdir):
"""
Test for issue #556, Mnw2 crashed if wells have
multiple node lengths
"""
import pandas as pd

mf = Modflow("test_mfmnw2", exe_name="mf2005")
ws = function_tmpdir
Expand Down
3 changes: 1 addition & 2 deletions autotest/test_mp5.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import numpy as np
import pandas as pd
from autotest.test_mp6 import eval_timeseries
from matplotlib import pyplot as plt
from modflow_devtools.markers import requires_pkg
Expand All @@ -10,7 +11,6 @@
from flopy.utils import EndpointFile, PathlineFile


@requires_pkg("pandas")
def test_mp5_load(function_tmpdir, example_data_path):
# load the base freyberg model
freyberg_ws = example_data_path / "freyberg"
Expand Down Expand Up @@ -68,7 +68,6 @@ def test_mp5_load(function_tmpdir, example_data_path):
plt.close()


@requires_pkg("pandas")
def test_mp5_timeseries_load(example_data_path):
pth = str(example_data_path / "mp5")
files = [
Expand Down
14 changes: 4 additions & 10 deletions autotest/test_mp6.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pytest
from autotest.conftest import get_example_data_path
from autotest.test_mp6_cases import Mp6Cases1, Mp6Cases2
Expand Down Expand Up @@ -110,12 +111,8 @@ def test_mpsim(function_tmpdir, mp6_test_path):
)
mp.write_input()

use_pandas_combs = [False] # test StartingLocationsFile._write_wo_pandas
if has_pkg("pandas"):
# test StartingLocationsFile._write_particle_data_with_pandas
use_pandas_combs.append(True)

for use_pandas in use_pandas_combs:
# test StartingLocationsFile._write_wo_pandas
for use_pandas in [True, False]:
sim = Modpath6Sim(model=mp)
# starting locations file
stl = StartingLocationsFile(model=mp, use_pandas=use_pandas)
Expand All @@ -135,7 +132,7 @@ def test_mpsim(function_tmpdir, mp6_test_path):
assert stllines[6].strip().split()[-1] == "p2"


@requires_pkg("pandas", "shapefile", "shapely")
@requires_pkg("shapefile", "shapely")
def test_get_destination_data(function_tmpdir, mp6_test_path):
copy_modpath_files(mp6_test_path, function_tmpdir, "EXAMPLE.")
copy_modpath_files(mp6_test_path, function_tmpdir, "EXAMPLE-3.")
Expand Down Expand Up @@ -307,7 +304,6 @@ def test_get_destination_data(function_tmpdir, mp6_test_path):
pthobj.write_shapefile(shpname=fpth, direction="ending", mg=mg4)


@requires_pkg("pandas")
def test_loadtxt(function_tmpdir, mp6_test_path):
copy_modpath_files(mp6_test_path, function_tmpdir, "EXAMPLE-3.")

Expand All @@ -324,7 +320,6 @@ def test_loadtxt(function_tmpdir, mp6_test_path):


@requires_exe("mf2005")
@requires_pkg("pandas")
def test_modpath(function_tmpdir, example_data_path):
pth = example_data_path / "freyberg"
mfnam = "freyberg.nam"
Expand Down Expand Up @@ -480,7 +475,6 @@ def test_modpath(function_tmpdir, example_data_path):
plt.close()


@requires_pkg("pandas")
def test_mp6_timeseries_load(example_data_path):
pth = example_data_path / "mp5"
files = [
Expand Down
3 changes: 0 additions & 3 deletions autotest/test_mp7.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ def test_default_modpath(ex01b_mf6_model):


@requires_exe("mf6", "mp7")
@requires_pkg("pandas")
def test_faceparticles_is1(ex01b_mf6_model):
sim, function_tmpdir = ex01b_mf6_model

Expand Down Expand Up @@ -441,7 +440,6 @@ def test_facenode_is2a(ex01b_mf6_model):


@requires_exe("mf6", "mp7")
@requires_pkg("pandas")
def test_cellparticles_is1(ex01b_mf6_model):
sim, function_tmpdir = ex01b_mf6_model
grid = sim.get_model(ex01b_mf6_model_name).modelgrid
Expand Down Expand Up @@ -817,7 +815,6 @@ def test_pathline_output(function_tmpdir):
assert maxid0 == maxid1, msg


@requires_pkg("pandas")
@requires_exe("mf2005", "mf6", "mp7")
def test_endpoint_output(function_tmpdir):
case_mf2005 = Mp7Cases.mp7_mf2005(function_tmpdir)
Expand Down
2 changes: 0 additions & 2 deletions autotest/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,6 @@ def modpath_model(function_tmpdir, example_data_path):
return ml, mp, sim


@requires_pkg("pandas")
@requires_exe("mf2005", "mp6")
def test_xc_plot_particle_pathlines(modpath_model):
ml, mp, sim = modpath_model
Expand All @@ -407,7 +406,6 @@ def test_xc_plot_particle_pathlines(modpath_model):
assert len(pth._paths) == 6


@requires_pkg("pandas")
@requires_exe("mf2005", "mp6")
def test_map_plot_particle_endpoints(modpath_model):
ml, mp, sim = modpath_model
Expand Down
23 changes: 10 additions & 13 deletions autotest/test_sfr.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def test_const(sfr_data):
assert True


@requires_pkg("pandas", "shapefile", "shapely")
@requires_pkg("shapefile", "shapely")
def test_export(function_tmpdir, sfr_data):
m = Modflow()
dis = ModflowDis(m, 1, 10, 10, lenuni=2, itmuni=4)
Expand Down Expand Up @@ -666,7 +666,6 @@ def test_assign_layers(function_tmpdir):


@requires_exe("mf2005")
@requires_pkg("pandas")
def test_SfrFile(function_tmpdir, sfr_examples_path, mf2005_model_path):
common_names = [
"layer",
Expand All @@ -693,13 +692,12 @@ def test_SfrFile(function_tmpdir, sfr_examples_path, mf2005_model_path):
"gw_head",
], sfrout.names
assert sfrout.times == [(0, 0), (49, 1)], sfrout.times
# will be None if pandas is not installed
if sfrout.pd is not None:
df = sfrout.get_dataframe()
assert df.layer.values[0] == 1
assert df.column.values[0] == 169
assert df.Cond.values[0] == 74510.0
assert df.gw_head.values[3] == 1.288e03

df = sfrout.get_dataframe()
assert df.layer.values[0] == 1
assert df.column.values[0] == 169
assert df.Cond.values[0] == 74510.0
assert df.gw_head.values[3] == 1.288e03

sfrout = SfrFile(sfr_examples_path / "test1tr.flw")
assert sfrout.ncol == 16, sfrout.ncol
Expand Down Expand Up @@ -737,10 +735,9 @@ def test_SfrFile(function_tmpdir, sfr_examples_path, mf2005_model_path):
(49, 1),
]
assert sfrout.times == expected_times, sfrout.times
if sfrout.pd is not None:
df = sfrout.get_dataframe()
assert df.gradient.values[-1] == 5.502e-02
assert df.shape == (1080, 20)
df = sfrout.get_dataframe()
assert df.gradient.values[-1] == 5.502e-02
assert df.shape == (1080, 20)

ml = Modflow.load(
"test1tr.nam", model_ws=mf2005_model_path, exe_name="mf2005"
Expand Down
1 change: 0 additions & 1 deletion autotest/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@


@requires_exe("mf2005")
@requires_pkg("pandas")
def test_str_issue1164(function_tmpdir, example_data_path):
mf2005_model_path = example_data_path / "mf2005_test"
m = Modflow.load(
Expand Down
28 changes: 12 additions & 16 deletions autotest/test_swr_binaryread.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Test SWR binary read functionality
import pandas as pd
import pytest
from modflow_devtools.misc import has_pkg

Expand Down Expand Up @@ -446,21 +447,16 @@ def test_swr_binary_obs(swr_test_path, ipos):
), "SwrObs data does not have nobs + 1"

# test get_dataframes()
if has_pkg("pandas"):
import pandas as pd

for idx in range(ntimes):
df = sobj.get_dataframe(idx=idx, timeunit="S")
assert isinstance(df, pd.DataFrame), "A DataFrame was not returned"
assert df.shape == (1, nobs + 1), "data shape is not (1, 10)"

for time in times:
df = sobj.get_dataframe(totim=time, timeunit="S")
assert isinstance(df, pd.DataFrame), "A DataFrame was not returned"
assert df.shape == (1, nobs + 1), "data shape is not (1, 10)"
for idx in range(ntimes):
df = sobj.get_dataframe(idx=idx, timeunit="S")
assert isinstance(df, pd.DataFrame), "A DataFrame was not returned"
assert df.shape == (1, nobs + 1), "data shape is not (1, 10)"

df = sobj.get_dataframe(timeunit="S")
for time in times:
df = sobj.get_dataframe(totim=time, timeunit="S")
assert isinstance(df, pd.DataFrame), "A DataFrame was not returned"
assert df.shape == (336, nobs + 1), "data shape is not (336, 10)"
else:
print("pandas not available...")
assert df.shape == (1, nobs + 1), "data shape is not (1, 10)"

df = sobj.get_dataframe(timeunit="S")
assert isinstance(df, pd.DataFrame), "A DataFrame was not returned"
assert df.shape == (336, nobs + 1), "data shape is not (336, 10)"
Loading

0 comments on commit ac77a9b

Please sign in to comment.