MCPcopy
hub / github.com/dask/dask / make_meta_object

Function make_meta_object

dask/dataframe/backends.py:279–341  ·  view source on GitHub ↗

Create an empty pandas object containing the desired metadata. Parameters ---------- x : dict, tuple, list, pd.Series, pd.DataFrame, pd.Index, dtype, scalar To create a DataFrame, provide a `dict` mapping of `{name: dtype}`, or an iterable of `(name, dtype)` tuples. To c

(x, index=None)

Source from the content-addressed store, hash-verified

277
278@make_meta_obj.register(meta_object_types)
279def make_meta_object(x, index=None):
280 """Create an empty pandas object containing the desired metadata.
281
282 Parameters
283 ----------
284 x : dict, tuple, list, pd.Series, pd.DataFrame, pd.Index, dtype, scalar
285 To create a DataFrame, provide a `dict` mapping of `{name: dtype}`, or
286 an iterable of `(name, dtype)` tuples. To create a `Series`, provide a
287 tuple of `(name, dtype)`. If a pandas object, names, dtypes, and index
288 should match the desired output. If a dtype or scalar, a scalar of the
289 same dtype is returned.
290 index : pd.Index, optional
291 Any pandas index to use in the metadata. If none provided, a
292 `RangeIndex` will be used.
293
294 Examples
295 --------
296
297 >>> make_meta_object([('a', 'i8'), ('b', 'O')])
298 Empty DataFrame
299 Columns: [a, b]
300 Index: []
301 >>> make_meta_object(('a', 'f8'))
302 Series([], Name: a, dtype: float64)
303 >>> make_meta_object('i8')
304 np.int64(1)
305 """
306
307 if is_arraylike(x) and x.shape:
308 return x[:0]
309
310 if index is not None:
311 index = make_meta_dispatch(index)
312
313 if isinstance(x, dict):
314 return pd.DataFrame(
315 {c: _empty_series(c, d, index=index) for (c, d) in x.items()}, index=index
316 )
317 if isinstance(x, tuple) and len(x) == 2:
318 return _empty_series(x[0], x[1], index=index)
319 elif isinstance(x, Iterable) and not isinstance(x, str):
320 if not all(isinstance(i, tuple) and len(i) == 2 for i in x):
321 raise ValueError(f"Expected iterable of tuples of (name, dtype), got {x}")
322 return pd.DataFrame(
323 {c: _empty_series(c, d, index=index) for (c, d) in x},
324 columns=[c for c, d in x],
325 index=index,
326 )
327 elif not hasattr(x, "dtype") and x is not None:
328 # could be a string, a dtype object, or a python type. Skip `None`,
329 # because it is implicitly converted to `dtype('f8')`, which we don't
330 # want here.
331 try:
332 dtype = np.dtype(x)
333 return _scalar_from_dtype(dtype)
334 except Exception:
335 # Continue on to next check
336 pass

Callers

nothing calls this directly

Calls 8

is_arraylikeFunction · 0.90
_empty_seriesFunction · 0.90
_scalar_from_dtypeFunction · 0.90
_nonempty_scalarFunction · 0.90
allFunction · 0.85
is_scalarFunction · 0.85
itemsMethod · 0.45
dtypeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…