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", datetime.time(14, 15), ] expected = datetime.time(14, 15) for time_string in strings: self.assertEqual(tools.to_time(time_string), expected) new_string = "14.15" self.assertRaises(ValueError, tools.to_time, new_string) self.assertEqual(tools.to_time(new_string, format="%H.%M"), expected) arg = ["14:15", "20:20"] expected_arr = [datetime.time(14, 15), datetime.time(20, 20)] self.assertEqual(tools.to_time(arg), expected_arr) self.assertEqual(tools.to_time(arg, format="%H:%M"), expected_arr) self.assertEqual(tools.to_time(arg, infer_time_format=True), expected_arr) self.assertEqual(tools.to_time(arg, format="%I:%M%p", errors="coerce"), [None, None]) self.assert_numpy_array_equal(tools.to_time(arg, format="%I:%M%p", errors="ignore"), np.array(arg)) self.assertRaises(ValueError, lambda: tools.to_time(arg, format="%I:%M%p", errors="raise")) self.assert_series_equal(tools.to_time(Series(arg, name="test")), Series(expected_arr, name="test")) self.assert_numpy_array_equal(tools.to_time(np.array(arg)), np.array(expected_arr))
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", datetime.time(14, 15) ] expected = datetime.time(14, 15) for time_string in strings: self.assertEqual(tools.to_time(time_string), expected) new_string = "14.15" self.assertRaises(ValueError, tools.to_time, new_string) self.assertEqual(tools.to_time(new_string, format="%H.%M"), expected) arg = ["14:15", "20:20"] expected_arr = [datetime.time(14, 15), datetime.time(20, 20)] self.assertEqual(tools.to_time(arg), expected_arr) self.assertEqual(tools.to_time(arg, format="%H:%M"), expected_arr) self.assertEqual(tools.to_time(arg, infer_time_format=True), expected_arr) self.assertEqual(tools.to_time(arg, format="%I:%M%p", errors="coerce"), [None, None]) self.assert_numpy_array_equal( tools.to_time(arg, format="%I:%M%p", errors="ignore"), np.array(arg)) self.assertRaises( ValueError, lambda: tools.to_time(arg, format="%I:%M%p", errors="raise")) self.assert_series_equal(tools.to_time(Series(arg, name="test")), Series(expected_arr, name="test")) self.assert_numpy_array_equal(tools.to_time(np.array(arg)), np.array(expected_arr))
def test_guess_datetime_format_with_locale_specific_formats( self, string, format): # The month names will vary depending on the locale, in which # case these wont be parsed properly (dateutil can't parse them) tm._skip_if_has_locale() result = parsing._guess_datetime_format(string) assert result == format
def test_guess_datetime_format_with_locale_specific_formats(self): # The month names will vary depending on the locale, in which # case these wont be parsed properly (dateutil can't parse them) tm._skip_if_has_locale() dt_string_to_format = (('30/Dec/2011', '%d/%b/%Y'), ('30/December/2011', '%d/%B/%Y'), ('30/Dec/2011 00:00:00', '%d/%b/%Y %H:%M:%S'), ) for dt_string, dt_format in dt_string_to_format: assert parsing._guess_datetime_format(dt_string) == dt_format
def test_between_time_formats(self): # GH11818 _skip_if_has_locale() rng = date_range('1/1/2000', '1/5/2000', freq='5min') ts = DataFrame(np.random.randn(len(rng), 2), index=rng) strings = [("2:00", "2:30"), ("0200", "0230"), ("2:00am", "2:30am"), ("0200am", "0230am"), ("2:00:00", "2:30:00"), ("020000", "023000"), ("2:00:00am", "2:30:00am"), ("020000am", "023000am")] expected_length = 28 for time_string in strings: assert len(ts.between_time(*time_string)) == expected_length
def test_to_datetime_with_non_exact(self): # GH 10834 tm._skip_if_has_locale() # 8904 # exact kw if sys.version_info < (2, 7): raise nose.SkipTest('on python version < 2.7') s = Series(['19MAY11', 'foobar19MAY11', '19MAY11:00:00:00', '19MAY11 00:00:00Z']) result = to_datetime(s, format='%d%b%y', exact=False) expected = to_datetime(s.str.extract(r'(\d+\w+\d+)', expand=False), format='%d%b%y') assert_series_equal(result, expected)
def test_to_datetime_with_apply(self): # this is only locale tested with US/None locales tm._skip_if_has_locale() # GH 5195 # with a format and coerce a single item to_datetime fails td = Series(['May 04', 'Jun 02', 'Dec 11'], index=[1, 2, 3]) expected = pd.to_datetime(td, format='%b %y') result = td.apply(pd.to_datetime, format='%b %y') assert_series_equal(result, expected) td = pd.Series(['May 04', 'Jun 02', ''], index=[1, 2, 3]) self.assertRaises(ValueError, lambda: pd.to_datetime(td, format='%b %y', errors='raise')) self.assertRaises(ValueError, lambda: td.apply(pd.to_datetime, format='%b %y', errors='raise')) expected = pd.to_datetime(td, format='%b %y', errors='coerce') result = td.apply( lambda x: pd.to_datetime(x, format='%b %y', errors='coerce')) assert_series_equal(result, expected)