Python pandas._libs.tslib.array_to_datetime() Examples

The following are 30 code examples of pandas._libs.tslib.array_to_datetime(). 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._libs.tslib , or try the search function .
Example #1
Source File: test_array_to_datetime.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_coerce_of_invalid_datetimes(self):
        arr = np.array(['01-01-2013', 'not_a_date', '1'], dtype=object)

        # Without coercing, the presence of any invalid dates prevents
        # any values from being converted
        result = tslib.array_to_datetime(arr, errors='ignore')
        tm.assert_numpy_array_equal(result, arr)

        # With coercing, the invalid dates becomes iNaT
        result = tslib.array_to_datetime(arr, errors='coerce')
        expected = ['2013-01-01T00:00:00.000000000-0000',
                    tslib.iNaT,
                    tslib.iNaT]

        tm.assert_numpy_array_equal(
            result,
            np_array_datetime64_compat(expected, dtype='M8[ns]')) 
Example #2
Source File: test_array_to_datetime.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_coerce_of_invalid_datetimes(errors):
    arr = np.array(["01-01-2013", "not_a_date", "1"], dtype=object)
    kwargs = dict(values=arr, errors=errors)

    if errors == "ignore":
        # Without coercing, the presence of any invalid
        # dates prevents any values from being converted.
        result, _ = tslib.array_to_datetime(**kwargs)
        tm.assert_numpy_array_equal(result, arr)
    else:  # coerce.
        # With coercing, the invalid dates becomes iNaT
        result, _ = tslib.array_to_datetime(arr, errors="coerce")
        expected = ["2013-01-01T00:00:00.000000000-0000",
                    iNaT,
                    iNaT]

        tm.assert_numpy_array_equal(
            result,
            np_array_datetime64_compat(expected, dtype="M8[ns]")) 
Example #3
Source File: test_array_to_datetime.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_parsing_valid_dates(self):
        arr = np.array(['01-01-2013', '01-02-2013'], dtype=object)
        result = tslib.array_to_datetime(arr)
        expected = ['2013-01-01T00:00:00.000000000-0000',
                    '2013-01-02T00:00:00.000000000-0000']
        tm.assert_numpy_array_equal(
            result,
            np_array_datetime64_compat(expected, dtype='M8[ns]'))

        arr = np.array(['Mon Sep 16 2013', 'Tue Sep 17 2013'], dtype=object)
        result = tslib.array_to_datetime(arr)
        expected = ['2013-09-16T00:00:00.000000000-0000',
                    '2013-09-17T00:00:00.000000000-0000']
        tm.assert_numpy_array_equal(
            result,
            np_array_datetime64_compat(expected, dtype='M8[ns]')) 
Example #4
Source File: test_tools.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_parsing_valid_dates(self):
        arr = np.array(['01-01-2013', '01-02-2013'], dtype=object)
        tm.assert_numpy_array_equal(
            tslib.array_to_datetime(arr),
            np_array_datetime64_compat(
                [
                    '2013-01-01T00:00:00.000000000-0000',
                    '2013-01-02T00:00:00.000000000-0000'
                ],
                dtype='M8[ns]'
            )
        )

        arr = np.array(['Mon Sep 16 2013', 'Tue Sep 17 2013'], dtype=object)
        tm.assert_numpy_array_equal(
            tslib.array_to_datetime(arr),
            np_array_datetime64_compat(
                [
                    '2013-09-16T00:00:00.000000000-0000',
                    '2013-09-17T00:00:00.000000000-0000'
                ],
                dtype='M8[ns]'
            )
        ) 
Example #5
Source File: test_array_to_datetime.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_coerce_of_invalid_datetimes(errors):
    arr = np.array(["01-01-2013", "not_a_date", "1"], dtype=object)
    kwargs = dict(values=arr, errors=errors)

    if errors == "ignore":
        # Without coercing, the presence of any invalid
        # dates prevents any values from being converted.
        result, _ = tslib.array_to_datetime(**kwargs)
        tm.assert_numpy_array_equal(result, arr)
    else:  # coerce.
        # With coercing, the invalid dates becomes iNaT
        result, _ = tslib.array_to_datetime(arr, errors="coerce")
        expected = ["2013-01-01T00:00:00.000000000-0000",
                    iNaT,
                    iNaT]

        tm.assert_numpy_array_equal(
            result,
            np_array_datetime64_compat(expected, dtype="M8[ns]")) 
Example #6
Source File: test_array_to_datetime.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def test_coerce_of_invalid_datetimes(errors):
    arr = np.array(["01-01-2013", "not_a_date", "1"], dtype=object)
    kwargs = dict(values=arr, errors=errors)

    if errors == "ignore":
        # Without coercing, the presence of any invalid
        # dates prevents any values from being converted.
        result, _ = tslib.array_to_datetime(**kwargs)
        tm.assert_numpy_array_equal(result, arr)
    else:  # coerce.
        # With coercing, the invalid dates becomes iNaT
        result, _ = tslib.array_to_datetime(arr, errors="coerce")
        expected = ["2013-01-01T00:00:00.000000000-0000",
                    iNaT,
                    iNaT]

        tm.assert_numpy_array_equal(
            result,
            np_array_datetime64_compat(expected, dtype="M8[ns]")) 
Example #7
Source File: test_tools.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_parsing_timezone_offsets(self):
        # All of these datetime strings with offsets are equivalent
        # to the same datetime after the timezone offset is added
        dt_strings = [
            '01-01-2013 08:00:00+08:00',
            '2013-01-01T08:00:00.000000000+0800',
            '2012-12-31T16:00:00.000000000-0800',
            '12-31-2012 23:00:00-01:00'
        ]

        expected_output = tslib.array_to_datetime(np.array(
            ['01-01-2013 00:00:00'], dtype=object))

        for dt_string in dt_strings:
            tm.assert_numpy_array_equal(
                tslib.array_to_datetime(
                    np.array([dt_string], dtype=object)
                ),
                expected_output
            ) 
Example #8
Source File: test_array_to_datetime.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_parsing_valid_dates(self):
        arr = np.array(['01-01-2013', '01-02-2013'], dtype=object)
        result = tslib.array_to_datetime(arr)
        expected = ['2013-01-01T00:00:00.000000000-0000',
                    '2013-01-02T00:00:00.000000000-0000']
        tm.assert_numpy_array_equal(
            result,
            np_array_datetime64_compat(expected, dtype='M8[ns]'))

        arr = np.array(['Mon Sep 16 2013', 'Tue Sep 17 2013'], dtype=object)
        result = tslib.array_to_datetime(arr)
        expected = ['2013-09-16T00:00:00.000000000-0000',
                    '2013-09-17T00:00:00.000000000-0000']
        tm.assert_numpy_array_equal(
            result,
            np_array_datetime64_compat(expected, dtype='M8[ns]')) 
Example #9
Source File: test_array_to_datetime.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_coerce_of_invalid_datetimes(self):
        arr = np.array(['01-01-2013', 'not_a_date', '1'], dtype=object)

        # Without coercing, the presence of any invalid dates prevents
        # any values from being converted
        result = tslib.array_to_datetime(arr, errors='ignore')
        tm.assert_numpy_array_equal(result, arr)

        # With coercing, the invalid dates becomes iNaT
        result = tslib.array_to_datetime(arr, errors='coerce')
        expected = ['2013-01-01T00:00:00.000000000-0000',
                    tslib.iNaT,
                    tslib.iNaT]

        tm.assert_numpy_array_equal(
            result,
            np_array_datetime64_compat(expected, dtype='M8[ns]')) 
Example #10
Source File: test_array_to_datetime.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_coerce_outside_ns_bounds_one_valid():
    arr = np.array(["1/1/1000", "1/1/2000"], dtype=object)
    result, _ = tslib.array_to_datetime(arr, errors="coerce")

    expected = [iNaT, "2000-01-01T00:00:00.000000000-0000"]
    expected = np_array_datetime64_compat(expected, dtype="M8[ns]")

    tm.assert_numpy_array_equal(result, expected) 
Example #11
Source File: test_array_to_datetime.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_to_datetime_barely_out_of_bounds(self):
        # GH#19529
        # GH#19382 close enough to bounds that dropping nanos would result
        # in an in-bounds datetime
        arr = np.array(['2262-04-11 23:47:16.854775808'], dtype=object)
        with pytest.raises(tslib.OutOfBoundsDatetime):
            tslib.array_to_datetime(arr) 
Example #12
Source File: test_array_to_datetime.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_to_datetime_barely_out_of_bounds():
    # see gh-19382, gh-19529
    #
    # Close enough to bounds that dropping nanos
    # would result in an in-bounds datetime.
    arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object)
    msg = "Out of bounds nanosecond timestamp: 2262-04-11 23:47:16"

    with pytest.raises(tslib.OutOfBoundsDatetime, match=msg):
        tslib.array_to_datetime(arr) 
Example #13
Source File: test_tools.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_number_looking_strings_not_into_datetime(self):
        # #4601
        # These strings don't look like datetimes so they shouldn't be
        # attempted to be converted
        arr = np.array(['-352.737091', '183.575577'], dtype=object)
        tm.assert_numpy_array_equal(
            tslib.array_to_datetime(arr, errors='ignore'), arr)

        arr = np.array(['1', '2', '3', '4', '5'], dtype=object)
        tm.assert_numpy_array_equal(
            tslib.array_to_datetime(arr, errors='ignore'), arr) 
Example #14
Source File: test_tools.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_coercing_dates_outside_of_datetime64_ns_bounds(self):
        invalid_dates = [
            date(1000, 1, 1),
            datetime(1000, 1, 1),
            '1000-01-01',
            'Jan 1, 1000',
            np.datetime64('1000-01-01'),
        ]

        for invalid_date in invalid_dates:
            pytest.raises(ValueError,
                          tslib.array_to_datetime,
                          np.array([invalid_date], dtype='object'),
                          errors='raise', )
            tm.assert_numpy_array_equal(
                tslib.array_to_datetime(
                    np.array([invalid_date], dtype='object'),
                    errors='coerce'),
                np.array([tslib.iNaT], dtype='M8[ns]')
            )

        arr = np.array(['1/1/1000', '1/1/2000'], dtype=object)
        tm.assert_numpy_array_equal(
            tslib.array_to_datetime(arr, errors='coerce'),
            np_array_datetime64_compat(
                [
                    tslib.iNaT,
                    '2000-01-01T00:00:00.000000000-0000'
                ],
                dtype='M8[ns]'
            )
        ) 
Example #15
Source File: test_array_to_datetime.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_parsing_valid_dates(data, expected):
    arr = np.array(data, dtype=object)
    result, _ = tslib.array_to_datetime(arr)

    expected = np_array_datetime64_compat(expected, dtype="M8[ns]")
    tm.assert_numpy_array_equal(result, expected) 
Example #16
Source File: test_array_to_datetime.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_parsing_timezone_offsets(dt_string, expected_tz):
    # All of these datetime strings with offsets are equivalent
    # to the same datetime after the timezone offset is added.
    arr = np.array(["01-01-2013 00:00:00"], dtype=object)
    expected, _ = tslib.array_to_datetime(arr)

    arr = np.array([dt_string], dtype=object)
    result, result_tz = tslib.array_to_datetime(arr)

    tm.assert_numpy_array_equal(result, expected)
    assert result_tz is pytz.FixedOffset(expected_tz) 
Example #17
Source File: test_array_to_datetime.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_parsing_non_iso_timezone_offset():
    dt_string = "01-01-2013T00:00:00.000000000+0000"
    arr = np.array([dt_string], dtype=object)

    result, result_tz = tslib.array_to_datetime(arr)
    expected = np.array([np.datetime64("2013-01-01 00:00:00.000000000")])

    tm.assert_numpy_array_equal(result, expected)
    assert result_tz is pytz.FixedOffset(0) 
Example #18
Source File: test_array_to_datetime.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_parsing_different_timezone_offsets():
    # see gh-17697
    data = ["2015-11-18 15:30:00+05:30", "2015-11-18 15:30:00+06:30"]
    data = np.array(data, dtype=object)

    result, result_tz = tslib.array_to_datetime(data)
    expected = np.array([datetime(2015, 11, 18, 15, 30,
                                  tzinfo=tzoffset(None, 19800)),
                         datetime(2015, 11, 18, 15, 30,
                                  tzinfo=tzoffset(None, 23400))],
                        dtype=object)

    tm.assert_numpy_array_equal(result, expected)
    assert result_tz is None 
Example #19
Source File: test_array_to_datetime.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_coerce_outside_ns_bounds(invalid_date, errors):
    arr = np.array([invalid_date], dtype="object")
    kwargs = dict(values=arr, errors=errors)

    if errors == "raise":
        msg = "Out of bounds nanosecond timestamp"

        with pytest.raises(ValueError, match=msg):
            tslib.array_to_datetime(**kwargs)
    else:  # coerce.
        result, _ = tslib.array_to_datetime(**kwargs)
        expected = np.array([iNaT], dtype="M8[ns]")

        tm.assert_numpy_array_equal(result, expected) 
Example #20
Source File: test_array_to_datetime.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_coerce_outside_ns_bounds_one_valid():
    arr = np.array(["1/1/1000", "1/1/2000"], dtype=object)
    result, _ = tslib.array_to_datetime(arr, errors="coerce")

    expected = [iNaT, "2000-01-01T00:00:00.000000000-0000"]
    expected = np_array_datetime64_compat(expected, dtype="M8[ns]")

    tm.assert_numpy_array_equal(result, expected) 
Example #21
Source File: test_array_to_datetime.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_to_datetime_barely_out_of_bounds():
    # see gh-19382, gh-19529
    #
    # Close enough to bounds that dropping nanos
    # would result in an in-bounds datetime.
    arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object)
    msg = "Out of bounds nanosecond timestamp: 2262-04-11 23:47:16"

    with pytest.raises(tslib.OutOfBoundsDatetime, match=msg):
        tslib.array_to_datetime(arr) 
Example #22
Source File: test_array_to_datetime.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_parsing_timezone_offsets(self, dt_string):
        # All of these datetime strings with offsets are equivalent
        # to the same datetime after the timezone offset is added
        arr = np.array(['01-01-2013 00:00:00'], dtype=object)
        expected = tslib.array_to_datetime(arr)

        arr = np.array([dt_string], dtype=object)
        result = tslib.array_to_datetime(arr)
        tm.assert_numpy_array_equal(result, expected) 
Example #23
Source File: test_array_to_datetime.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_number_looking_strings_not_into_datetime(self):
        # GH#4601
        # These strings don't look like datetimes so they shouldn't be
        # attempted to be converted
        arr = np.array(['-352.737091', '183.575577'], dtype=object)
        result = tslib.array_to_datetime(arr, errors='ignore')
        tm.assert_numpy_array_equal(result, arr)

        arr = np.array(['1', '2', '3', '4', '5'], dtype=object)
        result = tslib.array_to_datetime(arr, errors='ignore')
        tm.assert_numpy_array_equal(result, arr) 
Example #24
Source File: test_array_to_datetime.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_coerce_outside_ns_bounds(self, invalid_date):
        arr = np.array([invalid_date], dtype='object')
        with pytest.raises(ValueError):
            tslib.array_to_datetime(arr, errors='raise')

        result = tslib.array_to_datetime(arr, errors='coerce')
        expected = np.array([tslib.iNaT], dtype='M8[ns]')
        tm.assert_numpy_array_equal(result, expected) 
Example #25
Source File: test_array_to_datetime.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_parsing_different_timezone_offsets():
    # see gh-17697
    data = ["2015-11-18 15:30:00+05:30", "2015-11-18 15:30:00+06:30"]
    data = np.array(data, dtype=object)

    result, result_tz = tslib.array_to_datetime(data)
    expected = np.array([datetime(2015, 11, 18, 15, 30,
                                  tzinfo=tzoffset(None, 19800)),
                         datetime(2015, 11, 18, 15, 30,
                                  tzinfo=tzoffset(None, 23400))],
                        dtype=object)

    tm.assert_numpy_array_equal(result, expected)
    assert result_tz is None 
Example #26
Source File: test_array_to_datetime.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_parsing_valid_dates(data, expected):
    arr = np.array(data, dtype=object)
    result, _ = tslib.array_to_datetime(arr)

    expected = np_array_datetime64_compat(expected, dtype="M8[ns]")
    tm.assert_numpy_array_equal(result, expected) 
Example #27
Source File: test_array_to_datetime.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_parsing_timezone_offsets(dt_string, expected_tz):
    # All of these datetime strings with offsets are equivalent
    # to the same datetime after the timezone offset is added.
    arr = np.array(["01-01-2013 00:00:00"], dtype=object)
    expected, _ = tslib.array_to_datetime(arr)

    arr = np.array([dt_string], dtype=object)
    result, result_tz = tslib.array_to_datetime(arr)

    tm.assert_numpy_array_equal(result, expected)
    assert result_tz is pytz.FixedOffset(expected_tz) 
Example #28
Source File: test_array_to_datetime.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_parsing_non_iso_timezone_offset():
    dt_string = "01-01-2013T00:00:00.000000000+0000"
    arr = np.array([dt_string], dtype=object)

    result, result_tz = tslib.array_to_datetime(arr)
    expected = np.array([np.datetime64("2013-01-01 00:00:00.000000000")])

    tm.assert_numpy_array_equal(result, expected)
    assert result_tz is pytz.FixedOffset(0) 
Example #29
Source File: test_array_to_datetime.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_parsing_different_timezone_offsets():
    # see gh-17697
    data = ["2015-11-18 15:30:00+05:30", "2015-11-18 15:30:00+06:30"]
    data = np.array(data, dtype=object)

    result, result_tz = tslib.array_to_datetime(data)
    expected = np.array([datetime(2015, 11, 18, 15, 30,
                                  tzinfo=tzoffset(None, 19800)),
                         datetime(2015, 11, 18, 15, 30,
                                  tzinfo=tzoffset(None, 23400))],
                        dtype=object)

    tm.assert_numpy_array_equal(result, expected)
    assert result_tz is None 
Example #30
Source File: test_array_to_datetime.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_coerce_outside_ns_bounds(invalid_date, errors):
    arr = np.array([invalid_date], dtype="object")
    kwargs = dict(values=arr, errors=errors)

    if errors == "raise":
        msg = "Out of bounds nanosecond timestamp"

        with pytest.raises(ValueError, match=msg):
            tslib.array_to_datetime(**kwargs)
    else:  # coerce.
        result, _ = tslib.array_to_datetime(**kwargs)
        expected = np.array([iNaT], dtype="M8[ns]")

        tm.assert_numpy_array_equal(result, expected)