diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 7450fc6fdc1da..57b2bf910e2e5 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -393,6 +393,7 @@ Datetimelike - Bug in :meth:`Timestamp.round` with values close to the implementation bounds returning incorrect results instead of raising ``OutOfBoundsDatetime`` (:issue:`51494`) - Bug in :meth:`arrays.DatetimeArray.map` and :meth:`DatetimeIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`) - Bug in constructing a :class:`Series` or :class:`DataFrame` from a datetime or timedelta scalar always inferring nanosecond resolution instead of inferring from the input (:issue:`52212`) +- Bug in constructing a :class:`Timestamp` from a string representing a time without a date inferring an incorrect unit (:issue:`54097`) - Bug in parsing datetime strings with weekday but no day e.g. "2023 Sept Thu" incorrectly raising ``AttributeError`` instead of ``ValueError`` (:issue:`52659`) Timedelta diff --git a/pandas/_libs/tslibs/parsing.pyx b/pandas/_libs/tslibs/parsing.pyx index 9173b7e8b1449..3643c840a50a6 100644 --- a/pandas/_libs/tslibs/parsing.pyx +++ b/pandas/_libs/tslibs/parsing.pyx @@ -42,10 +42,7 @@ cnp.import_array() from decimal import InvalidOperation -from dateutil.parser import ( - DEFAULTPARSER, - parse as du_parse, -) +from dateutil.parser import DEFAULTPARSER from dateutil.tz import ( tzlocal as _dateutil_tzlocal, tzoffset, @@ -309,9 +306,12 @@ cdef datetime parse_datetime_string( raise ValueError(f'Given date string "{date_string}" not likely a datetime') if _does_string_look_like_time(date_string): + # time without date e.g. "01:01:01.111" # use current datetime as default, not pass _DEFAULT_DATETIME - dt = du_parse(date_string, dayfirst=dayfirst, - yearfirst=yearfirst) + default = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) + dt = dateutil_parse(date_string, default=default, + dayfirst=dayfirst, yearfirst=yearfirst, + ignoretz=False, out_bestunit=out_bestunit) return dt dt = _parse_delimited_date(date_string, dayfirst, out_bestunit) diff --git a/pandas/tests/scalar/timestamp/test_constructors.py b/pandas/tests/scalar/timestamp/test_constructors.py index 5fca577ff28d1..198b6feea5f4e 100644 --- a/pandas/tests/scalar/timestamp/test_constructors.py +++ b/pandas/tests/scalar/timestamp/test_constructors.py @@ -25,6 +25,11 @@ class TestTimestampConstructors: + def test_construct_from_time_unit(self): + # GH#54097 only passing a time component, no date + ts = Timestamp("01:01:01.111") + assert ts.unit == "ms" + def test_weekday_but_no_day_raises(self): # GH#52659 msg = "Parsing datetimes with weekday but no day information is not supported"