Hello there! I was trying to use pygmt.grdtrack for a project, but kept getting an error when trying to run it. I was trying to use a dataframe that has a lot of extra columns apart from the first two (being ‘lon’, ‘lat’) as my points
argument in the function
df=pygmt.grdtrack(grid=grid, points=df, newcolname="newcol")
and got the error
[TypeError: sequence item 1: expected string, int found]
I found it came from a ' '.join()
line being run in one of the functions called by grdtrack. I called the function back as
df[['lon','lat','newcol']]=pygmt.grdtrack(grid=grid, points=df[['lon','lat']], newcolname="newcol")
and this time it worked without issue. The columns in my dataframe had different types of data (float, str and datetime objects). I don’t know if grdtrack has an issue with any of this data types, or just with the fact that the dataframe was too big (about 100k rows by 10 cols) but I think it would be good to clarify it in the documentation if it does.
I didn’t look too hard into trying to replicate the issue in a sample that can be shared, but I can try if someone from the development team is interested.
Edit: Adding a simple example, here it has problems with the timedelta object. Not the same error but you can see that, if you turn it into a datetime object, it also has some weird behaviour (it turns it into float after grdtrack is called)
import pandas as pd
import numpy as np
import datetime
df = pd.DataFrame(columns=['lon','lat','trash','trashdate'])
df['lon']=np.random.default_rng().uniform(low=-74,high=-71,size=[500])
df['lat']=np.random.default_rng().uniform(low=-51,high=-46,size=[500])
df['trash']=np.random.default_rng().uniform(low=-51,high=-46,size=[500])
df['trashdate']=(np.random.default_rng().uniform(low=0,high=5,size=[500]))*datetime.timedelta(seconds=3e7)#+datetime.date(2018,1,1)
spacing=0.1 #grados de arco
region=[-74.0, -71.0, -51.0, -46.0]
n_lon=round((region[1]-region[0])/spacing)+1
n_lat=round((region[3]-region[2])/spacing)+1
lat,lon=np.mgrid[region[2]:region[3]:n_lat*1j,region[0]:region[1]:n_lon*1j]
grd = pygmt.xyz2grd(x=lon.flatten(),y=lat.flatten(),
z=np.random.default_rng().uniform(low=-500,high=500,size=len(lon.flatten())),
region=region, spacing=spacing, projection='m6c',
outgrid='/home/ejemplo.grd')
df=pygmt.grdtrack(grid='/home/ejemplo.grd', points=df, newcolname="hsup3d")
Once again, I don’t know if it’s intended or if it’s a side effect of decisions made for efficiency sake, but I think it would be useful to document.