You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the input pandas DataFrame contains an additional column with datetime objects, pygmt.grdtrack mixes up the column names of the output pandas Dataframe.
importpygmtimportpandasaspdimportnumpyasnpimportdatetimenp.random.seed(100)
# Make random test datadf=pd.DataFrame(columns=["lon", "lat", "trash", "trashdate"])
df["lon"] =50+2*np.random.randn(100)
df["lat"] =55+4*np.random.randn(100)
df["trash"] =53+5*np.random.randn(100)
# Correct column names after applying grdtrack# df["trashdate"] = 51 + 7 * np.random.randn(100)# Mixed up column names after applying grdtrackdf["trashdate"] = (51+7*np.random.randn(100)) \
*datetime.timedelta(seconds=3e7) +datetime.date(1950, 1, 1)
# Make meshspacing=0.5region= [min(df.lon), max(df.lon), min(df.lat), max(df.lat)]
n_lon=round((region[1] -region[0]) /spacing) +1n_lat=round((region[3] -region[2]) /spacing) +1lat, lon=np.mgrid[region[2]:region[3]:n_lat*1j, region[0]:region[1]:n_lon*1j]
# Make GMT ready grid with random z valuespygmt.xyz2grd(
x=lon.flatten(),
y=lat.flatten(),
z=50+1*np.random.randn(len(lon.flatten())),
region=region,
spacing=spacing,
outgrid="test.grd",
)
# Extrakt the z values of the grid at the points stored in the pandas.DataFrame# and write them to the new column "z_grid"df_new=pygmt.grdtrack(
grid="test.grd",
points=df,
newcolname="z_grid",
)
No error message occurs, but the column names after applying grdtrack are mixed up in case the pandas DataFrame contains a column with datetime objects.
In the first case, the z value (-2) is appended as the last column, while in the 2nd case, the z value is inserted at the 3rd case (column number starting from 0) and the text column is appended as the last column. The philosophy of the GMT I/O mechanism is always putting text at the end of a row.
In your example, the df["trashdate"] has an object dtype, which is recognized as a string column, rather then a datetime column.
That's because the trashdate column with the object dtype can be correctly recognized as np.datetime64 dtype and passed to GMT C API as "GMT_DATETIME" type. So, the original issue has been fixed.
However, we still have an issue: the output trashdate is in float64 dtype, but we expect it to be in datetime dtype.
Description of the problem
This is related to the GMT forum post at https://forum.generic-mapping-tools.org/t/type-error-with-pygmt-grdtrack-possible-bug-or-intended/4286.
If the input pandas DataFrame contains an additional column with datetime objects,
pygmt.grdtrack
mixes up the column names of the output pandas Dataframe.Maybe this is partly related to the issue #2463?
Minimal Complete Verifiable Example
Compare the pandas DataFrames:
and
Full error message
No error message occurs, but the column names after applying
grdtrack
are mixed up in case the pandas DataFrame contains a column with datetime objects.System information
The text was updated successfully, but these errors were encountered: