Skip to content

Commit ad19057

Browse files
authored
BUG: Timestamp(2020,1, 1, tzinfo=foo) GH#31929 (#45308)
1 parent 4f99c32 commit ad19057

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Categorical
119119
Datetimelike
120120
^^^^^^^^^^^^
121121
- Bug in :func:`to_datetime` with sequences of ``np.str_`` objects incorrectly raising (:issue:`32264`)
122+
- Bug in :class:`Timestamp` construction when passing datetime components as positional arguments and ``tzinfo`` as a keyword argument incorrectly raising (:issue:`31929`)
122123
-
123124

124125
Timedelta

pandas/_libs/tslibs/timestamps.pyx

+21
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,27 @@ class Timestamp(_Timestamp):
12951295
# GH#17690 tzinfo must be a datetime.tzinfo object, ensured
12961296
# by the cython annotation.
12971297
if tz is not None:
1298+
if (is_integer_object(tz)
1299+
and is_integer_object(ts_input)
1300+
and is_integer_object(freq)
1301+
):
1302+
# GH#31929 e.g. Timestamp(2019, 3, 4, 5, 6, tzinfo=foo)
1303+
# TODO(GH#45307): this will still be fragile to
1304+
# mixed-and-matched positional/keyword arguments
1305+
ts_input = datetime(
1306+
ts_input,
1307+
freq,
1308+
tz,
1309+
unit or 0,
1310+
year or 0,
1311+
month or 0,
1312+
day or 0,
1313+
fold=fold or 0,
1314+
)
1315+
nanosecond = hour
1316+
tz = tzinfo
1317+
return cls(ts_input, nanosecond=nanosecond, tz=tz)
1318+
12981319
raise ValueError('Can provide at most one of tz, tzinfo')
12991320

13001321
# User passed tzinfo instead of tz; avoid silently ignoring

pandas/tests/scalar/timestamp/test_constructors.py

+20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import (
33
datetime,
44
timedelta,
5+
timezone,
56
)
67

78
import dateutil.tz
@@ -242,6 +243,25 @@ def test_constructor_tz_or_tzinfo(self):
242243
]
243244
assert all(ts == stamps[0] for ts in stamps)
244245

246+
def test_constructor_positional_with_tzinfo(self):
247+
# GH#31929
248+
ts = Timestamp(2020, 12, 31, tzinfo=timezone.utc)
249+
expected = Timestamp("2020-12-31", tzinfo=timezone.utc)
250+
assert ts == expected
251+
252+
@pytest.mark.xfail(reason="GH#45307")
253+
@pytest.mark.parametrize("kwd", ["nanosecond", "microsecond", "second", "minute"])
254+
def test_constructor_positional_keyword_mixed_with_tzinfo(self, kwd):
255+
# TODO: if we passed microsecond with a keyword we would mess up
256+
# xref GH#45307
257+
kwargs = {kwd: 4}
258+
ts = Timestamp(2020, 12, 31, tzinfo=timezone.utc, **kwargs)
259+
260+
td_kwargs = {kwd + "s": 4}
261+
td = Timedelta(**td_kwargs)
262+
expected = Timestamp("2020-12-31", tz=timezone.utc) + td
263+
assert ts == expected
264+
245265
def test_constructor_positional(self):
246266
# see gh-10758
247267
msg = (

0 commit comments

Comments
 (0)