@@ -187,31 +187,30 @@ def _check_encoding(
187
187
return "ISOLatin1+"
188
188
189
189
190
- def data_kind (
190
+ def data_kind ( # noqa: PLR0911
191
191
data : Any , required : bool = True
192
- ) -> Literal ["arg" , "file" , "geojson" , "grid" , "image" , "matrix" , "vectors" ]:
192
+ ) -> Literal ["none" , " arg" , "file" , "geojson" , "grid" , "image" , "matrix" , "vectors" ]:
193
193
"""
194
194
Check the kind of data that is provided to a module.
195
195
196
196
Recognized data kinds are:
197
197
198
- - ``"arg"``: bool, int or float, representing an optional argument, mainly used for
199
- dealing with optional virtual files
198
+ - ``"none"``: None and data is required. In this case, the data is usually given via
199
+ a series of vectors (e.g., x/y/z)
200
+ - ``"arg"``: bool, int, float, or None (only when ``required`` is False),
201
+ representing an optional argument, mainly used for dealing with optional virtual
202
+ files
200
203
- ``"file"``: a string or a :class:`pathlib.PurePath` object or a sequence of them,
201
204
representing a file name or a list of file names
202
205
- ``"geojson"``: a geo-like Python object that implements ``__geo_interface__``
203
206
(e.g., geopandas.GeoDataFrame or shapely.geometry)
204
207
- ``"grid"``: a :class:`xarray.DataArray` object with dimensions not equal to 3
205
208
- ``"image"``: a :class:`xarray.DataArray` object with 3 dimensions
206
- - ``"matrix"``: a :class:`pandas.DataFrame` object, a 2-D :class:`numpy.ndarray`,
207
- a dictionary with array-like values, or a sequence of sequences
209
+ - ``"matrix"``: a 2-D :class:`numpy.ndarray` object
210
+ - ``"vectors"``: a :class:`pandas.DataFrame` object, a dictionary with array-like
211
+ values, or a sequence of sequences
208
212
209
- In addition, the data can be given via a series of vectors (e.g., x/y/z). In this
210
- case, the ``data`` argument is ``None`` and the data kind is determined by the
211
- ``required`` argument. The data kind is ``"vectors"`` if ``required`` is ``True``,
212
- otherwise the data kind is ``"arg"``.
213
-
214
- The function will fallback to ``"matrix"`` for any unrecognized data.
213
+ The function will fallback to ``"vectors"`` for any unrecognized data.
215
214
216
215
Parameters
217
216
----------
@@ -232,12 +231,12 @@ def data_kind(
232
231
>>> import xarray as xr
233
232
>>> import pandas as pd
234
233
>>> import pathlib
235
- >>> [data_kind(data=data) for data in (2, 2.0, True, False)]
236
- ['arg', 'arg', 'arg', 'arg']
237
234
>>> data_kind(data=None)
238
- 'vectors '
235
+ 'none '
239
236
>>> data_kind(data=None, required=False)
240
237
'arg'
238
+ >>> [data_kind(data=data) for data in (2, 2.0, True, False)]
239
+ ['arg', 'arg', 'arg', 'arg']
241
240
>>> data_kind(data="my-data-file.txt")
242
241
'file'
243
242
>>> data_kind(data=pathlib.Path("my-data-file.txt"))
@@ -251,16 +250,16 @@ def data_kind(
251
250
>>> data_kind(data=np.arange(10).reshape((5, 2)))
252
251
'matrix'
253
252
>>> data_kind(data=pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]}))
254
- 'matrix '
253
+ 'vectors '
255
254
>>> data_kind(data={"x": [1, 2], "y": [3, 4]})
256
- 'matrix '
255
+ 'vectors '
257
256
>>> data_kind(data=[1, 2, 3])
258
- 'matrix '
257
+ 'vectors '
259
258
"""
260
259
# data is None, so data must be given via a series of vectors (i.e., x/y/z).
261
260
# The only exception is when dealing with optional virtual files.
262
261
if data is None :
263
- return "vectors " if required else "arg"
262
+ return "none " if required else "arg"
264
263
265
264
# A file or a list of files
266
265
if isinstance (data , str | pathlib .PurePath ) or (
@@ -282,8 +281,12 @@ def data_kind(
282
281
if hasattr (data , "__geo_interface__" ):
283
282
return "geojson"
284
283
285
- # Fallback to "matrix" for anything else
286
- return "matrix"
284
+ # A 2-D numpy.ndarray
285
+ if hasattr (data , "__array_interface__" ) and data .ndim == 2 :
286
+ return "matrix"
287
+
288
+ # Fallback to "vectors" for anything else
289
+ return "vectors"
287
290
288
291
289
292
def non_ascii_to_octal (
0 commit comments