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
Running pygmt.info on a pandas.Series object with a pd.NA value (that uses pandas' nullable integer datatype) raises an error like ValueError: Converting an integer to a NumPy datetime requires a specified unit. This is caused by the _check_dtype_and_dim function here
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
File ~/Documents/pygmt/pygmt/clib/session.py:839, in Session._check_dtype_and_dim(self, array, ndim)
837try:
838# Try to convert any unknown numpy data types to np.datetime64
--> 839 array = array_to_datetime(array)
840exceptValueErroras e:
File ~/Documents/pygmt/pygmt/clib/conversion.py:327, in array_to_datetime(array)
253"""254 Convert a 1-D datetime array from various types into numpy.datetime64.
255
(...)
325'2018-01-01T00:00:00.000000'], dtype='datetime64[us]')
326"""
--> 327 return np.asarray(array, dtype=np.datetime64)
ValueError: Converting an integer to a NumPy datetime requires a specified unit
The above exception was the direct cause of the following exception:
GMTInvalidInput Traceback (most recent call last)
Cell In[4], line 1
----> 1 output = pygmt.info(data=series)
File ~/Documents/pygmt/pygmt/helpers/decorators.py:600, in use_alias.<locals>.alias_decorator.<locals>.new_module(*args, **kwargs)
593 msg = (
594"Parameters 'Y' and 'yshift' are deprecated since v0.8.0. "595"and will be removed in v0.12.0. "596"Use Figure.shift_origin(yshift=...) instead."597 )
598 warnings.warn(msg, category=SyntaxWarning, stacklevel=2)
--> 600 return module_func(*args, **kwargs)
File ~/Documents/pygmt/pygmt/helpers/decorators.py:740, in kwargs_to_strings.<locals>.converter.<locals>.new_module(*args, **kwargs)
738 kwargs[arg] = separators[fmt].join(f"{item}"for item in value)
739# Execute the original function and return its output
--> 740 return module_func(*args, **kwargs)
File ~/Documents/pygmt/pygmt/src/info.py:85, in info(data, **kwargs)
83 file_context = lib.virtualfile_from_data(check_kind="vector", data=data)
84with GMTTempFile() as tmpfile:
---> 85 with file_context as fname:
86 lib.call_module(
87 module="info",
88 args=build_arg_string(kwargs, infile=fname, outfile=tmpfile.name),
89 )
90 result = tmpfile.read()
File ~/mambaforge/envs/pygmt/lib/python3.12/contextlib.py:137, in _GeneratorContextManager.__enter__(self)
135delself.args, self.kwds, self.func
136try:
--> 137 return next(self.gen)
138exceptStopIteration:
139raiseRuntimeError("generator didn't yield") fromNone
File ~/Documents/pygmt/pygmt/clib/session.py:1276, in Session.virtualfile_from_vectors(self, *vectors)
1274 # Use put_vector for columns with numerical type data
1275 for col, array in enumerate(arrays[:columns]):
-> 1276 self.put_vector(dataset, column=col, vector=array)
1278 # Use put_strings for last column(s) with string type data
1279 # Have to use modifier "GMT_IS_DUPLICATE" to duplicate the strings
1280 string_arrays = arrays[columns:]
File ~/Documents/pygmt/pygmt/clib/session.py:888, in Session.put_vector(self, dataset, column, vector)
847r"""848 Attach a numpy 1-D array as a column on a GMT dataset.
849
(...)
880 status !=0.
881"""882 c_put_vector =self.get_libgmt_func(
883"GMT_Put_Vector",
884 argtypes=[ctp.c_void_p, ctp.c_void_p, ctp.c_uint, ctp.c_uint, ctp.c_void_p],
885 restype=ctp.c_int,
886 )
--> 888 gmt_type = self._check_dtype_and_dim(vector, ndim=1)
889if gmt_type in (self["GMT_TEXT"], self["GMT_DATETIME"]):
890 vector_pointer = (ctp.c_char_p *len(vector))()
File ~/Documents/pygmt/pygmt/clib/session.py:841, in Session._check_dtype_and_dim(self, array, ndim)
839 array = array_to_datetime(array)
840exceptValueErroras e:
--> 841 raise GMTInvalidInput(
842f"Unsupported numpy data type '{array.dtype.type}'."843 ) from e
844returnself[DTYPES[array.dtype.type]]
GMTInvalidInput: Unsupported numpy data type '<class 'numpy.object_'>'.
weiji14
changed the title
Error when passing data with pandas nullable int dtype
Error when passing pd.NA value in a data column with pandas nullable int dtype
Dec 3, 2023
Description of the problem
Running
pygmt.info
on apandas.Series
object with apd.NA
value (that uses pandas' nullable integer datatype) raises an error likeValueError: Converting an integer to a NumPy datetime requires a specified unit
. This is caused by the_check_dtype_and_dim
function herepygmt/pygmt/clib/session.py
Lines 789 to 844 in 7aac7fd
Maybe we need to update the dictionary here
pygmt/pygmt/clib/session.py
Lines 75 to 88 in 7aac7fd
to include
pd.Int32Dtype()
andpd.Int64Dtype()
? But what would we map these nullable dtypes to in GMT?Minimal Complete Verifiable Example
Full error message
System information
The text was updated successfully, but these errors were encountered: