Skip to content

Commit

Permalink
PERF: add shortcut to Timestamp constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKirko committed Jan 4, 2020
1 parent 50ae37d commit 0a9c79e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ Performance improvements
The improvement is not present if checking if the :class:`Categorical` is less than or less than or equal than the scalar (:issue:`29820`)
- Performance improvement in :meth:`Index.equals` and :meth:`MultiIndex.equals` (:issue:`29134`)
- Performance improvement in :func:`~pandas.api.types.infer_dtype` when ``skipna`` is ``True`` (:issue:`28814`)
- Performance improvement in :class:`Timestamp` constructor (:issue:`30543`)

.. _whatsnew_1000.bug_fixes:

Expand Down
5 changes: 4 additions & 1 deletion pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,10 @@ class Timestamp(_Timestamp):
# User passed tzinfo instead of tz; avoid silently ignoring
tz, tzinfo = tzinfo, None

if isinstance(ts_input, str):
if isinstance(ts_input, Timestamp) and tz is None:
# GH 30543 if pd.Timestamp already passed, return it
return ts_input
elif isinstance(ts_input, str):
# User passed a date string to parse.
# Check that the user didn't also pass a date attribute kwarg.
if any(arg is not None for arg in _date_attributes):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2313,3 +2313,10 @@ def test_nullable_integer_to_datetime():
tm.assert_series_equal(res, expected)
# Check that ser isn't mutated
tm.assert_series_equal(ser, ser_copy)


def test_timestamp_constructor_identity():
# Test for #30543
expected = pd.Timestamp("2017-01-01T12")
result = pd.Timestamp(expected)
assert result is expected

0 comments on commit 0a9c79e

Please sign in to comment.