def delete(self, loc) -> Index: # type: ignore[override] # In some cases we can retain RangeIndex, see also # DatetimeTimedeltaMixin._get_delete_Freq if is_integer(loc): if loc == 0 or loc == -len(self): return self[1:] if loc == -1 or loc == len(self) - 1: return self[:-1] elif lib.is_list_like(loc): slc = lib.maybe_indices_to_slice(np.asarray(loc, dtype=np.intp), len(self)) if isinstance(slc, slice) and slc.step is not None and slc.step < 0: rng = range(len(self))[slc][::-1] slc = slice(rng.start, rng.stop, rng.step) if isinstance(slc, slice) and slc.step in [1, None]: # Note: maybe_indices_to_slice will never return a slice # with 'slc.start is None'; may have slc.stop None in cases # with negative step if slc.start == 0: return self[slc.stop:] elif slc.stop in [len(self), None]: return self[:slc.start] # TODO: more generally, self.difference(self[slc]), # once _difference is better about retaining RangeIndex return super().delete(loc)
def new_method(self, other): if is_cmp and isinstance(self, ABCIndexClass) and isinstance( other, ABCSeries): # For comparison ops, Index does *not* defer to Series pass else: for cls in [ABCDataFrame, ABCSeries, ABCIndexClass]: if isinstance(self, cls): break if isinstance(other, cls): return NotImplemented other = item_from_zerodim(other) if isinstance(self, (ABCSeries, ABCDataFrame)) and isinstance( other, (ABCSeries, ABCDataFrame)): # we dont require length matches pass elif is_list_like(other, allow_sets=False) and not isinstance( other, (dict, UserDict)): if len(other) != len(self): if len(other) == 1 and not hasattr(other, "dtype"): # i.e. unpack scalar list, but leave e.g. Categorical, # for which the scalar behavior doesnt match the # array behavior other = other[0] else: raise ValueError("Lengths must match", self.shape, np.shape(other), type(other)) return method(self, other)
def delete(self, loc) -> Index: # type: ignore[override] # In some cases we can retain RangeIndex, see also # DatetimeTimedeltaMixin._get_delete_Freq if is_integer(loc): if loc == 0 or loc == -len(self): return self[1:] if loc == -1 or loc == len(self) - 1: return self[:-1] if len(self) == 3 and (loc == 1 or loc == -2): return self[::2] elif lib.is_list_like(loc): slc = lib.maybe_indices_to_slice(np.asarray(loc, dtype=np.intp), len(self)) if isinstance(slc, slice): # defer to RangeIndex._difference, which is optimized to return # a RangeIndex whenever possible other = self[slc] return self.difference(other, sort=False) return super().delete(loc)
def _has_aliases(self) -> bool: """Whether the aliases for column names are present.""" return is_list_like(self.header)
def to_time(arg, format=None, infer_time_format=False, errors="raise"): """ Parse time strings to time objects using fixed strptime formats ("%H:%M", "%H%M", "%I:%M%p", "%I%M%p", "%H:%M:%S", "%H%M%S", "%I:%M:%S%p", "%I%M%S%p") Use infer_time_format if all the strings are in the same format to speed up conversion. Parameters ---------- arg : string in time format, datetime.time, list, tuple, 1-d array, Series format : str, default None Format used to convert arg into a time object. If None, fixed formats are used. infer_time_format: bool, default False Infer the time format based on the first non-NaN element. If all strings are in the same format, this will speed up conversion. errors : {'ignore', 'raise', 'coerce'}, default 'raise' - If 'raise', then invalid parsing will raise an exception - If 'coerce', then invalid parsing will be set as None - If 'ignore', then invalid parsing will return the input Returns ------- datetime.time """ def _convert_listlike(arg, format): if isinstance(arg, (list, tuple)): arg = np.array(arg, dtype="O") elif getattr(arg, "ndim", 1) > 1: raise TypeError( "arg must be a string, datetime, list, tuple, 1-d array, or Series" ) arg = np.asarray(arg, dtype="O") if infer_time_format and format is None: format = _guess_time_format_for_array(arg) times: List[Optional[time]] = [] if format is not None: for element in arg: try: times.append(datetime.strptime(element, format).time()) except (ValueError, TypeError) as err: if errors == "raise": msg = ( f"Cannot convert {element} to a time with given " f"format {format}") raise ValueError(msg) from err elif errors == "ignore": return arg else: times.append(None) else: formats = _time_formats[:] format_found = False for element in arg: time_object = None for time_format in formats: try: time_object = datetime.strptime(element, time_format).time() if not format_found: # Put the found format in front fmt = formats.pop(formats.index(time_format)) formats.insert(0, fmt) format_found = True break except (ValueError, TypeError): continue if time_object is not None: times.append(time_object) elif errors == "raise": raise ValueError(f"Cannot convert arg {arg} to a time") elif errors == "ignore": return arg else: times.append(None) return times if arg is None: return arg elif isinstance(arg, time): return arg elif isinstance(arg, ABCSeries): values = _convert_listlike(arg._values, format) return arg._constructor(values, index=arg.index, name=arg.name) elif isinstance(arg, Index): return _convert_listlike(arg, format) elif is_list_like(arg): return _convert_listlike(arg, format) return _convert_listlike(np.array([arg]), format)[0]
def time_is_list_like(self, param): is_list_like(param)