MCPcopy
hub / github.com/pandas-dev/pandas / _ensure_data

Function _ensure_data

pandas/core/algorithms.py:114–189  ·  view source on GitHub ↗

routine to ensure that our data is of the correct input dtype for lower-level routines This will coerce: - ints -> int64 - uint -> uint64 - bool -> uint8 - datetimelike -> i8 - datetime64tz -> i8 (in local tz) - categorical -> codes Parameters ---------

(values: ArrayLike)

Source from the content-addressed store, hash-verified

112# dtype access #
113# --------------- #
114def _ensure_data(values: ArrayLike) -> np.ndarray:
115 """
116 routine to ensure that our data is of the correct
117 input dtype for lower-level routines
118
119 This will coerce:
120 - ints -> int64
121 - uint -> uint64
122 - bool -> uint8
123 - datetimelike -> i8
124 - datetime64tz -> i8 (in local tz)
125 - categorical -> codes
126
127 Parameters
128 ----------
129 values : np.ndarray or ExtensionArray
130
131 Returns
132 -------
133 np.ndarray
134 """
135
136 if not isinstance(values, ABCMultiIndex):
137 # extract_array would raise
138 values = extract_array(values, extract_numpy=True)
139
140 if is_object_dtype(values.dtype):
141 return ensure_object(np.asarray(values))
142
143 elif isinstance(values.dtype, BaseMaskedDtype):
144 # i.e. BooleanArray, FloatingArray, IntegerArray
145 values = cast("BaseMaskedArray", values)
146 if not values._hasna:
147 # No pd.NAs -> We can avoid an object-dtype cast (and copy) GH#41816
148 # recurse to avoid re-implementing logic for eg bool->uint8
149 return _ensure_data(values._data)
150 return np.asarray(values)
151
152 elif isinstance(values.dtype, CategoricalDtype):
153 # NB: cases that go through here should NOT be using _reconstruct_data
154 # on the back-end.
155 values = cast("Categorical", values)
156 return values.codes
157
158 elif is_bool_dtype(values.dtype):
159 if isinstance(values, np.ndarray):
160 # i.e. actually dtype == np.dtype("bool")
161 return np.asarray(values).view("uint8")
162 else:
163 # e.g. Sparse[bool, False] # TODO: no test cases get here
164 return np.asarray(values).astype("uint8", copy=False)
165
166 elif is_integer_dtype(values.dtype):
167 return np.asarray(values)
168
169 elif is_float_dtype(values.dtype):
170 # Note: checking `values.dtype == "float128"` raises on Windows and 32bit
171 # error: Item "ExtensionDtype" of "Union[Any, ExtensionDtype, dtype[Any]]"

Callers 6

_get_hashtable_algoFunction · 0.85
nunique_intsFunction · 0.85
value_counts_arraylikeFunction · 0.85
duplicatedFunction · 0.85
modeFunction · 0.85
rankFunction · 0.85

Calls 9

extract_arrayFunction · 0.90
is_object_dtypeFunction · 0.90
is_bool_dtypeFunction · 0.90
is_integer_dtypeFunction · 0.90
is_float_dtypeFunction · 0.90
is_complex_dtypeFunction · 0.90
needs_i8_conversionFunction · 0.90
viewMethod · 0.45
astypeMethod · 0.45

Tested by

no test coverage detected