Skip to content

Commit 87fb09e

Browse files
committed
PERF: add shortcut to Timestamp constructor
1 parent bc9d329 commit 87fb09e

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v1.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,7 @@ Performance improvements
905905
The improvement is not present if checking if the :class:`Categorical` is less than or less than or equal than the scalar (:issue:`29820`)
906906
- Performance improvement in :meth:`Index.equals` and :meth:`MultiIndex.equals` (:issue:`29134`)
907907
- Performance improvement in :func:`~pandas.api.types.infer_dtype` when ``skipna`` is ``True`` (:issue:`28814`)
908+
- Performance improvement in :class:`Timestamp` constructor (:issue:`30543`)
908909

909910
.. ---------------------------------------------------------------------------
910911

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,10 @@ class Timestamp(_Timestamp):
391391
# User passed tzinfo instead of tz; avoid silently ignoring
392392
tz, tzinfo = tzinfo, None
393393

394-
if isinstance(ts_input, str):
394+
if isinstance(ts_input, Timestamp) and tz is None:
395+
# GH 30543 if pd.Timestamp already passed, return it
396+
return ts_input
397+
elif isinstance(ts_input, str):
395398
# User passed a date string to parse.
396399
# Check that the user didn't also pass a date attribute kwarg.
397400
if any(arg is not None for arg in _date_attributes):

pandas/tests/indexes/datetimes/test_tools.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,3 +2313,10 @@ def test_nullable_integer_to_datetime():
23132313
tm.assert_series_equal(res, expected)
23142314
# Check that ser isn't mutated
23152315
tm.assert_series_equal(ser, ser_copy)
2316+
2317+
2318+
def test_timestamp_constructor_identity():
2319+
# Test for #30543
2320+
expected = pd.Timestamp("2017-01-01T12")
2321+
result = pd.Timestamp(expected)
2322+
assert result is expected

0 commit comments

Comments
 (0)