Skip to content

BUG: Timestamp unit with time and not date #54097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/scalar/timestamp/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down