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
{{ message }}
Repository navigation
The fig.text function in PyGMT does not plot text if the text is purely numeric in pygmt 0.14.1 #3803
Problem: The fig.text function in PyGMT does not plot text if the text is purely numeric.
Steps to Reproduce:
Create a PyGMT figure.
Use the fig.text function to plot a numeric string (e.g., "12345").
Observe that the numeric text does not appear on the plot.
Expected Behavior: The numeric text should be plotted on the figure.
Actual Behavior: The numeric text does not appear on the figure.
Minimal Complete Verifiable Example
importpygmtfig=pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True)
fig.text(x=5, y=5, text="12345") # Numeric textfig.show()
#doesnt plot text and raises error messageimportpygmtfig=pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True)
fig.text(x=5, y=5, text="12345a") # Numeric textfig.show()
#plots text as expected
Full error message
text [ERROR]: Text record line 0 is NULL! Skipped but this is trouble)
👋 Thanks for opening your first issue here! Please make sure you filled out the template with as much detail as possible. You might also want to take a look at our contributing guidelines and code of conduct.
For now, I just tried a few examples (Windows, PyGMT dev, GMT 6.5.0) and I can confirm this issue. I found that adding a white space to the pure "numeric string" may work as an workaround for now. This problem is probably related to changes in PR #3760 for the datetime bug-fix.
I can confirm that the bug was introduced by #3760. In that PR, we tried to convert any np.object/np.str_ arrays into a np.datetime64 array. However, prior to v0.14.0, we only tried to convert np.object arrays.
# Try to convert unknown object type to np.datetime64
array=array_to_datetime(array)
else:
raiseValueError
With codes in #3760, datetimes given as strings in ISO datetime format can be converted to a np.datetime64 array before passing to GMT. It works well for strings like ["2012-01", "2012-01-02", "2012-02-03T01:00:02"], but it causes the issue reported here for strings that contain numeric values only, e.g., ["12345", "67890"]:
Actually, it's impossible to know if a string array like ["12345", "67890"] contains numeric values or date years.
There are two possible solutions:
Before converting np.object/np.str_ array to np.datetime64, check if a np.str_ array contains numeric values only. If yes, then we can't know if they're numeric values or date years. We should return the original string array directly. The following codes implement this idea:
# Check if a string array contains only numeric values. If so, return the original
# string array. Addressing https://github.com/GenericMappingTools/pygmt/issues/3803.
if array.dtype.type == np.str_:
try:
_ = array.astype(float)
except (TypeError, ValueError):
pass
else:
return array
Revert back to the behavior prior to v0.14.0, i.e., do NOT try to convert np.str_ arrays into np.datetime64. It works well because GMT API function GMT_Put_Vector is clever enough to parse datetime-like strings.
I prefer to solution 2 and it's implemented in #3804.
Activity
welcome commentedon Feb 12, 2025
👋 Thanks for opening your first issue here! Please make sure you filled out the template with as much detail as possible. You might also want to take a look at our contributing guidelines and code of conduct.
yvonnefroehlich commentedon Feb 12, 2025
Hi @naveenragur. Thanks for your clear report!
For now, I just tried a few examples (Windows, PyGMT dev, GMT 6.5.0) and I can confirm this issue. I found that adding a white space to the pure "numeric string" may work as an workaround for now. This problem is probably related to changes in PR #3760 for the datetime bug-fix.
text=42text="42"text="42 "text="42a"seisman commentedon Feb 12, 2025
I can reproduce it. It's caused by changes in #3760. Need some time to find a fix.
seisman commentedon Feb 12, 2025
I can confirm that the bug was introduced by #3760. In that PR, we tried to convert any np.object/np.str_ arrays into a np.datetime64 array. However, prior to v0.14.0, we only tried to convert np.object arrays.
In v0.14.0:
pygmt/pygmt/clib/session.py
Lines 937 to 940 in 2a84b5e
In v0.13.0 and before:
pygmt/pygmt/clib/session.py
Lines 874 to 878 in 1ff03a3
With codes in #3760, datetimes given as strings in ISO datetime format can be converted to a np.datetime64 array before passing to GMT. It works well for strings like
["2012-01", "2012-01-02", "2012-02-03T01:00:02"], but it causes the issue reported here for strings that contain numeric values only, e.g.,["12345", "67890"]:Actually, it's impossible to know if a string array like
["12345", "67890"]contains numeric values or date years.There are two possible solutions:
GMT_Put_Vectoris clever enough to parse datetime-like strings.I prefer to solution 2 and it's implemented in #3804.