@@ -1787,10 +1787,7 @@ def virtualfile_in( # noqa: PLR0912
1787
1787
"grid" : self .virtualfile_from_grid ,
1788
1788
"image" : tempfile_from_image ,
1789
1789
"stringio" : self .virtualfile_from_stringio ,
1790
- # Note: virtualfile_from_matrix is not used because a matrix can be
1791
- # converted to vectors instead, and using vectors allows for better
1792
- # handling of string type inputs (e.g. for datetime data types)
1793
- "matrix" : self .virtualfile_from_vectors ,
1790
+ "matrix" : self .virtualfile_from_matrix ,
1794
1791
"vectors" : self .virtualfile_from_vectors ,
1795
1792
}[kind ]
1796
1793
@@ -1807,29 +1804,33 @@ def virtualfile_in( # noqa: PLR0912
1807
1804
warnings .warn (message = msg , category = RuntimeWarning , stacklevel = 2 )
1808
1805
_data = (data ,) if not isinstance (data , pathlib .PurePath ) else (str (data ),)
1809
1806
elif kind == "vectors" :
1810
- _data = [x , y ]
1811
- if z is not None :
1812
- _data .append (z )
1813
- if extra_arrays :
1814
- _data .extend (extra_arrays )
1815
- elif kind == "matrix" : # turn 2-D arrays into list of vectors
1816
- if hasattr (data , "items" ) and not hasattr (data , "to_frame" ):
1807
+ if data is None :
1808
+ # data is None, so data must be given via x/y/z.
1809
+ _data = [x , y ]
1810
+ if z is not None :
1811
+ _data .append (z )
1812
+ if extra_arrays :
1813
+ _data .extend (extra_arrays )
1814
+ elif hasattr (data , "items" ) and not hasattr (data , "to_frame" ):
1817
1815
# pandas.DataFrame or xarray.Dataset types.
1818
1816
# pandas.Series will be handled below like a 1-D numpy.ndarray.
1819
1817
_data = [array for _ , array in data .items ()]
1820
- elif hasattr (data , "ndim" ) and data .ndim == 2 and data .dtype .kind in "iuf" :
1821
- # Just use virtualfile_from_matrix for 2-D numpy.ndarray
1822
- # which are signed integer (i), unsigned integer (u) or
1823
- # floating point (f) types
1824
- _virtualfile_from = self .virtualfile_from_matrix
1825
- _data = (data ,)
1826
1818
else :
1827
1819
# Python list, tuple, numpy.ndarray, and pandas.Series types
1828
1820
_data = np .atleast_2d (np .asanyarray (data ).T )
1821
+ elif kind == "matrix" :
1822
+ # GMT can only accept a 2-D matrix which are signed integer (i), unsigned
1823
+ # integer (u) or floating point (f) types. For other data types, we need to
1824
+ # use virtualfile_from_vectors instead, which turns the matrix into a list
1825
+ # of vectors and allows for better handling of non-integer/float type inputs
1826
+ # (e.g. for string or datetime data types).
1827
+ _data = (data ,)
1828
+ if data .dtype .kind not in "iuf" :
1829
+ _virtualfile_from = self .virtualfile_from_vectors
1830
+ _data = data .T
1829
1831
1830
1832
# Finally create the virtualfile from the data, to be passed into GMT
1831
1833
file_context = _virtualfile_from (* _data )
1832
-
1833
1834
return file_context
1834
1835
1835
1836
def virtualfile_from_data (
0 commit comments