From 9e2d9d7e60267bd0e1741cea2f74150b8b0d9630 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sat, 25 Nov 2023 22:09:43 +0800 Subject: [PATCH 1/4] Enable ruff's pandas-vet (PD) rules --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 281bf6e6ec8..bf0f552cfe1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -100,6 +100,7 @@ select = [ "F", # pyflakes "I", # isort "NPY", # numpy + "PD", # pandas-vet "PL", # pylint "UP", # pyupgrade "W", # pycodestyle warnings From 01aa638fe381b0038238b0d91d55416d1485cfe4 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 26 Nov 2023 13:10:43 +0800 Subject: [PATCH 2/4] Fix PD errors --- pygmt/clib/conversion.py | 4 ++-- pygmt/tests/test_blockmedian.py | 2 +- pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py | 2 +- pygmt/tests/test_datasets_samples.py | 2 +- pygmt/tests/test_select.py | 2 +- pygmt/tests/test_surface.py | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pygmt/clib/conversion.py b/pygmt/clib/conversion.py index baf6d517df6..04264739e8f 100644 --- a/pygmt/clib/conversion.py +++ b/pygmt/clib/conversion.py @@ -92,7 +92,7 @@ def dataarray_to_matrix(grid): # grids, this would be North-South, East-West. GMT's region and inc are # East-West, North-South. for dim in grid.dims[::-1]: - coord = grid.coords[dim].values + coord = grid.coords[dim].to_numpy() coord_incs = coord[1:] - coord[0:-1] coord_inc = coord_incs[0] if not np.allclose(coord_incs, coord_inc): @@ -120,7 +120,7 @@ def dataarray_to_matrix(grid): inc = [abs(i) for i in inc] grid = grid.sortby(variables=list(grid.dims), ascending=True) - matrix = as_c_contiguous(grid[::-1].values) + matrix = as_c_contiguous(grid[::-1].to_numpy()) return matrix, region, inc diff --git a/pygmt/tests/test_blockmedian.py b/pygmt/tests/test_blockmedian.py index f8aae503bee..a431158bf87 100644 --- a/pygmt/tests/test_blockmedian.py +++ b/pygmt/tests/test_blockmedian.py @@ -36,7 +36,7 @@ def test_blockmedian_input_table_matrix(dataframe): Run blockmedian using table input that is not a pandas.DataFrame but still a matrix. """ - table = dataframe.values + table = dataframe.to_numpy() output = blockmedian(data=table, spacing="5m", region=[245, 255, 20, 30]) assert isinstance(output, pd.DataFrame) assert output.shape == (5849, 3) diff --git a/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py b/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py index 7acc3674623..840fe2b431d 100644 --- a/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py +++ b/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py @@ -21,7 +21,7 @@ def test_earth_vertical_gravity_gradient_01d(): npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -137.125, atol=1 / 32) npt.assert_allclose(data.max(), 104.59375, atol=1 / 32) - assert data[1, 1].isnull() + assert data[1, 1].isna() def test_earth_vertical_gravity_gradient_01d_with_region(): diff --git a/pygmt/tests/test_datasets_samples.py b/pygmt/tests/test_datasets_samples.py index 4c9d841de89..a00706163e4 100644 --- a/pygmt/tests/test_datasets_samples.py +++ b/pygmt/tests/test_datasets_samples.py @@ -189,7 +189,7 @@ def test_earth_relief_holes(): npt.assert_allclose(grid.max(), 1601) npt.assert_allclose(grid.min(), -4929.5) # Test for the NaN values in the remote file - assert grid[2, 21].isnull() + assert grid[2, 21].isna() def test_maunaloa_co2(): diff --git a/pygmt/tests/test_select.py b/pygmt/tests/test_select.py index 81cf5bd8c72..849cfcd743c 100644 --- a/pygmt/tests/test_select.py +++ b/pygmt/tests/test_select.py @@ -37,7 +37,7 @@ def test_select_input_table_matrix(dataframe): Also testing the reverse (I) alias. """ - data = dataframe.values + data = dataframe.to_numpy() output = select(data=data, region=[245.5, 254.5, 20.5, 29.5], reverse="r") assert isinstance(output, pd.DataFrame) assert output.shape == (9177, 3) diff --git a/pygmt/tests/test_surface.py b/pygmt/tests/test_surface.py index a4035bc2352..040da015faf 100644 --- a/pygmt/tests/test_surface.py +++ b/pygmt/tests/test_surface.py @@ -93,7 +93,7 @@ def test_surface_input_data_array(data, region, spacing, expected_grid): """ Run surface by passing in a numpy array into data. """ - data = data.values # convert pandas.DataFrame to numpy.ndarray + data = data.to_numpy() # convert pandas.DataFrame to numpy.ndarray output = surface( data=data, spacing=spacing, @@ -132,7 +132,7 @@ def test_surface_with_outgrid_param(data, region, spacing, expected_grid): """ Run surface with the -Goutputfile.nc parameter. """ - data = data.values # convert pandas.DataFrame to numpy.ndarray + data = data.to_numpy() # convert pandas.DataFrame to numpy.ndarray with GMTTempFile(suffix=".nc") as tmpfile: output = surface( data=data, From b79c056b42688601d35ab2ae85ea5db5d2850086 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 27 Nov 2023 09:12:45 +0800 Subject: [PATCH 3/4] Ignore PD901 --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index bf0f552cfe1..8b904b225f7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -107,6 +107,7 @@ select = [ ] ignore = [ "E501", # Avoid enforcing line-length violations + "PD901", # Allow using the generic variable name `df` for DataFrames "PLR2004", # Allow any magic values ] From 0665491a019e1984c51c648f53da7351c7177790 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 27 Nov 2023 09:58:18 +0800 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py | 2 +- pygmt/tests/test_datasets_samples.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py b/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py index 840fe2b431d..dbc85e28b46 100644 --- a/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py +++ b/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py @@ -21,7 +21,7 @@ def test_earth_vertical_gravity_gradient_01d(): npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -137.125, atol=1 / 32) npt.assert_allclose(data.max(), 104.59375, atol=1 / 32) - assert data[1, 1].isna() + assert data[1, 1].isnull() # noqa: PD003 # ruff's bug def test_earth_vertical_gravity_gradient_01d_with_region(): diff --git a/pygmt/tests/test_datasets_samples.py b/pygmt/tests/test_datasets_samples.py index a00706163e4..28a86711f74 100644 --- a/pygmt/tests/test_datasets_samples.py +++ b/pygmt/tests/test_datasets_samples.py @@ -189,7 +189,7 @@ def test_earth_relief_holes(): npt.assert_allclose(grid.max(), 1601) npt.assert_allclose(grid.min(), -4929.5) # Test for the NaN values in the remote file - assert grid[2, 21].isna() + assert grid[2, 21].isnull() # noqa: PD003 # ruff's bug def test_maunaloa_co2():