def indexer_between_time(self, start_time, end_time, include_start=True, include_end=True): """ Return index locations of values between particular times of day (e.g., 9:00-9:30AM). Parameters ---------- start_time, end_time : datetime.time, str datetime.time or string in appropriate format ("%H:%M", "%H%M", "%I:%M%p", "%I%M%p", "%H:%M:%S", "%H%M%S", "%I:%M:%S%p", "%I%M%S%p"). include_start : bool, default True include_end : bool, default True Returns ------- values_between_time : array of integers See Also -------- indexer_at_time : Get index locations of values at particular time of day. DataFrame.between_time : Select values between particular times of day. """ start_time = to_time(start_time) end_time = to_time(end_time) time_micros = self._get_time_micros() start_micros = _time_to_micros(start_time) end_micros = _time_to_micros(end_time) if include_start and include_end: lop = rop = operator.le elif include_start: lop = operator.le rop = operator.lt elif include_end: lop = operator.lt rop = operator.le else: lop = rop = operator.lt if start_time <= end_time: join_op = operator.and_ else: join_op = operator.or_ mask = join_op(lop(start_micros, time_micros), rop(time_micros, end_micros)) return mask.nonzero()[0]
def indexer_between_time(self, start_time, end_time, include_start: bool = True, include_end: bool = True) -> npt.NDArray[np.intp]: """ Return index locations of values between particular times of day. Parameters ---------- start_time, end_time : datetime.time, str Time passed either as object (datetime.time) or as string in appropriate format ("%H:%M", "%H%M", "%I:%M%p", "%I%M%p", "%H:%M:%S", "%H%M%S", "%I:%M:%S%p","%I%M%S%p"). include_start : bool, default True include_end : bool, default True Returns ------- np.ndarray[np.intp] See Also -------- indexer_at_time : Get index locations of values at particular time of day. DataFrame.between_time : Select values between particular times of day. """ start_time = to_time(start_time) end_time = to_time(end_time) time_micros = self._get_time_micros() start_micros = _time_to_micros(start_time) end_micros = _time_to_micros(end_time) if include_start and include_end: lop = rop = operator.le elif include_start: lop = operator.le rop = operator.lt elif include_end: lop = operator.lt rop = operator.le else: lop = rop = operator.lt if start_time <= end_time: join_op = operator.and_ else: join_op = operator.or_ mask = join_op(lop(start_micros, time_micros), rop(time_micros, end_micros)) return mask.nonzero()[0]
def to_time(arg, format=None, infer_time_format=False, errors="raise"): # GH#34145 warnings.warn( "`to_time` has been moved, should be imported from pandas.core.tools.times. " "This alias will be removed in a future version.", FutureWarning, stacklevel=2, ) from pandas.core.tools.times import to_time return to_time(arg, format, infer_time_format, errors)
def test_arraylike(self): arg = ["14:15", "20:20"] expected_arr = [time(14, 15), time(20, 20)] assert to_time(arg) == expected_arr assert to_time(arg, format="%H:%M") == expected_arr assert to_time(arg, infer_time_format=True) == expected_arr assert to_time(arg, format="%I:%M%p", errors="coerce") == [None, None] res = to_time(arg, format="%I:%M%p", errors="ignore") tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_)) msg = "Cannot convert.+to a time with given format" with pytest.raises(ValueError, match=msg): to_time(arg, format="%I:%M%p", errors="raise") tm.assert_series_equal(to_time(Series(arg, name="test")), Series(expected_arr, name="test")) res = to_time(np.array(arg)) assert isinstance(res, list) assert res == expected_arr
def _from_sequence_of_strings(cls, strings, *, dtype: Dtype | None = None, copy=False): """ Construct a new ExtensionArray from a sequence of strings. """ pa_type = to_pyarrow_type(dtype) if pa.types.is_timestamp(pa_type): from pandas.core.tools.datetimes import to_datetime scalars = to_datetime(strings, errors="raise") elif pa.types.is_date(pa_type): from pandas.core.tools.datetimes import to_datetime scalars = to_datetime(strings, errors="raise").date elif pa.types.is_duration(pa_type): from pandas.core.tools.timedeltas import to_timedelta scalars = to_timedelta(strings, errors="raise") elif pa.types.is_time(pa_type): from pandas.core.tools.times import to_time # "coerce" to allow "null times" (None) to not raise scalars = to_time(strings, errors="coerce") elif pa.types.is_boolean(pa_type): from pandas.core.arrays import BooleanArray scalars = BooleanArray._from_sequence_of_strings( strings).to_numpy() elif (pa.types.is_integer(pa_type) or pa.types.is_floating(pa_type) or pa.types.is_decimal(pa_type)): from pandas.core.tools.numeric import to_numeric scalars = to_numeric(strings, errors="raise") else: # Let pyarrow try to infer or raise scalars = strings return cls._from_sequence(scalars, dtype=pa_type, copy=copy)
def test_odd_format(self): new_string = "14.15" msg = r"Cannot convert arg \['14\.15'\] to a time" with pytest.raises(ValueError, match=msg): to_time(new_string) assert to_time(new_string, format="%H.%M") == time(14, 15)
def test_parsers_time(self, time_string): # GH#11818 assert to_time(time_string) == time(14, 15)
def test_parsers_time(self): # GH#11818 strings = [ "14:15", "1415", "2:15pm", "0215pm", "14:15:00", "141500", "2:15:00pm", "021500pm", time(14, 15), ] expected = time(14, 15) for time_string in strings: assert to_time(time_string) == expected new_string = "14.15" msg = r"Cannot convert arg \['14\.15'\] to a time" with pytest.raises(ValueError, match=msg): to_time(new_string) assert to_time(new_string, format="%H.%M") == expected arg = ["14:15", "20:20"] expected_arr = [time(14, 15), time(20, 20)] assert to_time(arg) == expected_arr assert to_time(arg, format="%H:%M") == expected_arr assert to_time(arg, infer_time_format=True) == expected_arr assert to_time(arg, format="%I:%M%p", errors="coerce") == [None, None] res = to_time(arg, format="%I:%M%p", errors="ignore") tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_)) msg = "Cannot convert.+to a time with given format" with pytest.raises(ValueError, match=msg): to_time(arg, format="%I:%M%p", errors="raise") tm.assert_series_equal(to_time(Series(arg, name="test")), Series(expected_arr, name="test")) res = to_time(np.array(arg)) assert isinstance(res, list) assert res == expected_arr