MCPcopy Create free account
hub / github.com/numpy/numpy / mintypecode

Function mintypecode

numpy/lib/type_check.py:26–76  ·  view source on GitHub ↗

Return the character for the minimum-size type to which given types can be safely cast. The returned type character must represent the smallest size dtype such that an array of the returned type can handle the data from an array of all types in `typechars` (or if `typechars` is

(typechars, typeset='GDFgdf', default='d')

Source from the content-addressed store, hash-verified

24
25@set_module('numpy')
26def mintypecode(typechars, typeset='GDFgdf', default='d'):
27 """
28 Return the character for the minimum-size type to which given types can
29 be safely cast.
30
31 The returned type character must represent the smallest size dtype such
32 that an array of the returned type can handle the data from an array of
33 all types in `typechars` (or if `typechars` is an array, then its
34 dtype.char).
35
36 Parameters
37 ----------
38 typechars : list of str or array_like
39 If a list of strings, each string should represent a dtype.
40 If array_like, the character representation of the array dtype is used.
41 typeset : str or list of str, optional
42 The set of characters that the returned character is chosen from.
43 The default set is 'GDFgdf'.
44 default : str, optional
45 The default character, this is returned if none of the characters in
46 `typechars` matches a character in `typeset`.
47
48 Returns
49 -------
50 typechar : str
51 The character representing the minimum-size type that was found.
52
53 See Also
54 --------
55 dtype, sctype2char, maximum_sctype
56
57 Examples
58 --------
59 >>> np.mintypecode(['d', 'f', 'S'])
60 'd'
61 >>> x = np.array([1.1, 2-3.j])
62 >>> np.mintypecode(x)
63 'D'
64
65 >>> np.mintypecode('abceh', default='G')
66 'G'
67
68 """
69 typecodes = ((isinstance(t, str) and t) or asarray(t).dtype.char
70 for t in typechars)
71 intersection = set(t for t in typecodes if t in typeset)
72 if not intersection:
73 return default
74 if 'F' in intersection and 'd' in intersection:
75 return 'D'
76 return min(intersection, key=_typecodes_by_elsize.index)
77
78
79def _asfarray_dispatcher(a, dtype=None):

Callers 4

polyFunction · 0.90
test_default_1Method · 0.90
test_default_2Method · 0.90
test_default_3Method · 0.90

Calls 2

asarrayFunction · 0.50
minFunction · 0.50

Tested by 3

test_default_1Method · 0.72
test_default_2Method · 0.72
test_default_3Method · 0.72