Skip to content

Commit 7672459

Browse files
authored
BUG: Timestamp unit with time and not date (#54097)
* BUG: Timestamp unit with time and not date * GH ref
1 parent ea0a08f commit 7672459

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

doc/source/whatsnew/v2.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ Datetimelike
393393
- Bug in :meth:`Timestamp.round` with values close to the implementation bounds returning incorrect results instead of raising ``OutOfBoundsDatetime`` (:issue:`51494`)
394394
- Bug in :meth:`arrays.DatetimeArray.map` and :meth:`DatetimeIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`)
395395
- 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`)
396+
- Bug in constructing a :class:`Timestamp` from a string representing a time without a date inferring an incorrect unit (:issue:`54097`)
396397
- Bug in parsing datetime strings with weekday but no day e.g. "2023 Sept Thu" incorrectly raising ``AttributeError`` instead of ``ValueError`` (:issue:`52659`)
397398

398399
Timedelta

pandas/_libs/tslibs/parsing.pyx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ cnp.import_array()
4242

4343
from decimal import InvalidOperation
4444

45-
from dateutil.parser import (
46-
DEFAULTPARSER,
47-
parse as du_parse,
48-
)
45+
from dateutil.parser import DEFAULTPARSER
4946
from dateutil.tz import (
5047
tzlocal as _dateutil_tzlocal,
5148
tzoffset,
@@ -309,9 +306,12 @@ cdef datetime parse_datetime_string(
309306
raise ValueError(f'Given date string "{date_string}" not likely a datetime')
310307

311308
if _does_string_look_like_time(date_string):
309+
# time without date e.g. "01:01:01.111"
312310
# use current datetime as default, not pass _DEFAULT_DATETIME
313-
dt = du_parse(date_string, dayfirst=dayfirst,
314-
yearfirst=yearfirst)
311+
default = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
312+
dt = dateutil_parse(date_string, default=default,
313+
dayfirst=dayfirst, yearfirst=yearfirst,
314+
ignoretz=False, out_bestunit=out_bestunit)
315315
return dt
316316

317317
dt = _parse_delimited_date(date_string, dayfirst, out_bestunit)

pandas/tests/scalar/timestamp/test_constructors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525

2626

2727
class TestTimestampConstructors:
28+
def test_construct_from_time_unit(self):
29+
# GH#54097 only passing a time component, no date
30+
ts = Timestamp("01:01:01.111")
31+
assert ts.unit == "ms"
32+
2833
def test_weekday_but_no_day_raises(self):
2934
# GH#52659
3035
msg = "Parsing datetimes with weekday but no day information is not supported"

0 commit comments

Comments
 (0)