From 4198fc34dd839411f7194d745a5774dd443c8cbc Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 12 Jul 2023 14:04:33 -0700 Subject: [PATCH 1/2] BUG: Timestamp unit with time and not date --- doc/source/whatsnew/v2.1.0.rst | 1 + pandas/_libs/tslibs/parsing.pyx | 12 ++++++------ pandas/tests/scalar/timestamp/test_constructors.py | 5 +++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 7450fc6fdc1da..094414b85f380 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -394,6 +394,7 @@ Datetimelike - 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 parsing datetime strings with weekday but no day e.g. "2023 Sept Thu" incorrectly raising ``AttributeError`` instead of ``ValueError`` (:issue:`52659`) +- Bug in constructing a :class:`Timestamp` from a string representing a time without a date inferring an incorrect unit (:issue:`??`) 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..70c2ba99b0751 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): + # 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" From 2069a7265e36112858fc149c9b74fc819c756696 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 12 Jul 2023 14:05:40 -0700 Subject: [PATCH 2/2] GH ref --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/tests/scalar/timestamp/test_constructors.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 094414b85f380..57b2bf910e2e5 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -393,8 +393,8 @@ 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`) -- Bug in constructing a :class:`Timestamp` from a string representing a time without a date inferring an incorrect unit (:issue:`??`) Timedelta ^^^^^^^^^ diff --git a/pandas/tests/scalar/timestamp/test_constructors.py b/pandas/tests/scalar/timestamp/test_constructors.py index 70c2ba99b0751..198b6feea5f4e 100644 --- a/pandas/tests/scalar/timestamp/test_constructors.py +++ b/pandas/tests/scalar/timestamp/test_constructors.py @@ -26,7 +26,7 @@ class TestTimestampConstructors: def test_construct_from_time_unit(self): - # only passing a time component, no date + # GH#54097 only passing a time component, no date ts = Timestamp("01:01:01.111") assert ts.unit == "ms"