Skip to content

Commit 35df212

Browse files
AlexKirkojreback
authored andcommitted
PERF: add shortcut to Timestamp constructor (#30676)
1 parent b54aaf7 commit 35df212

File tree

5 files changed

+41
-12
lines changed

5 files changed

+41
-12
lines changed

asv_bench/benchmarks/tslibs/timestamp.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import datetime
22

33
import dateutil
4+
import numpy as np
45
import pytz
56

67
from pandas import Timestamp
78

89

910
class TimestampConstruction:
11+
def setup(self):
12+
self.npdatetime64 = np.datetime64("2020-01-01 00:00:00")
13+
self.dttime_unaware = datetime.datetime(2020, 1, 1, 0, 0, 0)
14+
self.dttime_aware = datetime.datetime(2020, 1, 1, 0, 0, 0, 0, pytz.UTC)
15+
self.ts = Timestamp("2020-01-01 00:00:00")
16+
1017
def time_parse_iso8601_no_tz(self):
1118
Timestamp("2017-08-25 08:16:14")
1219

@@ -28,6 +35,18 @@ def time_fromordinal(self):
2835
def time_fromtimestamp(self):
2936
Timestamp.fromtimestamp(1515448538)
3037

38+
def time_from_npdatetime64(self):
39+
Timestamp(self.npdatetime64)
40+
41+
def time_from_datetime_unaware(self):
42+
Timestamp(self.dttime_unaware)
43+
44+
def time_from_datetime_aware(self):
45+
Timestamp(self.dttime_aware)
46+
47+
def time_from_pd_timestamp(self):
48+
Timestamp(self.ts)
49+
3150

3251
class TimestampProperties:
3352
_tzs = [None, pytz.timezone("Europe/Amsterdam"), pytz.UTC, dateutil.tz.tzutc()]

doc/source/whatsnew/v1.1.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ Deprecations
7979

8080
Performance improvements
8181
~~~~~~~~~~~~~~~~~~~~~~~~
82+
8283
- Performance improvement in :class:`Timedelta` constructor (:issue:`30543`)
84+
- Performance improvement in :class:`Timestamp` constructor (:issue:`30543`)
8385
-
8486
-
8587

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,18 @@ 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+
# GH 30543 if pd.Timestamp already passed, return it
395+
# check that only ts_input is passed
396+
# checking verbosely, because cython doesn't optimize
397+
# list comprehensions (as of cython 0.29.x)
398+
if (isinstance(ts_input, Timestamp) and freq is None and
399+
tz is None and unit is None and year is None and
400+
month is None and day is None and hour is None and
401+
minute is None and second is None and
402+
microsecond is None and nanosecond is None and
403+
tzinfo is None):
404+
return ts_input
405+
elif isinstance(ts_input, str):
395406
# User passed a date string to parse.
396407
# Check that the user didn't also pass a date attribute kwarg.
397408
if any(arg is not None for arg in _date_attributes):

pandas/tests/indexes/datetimes/test_constructors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,3 +957,10 @@ def test_timedelta_constructor_identity():
957957
expected = pd.Timedelta(np.timedelta64(1, "s"))
958958
result = pd.Timedelta(expected)
959959
assert result is expected
960+
961+
962+
def test_timestamp_constructor_identity():
963+
# Test for #30543
964+
expected = pd.Timestamp("2017-01-01T12")
965+
result = pd.Timestamp(expected)
966+
assert result is expected

pandas/tests/indexes/datetimes/test_timezones.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Tests for DatetimeIndex timezone-related methods
33
"""
44
from datetime import date, datetime, time, timedelta, tzinfo
5-
from distutils.version import LooseVersion
65

76
import dateutil
87
from dateutil.tz import gettz, tzlocal
@@ -11,7 +10,6 @@
1110
import pytz
1211

1312
from pandas._libs.tslibs import conversion, timezones
14-
from pandas.compat._optional import _get_version
1513
import pandas.util._test_decorators as td
1614

1715
import pandas as pd
@@ -583,15 +581,7 @@ def test_dti_construction_ambiguous_endpoint(self, tz):
583581
["US/Pacific", "shift_forward", "2019-03-10 03:00"],
584582
["dateutil/US/Pacific", "shift_forward", "2019-03-10 03:00"],
585583
["US/Pacific", "shift_backward", "2019-03-10 01:00"],
586-
pytest.param(
587-
"dateutil/US/Pacific",
588-
"shift_backward",
589-
"2019-03-10 01:00",
590-
marks=pytest.mark.xfail(
591-
LooseVersion(_get_version(dateutil)) < LooseVersion("2.7.0"),
592-
reason="GH 31043",
593-
),
594-
),
584+
["dateutil/US/Pacific", "shift_backward", "2019-03-10 01:00"],
595585
["US/Pacific", timedelta(hours=1), "2019-03-10 03:00"],
596586
],
597587
)

0 commit comments

Comments
 (0)