MCPcopy Create free account
hub / github.com/quantopian/zipline / _normalize_array

Function _normalize_array

zipline/lib/adjusted_array.py:84–138  ·  view source on GitHub ↗

Coerce buffer data for an AdjustedArray into a standard scalar representation, returning the coerced array and a dict of argument to pass to np.view to use when providing a user-facing view of the underlying data. - float* data is coerced to float64 with viewtype float64. - int

(data, missing_value)

Source from the content-addressed store, hash-verified

82
83
84def _normalize_array(data, missing_value):
85 """
86 Coerce buffer data for an AdjustedArray into a standard scalar
87 representation, returning the coerced array and a dict of argument to pass
88 to np.view to use when providing a user-facing view of the underlying data.
89
90 - float* data is coerced to float64 with viewtype float64.
91 - int32, int64, and uint32 are converted to int64 with viewtype int64.
92 - datetime[*] data is coerced to int64 with a viewtype of datetime64[ns].
93 - bool_ data is coerced to uint8 with a viewtype of bool_.
94
95 Parameters
96 ----------
97 data : np.ndarray
98
99 Returns
100 -------
101 coerced, view_kwargs : (np.ndarray, np.dtype)
102 The input ``data`` array coerced to the appropriate pipeline type.
103 This may return the original array or a view over the same data.
104 """
105 if isinstance(data, LabelArray):
106 return data, {}
107
108 data_dtype = data.dtype
109 if data_dtype in BOOL_DTYPES:
110 return data.astype(uint8, copy=False), {'dtype': dtype(bool_)}
111 elif data_dtype in FLOAT_DTYPES:
112 return data.astype(float64, copy=False), {'dtype': dtype(float64)}
113 elif data_dtype in INT_DTYPES:
114 return data.astype(int64, copy=False), {'dtype': dtype(int64)}
115 elif is_categorical(data_dtype):
116 if not isinstance(missing_value, LabelArray.SUPPORTED_SCALAR_TYPES):
117 raise TypeError(
118 "Invalid missing_value for categorical array.\n"
119 "Expected None, bytes or unicode. Got %r." % missing_value,
120 )
121 return LabelArray(data, missing_value), {}
122 elif data_dtype.kind == 'M':
123 try:
124 outarray = data.astype('datetime64[ns]', copy=False).view('int64')
125 return outarray, {'dtype': datetime64ns_dtype}
126 except OverflowError:
127 raise ValueError(
128 "AdjustedArray received a datetime array "
129 "not representable as datetime64[ns].\n"
130 "Min Date: %s\n"
131 "Max Date: %s\n"
132 % (data.min(), data.max())
133 )
134 else:
135 raise TypeError(
136 "Don't know how to construct AdjustedArray "
137 "on data of type %s." % data_dtype
138 )
139
140
141def _merge_simple(adjustment_lists, front_idx, back_idx):

Callers 1

__init__Method · 0.85

Calls 6

LabelArrayClass · 0.90
is_categoricalFunction · 0.85
astypeMethod · 0.80
viewMethod · 0.80
minMethod · 0.80
maxMethod · 0.80

Tested by

no test coverage detected