Python pandas.core.tools.datetimes.to_time() Examples

The following are 9 code examples of pandas.core.tools.datetimes.to_time(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pandas.core.tools.datetimes , or try the search function .
Example #1
Source File: test_tools.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_parsers_time(self):
        # GH11818
        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 tools.to_time(time_string) == expected

        new_string = "14.15"
        pytest.raises(ValueError, tools.to_time, new_string)
        assert tools.to_time(new_string, format="%H.%M") == expected

        arg = ["14:15", "20:20"]
        expected_arr = [time(14, 15), time(20, 20)]
        assert tools.to_time(arg) == expected_arr
        assert tools.to_time(arg, format="%H:%M") == expected_arr
        assert tools.to_time(arg, infer_time_format=True) == expected_arr
        assert tools.to_time(arg, format="%I:%M%p",
                             errors="coerce") == [None, None]

        res = tools.to_time(arg, format="%I:%M%p", errors="ignore")
        tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_))

        with pytest.raises(ValueError):
            tools.to_time(arg, format="%I:%M%p", errors="raise")

        tm.assert_series_equal(tools.to_time(Series(arg, name="test")),
                               Series(expected_arr, name="test"))

        res = tools.to_time(np.array(arg))
        assert isinstance(res, list)
        assert res == expected_arr 
Example #2
Source File: test_tools.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_parsers_time(self):
        # GH11818
        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 tools.to_time(time_string) == expected

        new_string = "14.15"
        pytest.raises(ValueError, tools.to_time, new_string)
        assert tools.to_time(new_string, format="%H.%M") == expected

        arg = ["14:15", "20:20"]
        expected_arr = [time(14, 15), time(20, 20)]
        assert tools.to_time(arg) == expected_arr
        assert tools.to_time(arg, format="%H:%M") == expected_arr
        assert tools.to_time(arg, infer_time_format=True) == expected_arr
        assert tools.to_time(arg, format="%I:%M%p",
                             errors="coerce") == [None, None]

        res = tools.to_time(arg, format="%I:%M%p", errors="ignore")
        tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_))

        with pytest.raises(ValueError):
            tools.to_time(arg, format="%I:%M%p", errors="raise")

        tm.assert_series_equal(tools.to_time(Series(arg, name="test")),
                               Series(expected_arr, name="test"))

        res = tools.to_time(np.array(arg))
        assert isinstance(res, list)
        assert res == expected_arr 
Example #3
Source File: test_tools.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_parsers_time(self):
        # GH11818
        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 tools.to_time(time_string) == expected

        new_string = "14.15"
        pytest.raises(ValueError, tools.to_time, new_string)
        assert tools.to_time(new_string, format="%H.%M") == expected

        arg = ["14:15", "20:20"]
        expected_arr = [time(14, 15), time(20, 20)]
        assert tools.to_time(arg) == expected_arr
        assert tools.to_time(arg, format="%H:%M") == expected_arr
        assert tools.to_time(arg, infer_time_format=True) == expected_arr
        assert tools.to_time(arg, format="%I:%M%p",
                             errors="coerce") == [None, None]

        res = tools.to_time(arg, format="%I:%M%p", errors="ignore")
        tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_))

        with pytest.raises(ValueError):
            tools.to_time(arg, format="%I:%M%p", errors="raise")

        tm.assert_series_equal(tools.to_time(Series(arg, name="test")),
                               Series(expected_arr, name="test"))

        res = tools.to_time(np.array(arg))
        assert isinstance(res, list)
        assert res == expected_arr 
Example #4
Source File: test_tools.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_parsers_time(self):
        # GH11818
        _skip_if_has_locale()
        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 tools.to_time(time_string) == expected

        new_string = "14.15"
        pytest.raises(ValueError, tools.to_time, new_string)
        assert tools.to_time(new_string, format="%H.%M") == expected

        arg = ["14:15", "20:20"]
        expected_arr = [time(14, 15), time(20, 20)]
        assert tools.to_time(arg) == expected_arr
        assert tools.to_time(arg, format="%H:%M") == expected_arr
        assert tools.to_time(arg, infer_time_format=True) == expected_arr
        assert tools.to_time(arg, format="%I:%M%p",
                             errors="coerce") == [None, None]

        res = tools.to_time(arg, format="%I:%M%p", errors="ignore")
        tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_))

        with pytest.raises(ValueError):
            tools.to_time(arg, format="%I:%M%p", errors="raise")

        tm.assert_series_equal(tools.to_time(Series(arg, name="test")),
                               Series(expected_arr, name="test"))

        res = tools.to_time(np.array(arg))
        assert isinstance(res, list)
        assert res == expected_arr 
Example #5
Source File: test_tools.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_parsers_time(self):
        # GH11818
        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 tools.to_time(time_string) == expected

        new_string = "14.15"
        pytest.raises(ValueError, tools.to_time, new_string)
        assert tools.to_time(new_string, format="%H.%M") == expected

        arg = ["14:15", "20:20"]
        expected_arr = [time(14, 15), time(20, 20)]
        assert tools.to_time(arg) == expected_arr
        assert tools.to_time(arg, format="%H:%M") == expected_arr
        assert tools.to_time(arg, infer_time_format=True) == expected_arr
        assert tools.to_time(arg, format="%I:%M%p",
                             errors="coerce") == [None, None]

        res = tools.to_time(arg, format="%I:%M%p", errors="ignore")
        tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_))

        with pytest.raises(ValueError):
            tools.to_time(arg, format="%I:%M%p", errors="raise")

        tm.assert_series_equal(tools.to_time(Series(arg, name="test")),
                               Series(expected_arr, name="test"))

        res = tools.to_time(np.array(arg))
        assert isinstance(res, list)
        assert res == expected_arr 
Example #6
Source File: test_tools.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_parsers_time(self):
        # GH11818
        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 tools.to_time(time_string) == expected

        new_string = "14.15"
        pytest.raises(ValueError, tools.to_time, new_string)
        assert tools.to_time(new_string, format="%H.%M") == expected

        arg = ["14:15", "20:20"]
        expected_arr = [time(14, 15), time(20, 20)]
        assert tools.to_time(arg) == expected_arr
        assert tools.to_time(arg, format="%H:%M") == expected_arr
        assert tools.to_time(arg, infer_time_format=True) == expected_arr
        assert tools.to_time(arg, format="%I:%M%p",
                             errors="coerce") == [None, None]

        res = tools.to_time(arg, format="%I:%M%p", errors="ignore")
        tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_))

        with pytest.raises(ValueError):
            tools.to_time(arg, format="%I:%M%p", errors="raise")

        tm.assert_series_equal(tools.to_time(Series(arg, name="test")),
                               Series(expected_arr, name="test"))

        res = tools.to_time(np.array(arg))
        assert isinstance(res, list)
        assert res == expected_arr 
Example #7
Source File: datetimes.py    From recruit with Apache License 2.0 4 votes vote down vote up
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 : boolean, default True
        include_end : boolean, default True

        Returns
        -------
        values_between_time : array of integers

        See Also
        --------
        indexer_at_time, DataFrame.between_time
        """
        start_time = tools.to_time(start_time)
        end_time = tools.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] 
Example #8
Source File: datetimes.py    From vnpy_crypto with MIT License 4 votes vote down vote up
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 : boolean, default True
        include_end : boolean, default True

        Returns
        -------
        values_between_time : array of integers

        See Also
        --------
        indexer_at_time, DataFrame.between_time
        """
        start_time = tools.to_time(start_time)
        end_time = tools.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] 
Example #9
Source File: datetimes.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 4 votes vote down vote up
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 : boolean, default True
        include_end : boolean, default True

        Returns
        -------
        values_between_time : array of integers

        See Also
        --------
        indexer_at_time, DataFrame.between_time
        """
        start_time = tools.to_time(start_time)
        end_time = tools.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]