Skip to content

Commit

Permalink
BUG: Timestamp(2020,1, 1, tzinfo=foo) GH#31929 (#45308)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Jan 12, 2022
1 parent 4f99c32 commit ad19057
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Expand Up @@ -119,6 +119,7 @@ Categorical
Datetimelike
^^^^^^^^^^^^
- Bug in :func:`to_datetime` with sequences of ``np.str_`` objects incorrectly raising (:issue:`32264`)
- Bug in :class:`Timestamp` construction when passing datetime components as positional arguments and ``tzinfo`` as a keyword argument incorrectly raising (:issue:`31929`)
-

Timedelta
Expand Down
21 changes: 21 additions & 0 deletions pandas/_libs/tslibs/timestamps.pyx
Expand Up @@ -1295,6 +1295,27 @@ class Timestamp(_Timestamp):
# GH#17690 tzinfo must be a datetime.tzinfo object, ensured
# by the cython annotation.
if tz is not None:
if (is_integer_object(tz)
and is_integer_object(ts_input)
and is_integer_object(freq)
):
# GH#31929 e.g. Timestamp(2019, 3, 4, 5, 6, tzinfo=foo)
# TODO(GH#45307): this will still be fragile to
# mixed-and-matched positional/keyword arguments
ts_input = datetime(
ts_input,
freq,
tz,
unit or 0,
year or 0,
month or 0,
day or 0,
fold=fold or 0,
)
nanosecond = hour
tz = tzinfo
return cls(ts_input, nanosecond=nanosecond, tz=tz)

raise ValueError('Can provide at most one of tz, tzinfo')

# User passed tzinfo instead of tz; avoid silently ignoring
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/scalar/timestamp/test_constructors.py
Expand Up @@ -2,6 +2,7 @@
from datetime import (
datetime,
timedelta,
timezone,
)

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

def test_constructor_positional_with_tzinfo(self):
# GH#31929
ts = Timestamp(2020, 12, 31, tzinfo=timezone.utc)
expected = Timestamp("2020-12-31", tzinfo=timezone.utc)
assert ts == expected

@pytest.mark.xfail(reason="GH#45307")
@pytest.mark.parametrize("kwd", ["nanosecond", "microsecond", "second", "minute"])
def test_constructor_positional_keyword_mixed_with_tzinfo(self, kwd):
# TODO: if we passed microsecond with a keyword we would mess up
# xref GH#45307
kwargs = {kwd: 4}
ts = Timestamp(2020, 12, 31, tzinfo=timezone.utc, **kwargs)

td_kwargs = {kwd + "s": 4}
td = Timedelta(**td_kwargs)
expected = Timestamp("2020-12-31", tz=timezone.utc) + td
assert ts == expected

def test_constructor_positional(self):
# see gh-10758
msg = (
Expand Down

0 comments on commit ad19057

Please sign in to comment.