GenericMappingTools / GenericMappingTools/pygmt

pygmt.grdtrack: Bad behaviour when dataframe index isn't default

Open
#2,763 5 comments 0 reactions 0 assignees View on GitHub
feature request
Dominant language
Python
Stars
874
Forks
255
Avg merge
1d 21h
Merged PRs (30d)
40

Description

### Description of the problem

When I try to use pygmt.grdtrack on a dataframe that doesn't have default indexing (0 through len(df)), like a slice from an original dataframe, it doesn't work properly. For what I can gather, it works up until the index that coincides with the length of the dataframe, so if the index goes higher than that it starts giving out NaN values.

### Minimal Complete Verifiable Example

```python
import pandas as pd
import numpy as np
import pygmt

df = pd.DataFrame(columns=['lon','lat','height'])
df['lon']=np.random.default_rng().uniform(low=-74,high=-71,size=[5000])
df['lat']=np.random.default_rng().uniform(low=-51,high=-46,size=[5000])
df['height']=np.random.default_rng().uniform(low=0,high=3000,size=[5000])

spacing=0.1 #degrees
region=[-74.0, -71.0, -51.0, -46.0]

grd=pygmt.blockmean(data=df, region=region, spacing=spacing)
grd=pygmt.xyz2grd(data=grd, region=region, spacing=spacing)

over_1500=df[df['height']>1500].copy()

over_1500[['lon','lat','meanheight']]=pygmt.grdtrack(
grid=grd,points=over_1500[['lon','lat']],
newcolname="meanheight")

print(over_1500)

over_1500=df[df['height']>1500].copy()
over_1500.reset_index(drop=True,inplace=True)
over_1500[['lon','lat','meanheight']]=pygmt.grdtrack(
grid=grd,points=over_1500[['lon','lat']],
newcolname="meanheight")
print(over_1500)
```

### Full error message

```python-traceback
No error message but a DataFrame with NaN values

lon lat height meanheight
0 -71.931949 -47.668331 2589.084799 1298.987358
3 -73.828530 -49.563440 1939.247423 1407.897766
4 -72.569494 -49.096500 1676.786269 1716.785175
5 -72.487901 -50.849346 2638.614151 1758.230110
6 -72.927679 -48.920109 1920.867327 1772.126105
... ... ... ...
4988 NaN NaN 2096.621535 NaN
4991 NaN NaN 1779.764729 NaN
4992 NaN NaN 2378.422848 NaN
4994 NaN NaN 1908.190985 NaN
4995 NaN NaN 2744.655240 NaN
```

### System information

```bash
PyGMT information:
version: v0.10.0
System information:
python: 3.9.16 | packaged by conda-forge | (main, Feb 1 2023, 21:39:03) [GCC 11.3.0]
executable: /home/fedesuad/mambaforge/envs/cin2020/bin/python
machine: Linux-5.4.0-73-generic-x86_64-with-glibc2.31
Dependency information:
numpy: 1.23.5
pandas: 1.3.4
xarray: 0.19.0
netCDF4: 1.6.4
packaging: 23.2
contextily: None
geopandas: None
IPython: 8.14.0
rioxarray: None
ghostscript: 9.54.0
GMT library information:
binary version: 6.4.0
cores: 8
grid layout: rows
image layout:
library path: /home/fedesuad/mambaforge/envs/cin2020/lib/libgmt.so
padding: 2
plugin dir: /home/fedesuad/mambaforge/envs/cin2020/lib/gmt/plugins
share dir: /home/fedesuad/mambaforge/envs/cin2020/share/gmt
version: 6.4.0
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.