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

Function comparison_op

pandas/core/ops/array_ops.py:294–361  ·  view source on GitHub ↗

Evaluate a comparison operation `=`, `!=`, `>=`, `>`, `<=`, or `<`. Note: the caller is responsible for ensuring that numpy warnings are suppressed (with np.errstate(all="ignore")) if needed. Parameters ---------- left : np.ndarray or ExtensionArray right : object

(left: ArrayLike, right: Any, op)

Source from the content-addressed store, hash-verified

292
293
294def comparison_op(left: ArrayLike, right: Any, op) -> ArrayLike:
295 """
296 Evaluate a comparison operation `=`, `!=`, `>=`, `>`, `<=`, or `<`.
297
298 Note: the caller is responsible for ensuring that numpy warnings are
299 suppressed (with np.errstate(all="ignore")) if needed.
300
301 Parameters
302 ----------
303 left : np.ndarray or ExtensionArray
304 right : object
305 Cannot be a DataFrame, Series, or Index.
306 op : {operator.eq, operator.ne, operator.gt, operator.ge, operator.lt, operator.le}
307
308 Returns
309 -------
310 ndarray or ExtensionArray
311 """
312 # NB: We assume extract_array has already been called on left and right
313 lvalues = ensure_wrapped_if_datetimelike(left)
314 rvalues = ensure_wrapped_if_datetimelike(right)
315
316 rvalues = lib.item_from_zerodim(rvalues)
317
318 # Special handling needed if rvalues is a zerodim np.ndarray subclass, see GH#63205
319 rvalues_is_zerodim: bool = getattr(rvalues, "ndim", None) == 0
320
321 if isinstance(rvalues, list):
322 # We don't catch tuple here bc we may be comparing e.g. MultiIndex
323 # to a tuple that represents a single entry, see test_compare_tuple_strs
324 rvalues = sanitize_array(rvalues, None)
325 rvalues = ensure_wrapped_if_datetimelike(rvalues)
326
327 if isinstance(rvalues, (np.ndarray, ABCExtensionArray)) and not rvalues_is_zerodim:
328 # TODO: make this treatment consistent across ops and classes.
329 # We are not catching all listlikes here (e.g. frozenset, tuple)
330 # The ambiguous case is object-dtype. See GH#27803
331 if len(lvalues) != len(rvalues):
332 raise ValueError(
333 "Lengths must match to compare", lvalues.shape, rvalues.shape
334 )
335
336 if should_extension_dispatch(lvalues, rvalues) or (
337 (isinstance(rvalues, (Timedelta, BaseOffset, Timestamp)) or right is NaT)
338 and lvalues.dtype != object
339 ):
340 # Call the method on lvalues
341 res_values = op(lvalues, rvalues)
342
343 # TODO: but not pd.NA?
344 elif (is_scalar(rvalues) or rvalues_is_zerodim) and isna(rvalues):
345 # numpy does not like comparisons vs None
346 if op is operator.ne:
347 res_values = np.ones(lvalues.shape, dtype=bool)
348 else:
349 res_values = np.zeros(lvalues.shape, dtype=bool)
350
351 elif is_numeric_v_string_like(lvalues, rvalues):

Calls 9

sanitize_arrayFunction · 0.90
isnaFunction · 0.90
is_numeric_v_string_likeFunction · 0.90
invalid_comparisonFunction · 0.90
opFunction · 0.85
comp_method_OBJECT_ARRAYFunction · 0.85
_na_arithmetic_opFunction · 0.85