MCPcopy Create free account
hub / github.com/dask/dask / _meta_from_array

Function _meta_from_array

dask/dataframe/io/io.py:24–79  ·  view source on GitHub ↗

Create empty DataFrame or Series which has correct dtype

(x, columns=None, index=None, meta=None)

Source from the content-addressed store, hash-verified

22
23
24def _meta_from_array(x, columns=None, index=None, meta=None):
25 """Create empty DataFrame or Series which has correct dtype"""
26
27 if x.ndim > 2:
28 raise ValueError(
29 "from_array does not input more than 2D array, got"
30 f" array with shape {x.shape!r}"
31 )
32
33 if index is not None:
34 from dask.dataframe import Index
35
36 if not isinstance(index, Index):
37 raise ValueError("'index' must be an instance of dask.dataframe.Index")
38 index = index._meta
39
40 if meta is None:
41 meta = meta_lib_from_array(x).DataFrame()
42
43 if getattr(x.dtype, "names", None) is not None:
44 # record array has named columns
45 if columns is None:
46 columns = list(x.dtype.names)
47 elif np.isscalar(columns):
48 raise ValueError("For a struct dtype, columns must be a list.")
49 elif not all(i in x.dtype.names for i in columns):
50 extra = sorted(set(columns).difference(x.dtype.names))
51 raise ValueError(f"dtype {x.dtype} doesn't have fields {extra}")
52 fields = x.dtype.fields
53 dtypes = [fields[n][0] if n in fields else "f8" for n in columns]
54 elif x.ndim == 1:
55 if np.isscalar(columns) or columns is None:
56 return meta._constructor_sliced(
57 [], name=columns, dtype=x.dtype, index=index
58 )
59 elif len(columns) == 1:
60 return meta._constructor(
61 np.array([], dtype=x.dtype), columns=columns, index=index
62 )
63 raise ValueError(
64 "For a 1d array, columns must be a scalar or single element list"
65 )
66 else:
67 if np.isnan(x.shape[1]):
68 raise ValueError("Shape along axis 1 must be known")
69 if columns is None:
70 columns = list(range(x.shape[1])) if x.ndim == 2 else [0]
71 elif len(columns) != x.shape[1]:
72 raise ValueError(
73 "Number of column names must match width of the array. "
74 f"Got {len(columns)} names for {x.shape[1]} columns"
75 )
76 dtypes = [x.dtype] * len(columns)
77
78 data = {c: np.array([], dtype=dt) for (c, dt) in zip(columns, dtypes)}
79 return meta._constructor(data, columns=columns, index=index)
80
81

Callers 5

_metaMethod · 0.90
test_meta_from_arrayFunction · 0.90
test_meta_from_1darrayFunction · 0.90
test_meta_from_recarrayFunction · 0.90
from_dask_arrayFunction · 0.85

Calls 3

allFunction · 0.85
setClass · 0.85
_constructorMethod · 0.80

Tested by 3

test_meta_from_arrayFunction · 0.72
test_meta_from_1darrayFunction · 0.72
test_meta_from_recarrayFunction · 0.72