From b76fe2ad20ec42a657c8c474feb7b7c544f335df Mon Sep 17 00:00:00 2001 From: Chang She Date: Mon, 26 Nov 2012 18:05:52 -0500 Subject: [PATCH] BUG: tools.parse_time_string does not parse intraday strings correctly #2306 --- pandas/tseries/index.py | 2 +- pandas/tseries/tests/test_timeseries.py | 4 ++++ pandas/tseries/tools.py | 9 ++------- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/pandas/tseries/index.py b/pandas/tseries/index.py index f8c0dd760946a..6cbfbfa459308 100644 --- a/pandas/tseries/index.py +++ b/pandas/tseries/index.py @@ -1539,7 +1539,7 @@ def _to_m8(key): def _str_to_dt_array(arr, offset=None, dayfirst=None, yearfirst=None): def parser(x): result = parse_time_string(x, offset, dayfirst=dayfirst, - yearfirst=None) + yearfirst=yearfirst) return result[0] arr = np.asarray(arr, dtype=object) diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index d1b559c4f63a5..ef35c44b53772 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -246,6 +246,10 @@ def _check_rng(rng): _check_rng(rng_eastern) _check_rng(rng_utc) + def test_ctor_str_intraday(self): + rng = DatetimeIndex(['1-1-2000 00:00:01']) + self.assert_(rng[0].second == 1) + def test_series_ctor_plus_datetimeindex(self): rng = date_range('20090415', '20090519', freq='B') data = dict((k, 1) for k in rng) diff --git a/pandas/tseries/tools.py b/pandas/tseries/tools.py index 9e1c451c42887..befe3444d98bd 100644 --- a/pandas/tseries/tools.py +++ b/pandas/tseries/tools.py @@ -235,18 +235,13 @@ def parse_time_string(arg, freq=None, dayfirst=None, yearfirst=None): repl = {} reso = 'year' - stopped = False + for attr in ["year", "month", "day", "hour", "minute", "second", "microsecond"]: - can_be_zero = ['hour', 'minute', 'second', 'microsecond'] value = getattr(parsed, attr) if value is not None and value != 0: # or attr in can_be_zero): repl[attr] = value - if not stopped: - reso = attr - else: - stopped = True - break + reso = attr ret = default.replace(**repl) return ret, parsed, reso # datetime, resolution