|
| 1 | +import unittest |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from pandas import tslib |
| 6 | +from datetime import datetime |
| 7 | + |
| 8 | +class TestDatetimeParsingWrappers(unittest.TestCase): |
| 9 | + def test_verify_datetime_bounds(self): |
| 10 | + for year in (1, 1000, 1677, 2262, 5000): |
| 11 | + dt = datetime(year, 1, 1) |
| 12 | + self.assertRaises( |
| 13 | + ValueError, |
| 14 | + tslib.verify_datetime_bounds, |
| 15 | + dt |
| 16 | + ) |
| 17 | + |
| 18 | + for year in (1678, 2000, 2261): |
| 19 | + tslib.verify_datetime_bounds(datetime(year, 1, 1)) |
| 20 | + |
| 21 | + def test_does_not_convert_mixed_integer(self): |
| 22 | + bad_date_strings = ( |
| 23 | + '-50000', |
| 24 | + '999', |
| 25 | + '123.1234', |
| 26 | + 'm', |
| 27 | + 'T' |
| 28 | + ) |
| 29 | + |
| 30 | + for bad_date_string in bad_date_strings: |
| 31 | + self.assertFalse( |
| 32 | + tslib._does_string_look_like_datetime(bad_date_string) |
| 33 | + ) |
| 34 | + |
| 35 | + good_date_strings = ( |
| 36 | + '2012-01-01', |
| 37 | + '01/01/2012', |
| 38 | + 'Mon Sep 16, 2013', |
| 39 | + '01012012', |
| 40 | + '0101', |
| 41 | + '1-1', |
| 42 | + ) |
| 43 | + |
| 44 | + for good_date_string in good_date_strings: |
| 45 | + self.assertTrue( |
| 46 | + tslib._does_string_look_like_datetime(good_date_string) |
| 47 | + ) |
| 48 | + |
| 49 | +class TestArrayToDatetime(unittest.TestCase): |
| 50 | + def test_parsing_valid_dates(self): |
| 51 | + arr = np.array(['01-01-2013', '01-02-2013'], dtype=object) |
| 52 | + self.assert_( |
| 53 | + np.array_equal( |
| 54 | + tslib.array_to_datetime(arr), |
| 55 | + np.array( |
| 56 | + [ |
| 57 | + '2013-01-01T00:00:00.000000000-0000', |
| 58 | + '2013-01-02T00:00:00.000000000-0000' |
| 59 | + ], |
| 60 | + dtype='M8[ns]' |
| 61 | + ) |
| 62 | + ) |
| 63 | + ) |
| 64 | + |
| 65 | + arr = np.array(['Mon Sep 16 2013', 'Tue Sep 17 2013'], dtype=object) |
| 66 | + self.assert_( |
| 67 | + np.array_equal( |
| 68 | + tslib.array_to_datetime(arr), |
| 69 | + np.array( |
| 70 | + [ |
| 71 | + '2013-09-16T00:00:00.000000000-0000', |
| 72 | + '2013-09-17T00:00:00.000000000-0000' |
| 73 | + ], |
| 74 | + dtype='M8[ns]' |
| 75 | + ) |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | + def test_number_looking_strings_not_into_datetime(self): |
| 80 | + # #4601 |
| 81 | + # These strings don't look like datetimes so they shouldn't be |
| 82 | + # attempted to be converted |
| 83 | + arr = np.array(['-352.737091', '183.575577'], dtype=object) |
| 84 | + self.assert_(np.array_equal(tslib.array_to_datetime(arr), arr)) |
| 85 | + |
| 86 | + arr = np.array(['1', '2', '3', '4', '5'], dtype=object) |
| 87 | + self.assert_(np.array_equal(tslib.array_to_datetime(arr), arr)) |
| 88 | + |
| 89 | + def test_dates_outside_of_datetime64_ns_bounds(self): |
| 90 | + # These datetimes are outside of the bounds of the |
| 91 | + # datetime64[ns] bounds, so they cannot be converted to |
| 92 | + # datetimes |
| 93 | + arr = np.array(['1/1/1676', '1/2/1676'], dtype=object) |
| 94 | + self.assert_(np.array_equal(tslib.array_to_datetime(arr), arr)) |
| 95 | + |
| 96 | + arr = np.array(['1/1/2263', '1/2/2263'], dtype=object) |
| 97 | + self.assert_(np.array_equal(tslib.array_to_datetime(arr), arr)) |
| 98 | + |
| 99 | + def test_coerce_of_invalid_datetimes(self): |
| 100 | + arr = np.array(['01-01-2013', 'not_a_date', '1'], dtype=object) |
| 101 | + |
| 102 | + # Without coercing, the presence of any invalid dates prevents |
| 103 | + # any values from being converted |
| 104 | + self.assert_(np.array_equal(tslib.array_to_datetime(arr), arr)) |
| 105 | + |
| 106 | + # With coercing, the invalid dates becomes iNaT |
| 107 | + self.assert_( |
| 108 | + np.array_equal( |
| 109 | + tslib.array_to_datetime(arr, coerce=True), |
| 110 | + np.array( |
| 111 | + [ |
| 112 | + '2013-01-01T00:00:00.000000000-0000', |
| 113 | + tslib.iNaT, |
| 114 | + tslib.iNaT |
| 115 | + ], |
| 116 | + dtype='M8[ns]' |
| 117 | + ) |
| 118 | + ) |
| 119 | + ) |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], |
| 123 | + exit=False) |
0 commit comments