Skip to content

Commit 8bba1d4

Browse files
authored
BUG: Timestamp ignoring explicit tz=None (#58226)
* BUG: Timestamp ignoring explicit tz=None * Fix lint error * Fix new edge case * Simplify
1 parent e1752c8 commit 8bba1d4

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ Categorical
359359

360360
Datetimelike
361361
^^^^^^^^^^^^
362+
- Bug in :class:`Timestamp` constructor failing to raise when ``tz=None`` is explicitly specified in conjunction with timezone-aware ``tzinfo`` or data (:issue:`48688`)
362363
- Bug in :func:`date_range` where the last valid timestamp would sometimes not be produced (:issue:`56134`)
363364
- Bug in :func:`date_range` where using a negative frequency value would not include all points between the start and end values (:issue:`56382`)
364365
-

pandas/_libs/tslibs/timestamps.pyx

+10-1
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,7 @@ class Timestamp(_Timestamp):
17511751
tzinfo_type tzinfo=None,
17521752
*,
17531753
nanosecond=None,
1754-
tz=None,
1754+
tz=_no_input,
17551755
unit=None,
17561756
fold=None,
17571757
):
@@ -1783,6 +1783,10 @@ class Timestamp(_Timestamp):
17831783
_date_attributes = [year, month, day, hour, minute, second,
17841784
microsecond, nanosecond]
17851785

1786+
explicit_tz_none = tz is None
1787+
if tz is _no_input:
1788+
tz = None
1789+
17861790
if tzinfo is not None:
17871791
# GH#17690 tzinfo must be a datetime.tzinfo object, ensured
17881792
# by the cython annotation.
@@ -1883,6 +1887,11 @@ class Timestamp(_Timestamp):
18831887
if ts.value == NPY_NAT:
18841888
return NaT
18851889

1890+
if ts.tzinfo is not None and explicit_tz_none:
1891+
raise ValueError(
1892+
"Passed data is timezone-aware, incompatible with 'tz=None'."
1893+
)
1894+
18861895
return create_timestamp_from_ts(ts.value, ts.dts, ts.tzinfo, ts.fold, ts.creso)
18871896

18881897
def _round(self, freq, mode, ambiguous="raise", nonexistent="raise"):

pandas/tests/indexing/test_at.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ def test_at_datetime_index(self, row):
136136
class TestAtSetItemWithExpansion:
137137
def test_at_setitem_expansion_series_dt64tz_value(self, tz_naive_fixture):
138138
# GH#25506
139-
ts = Timestamp("2017-08-05 00:00:00+0100", tz=tz_naive_fixture)
139+
ts = (
140+
Timestamp("2017-08-05 00:00:00+0100", tz=tz_naive_fixture)
141+
if tz_naive_fixture is not None
142+
else Timestamp("2017-08-05 00:00:00+0100")
143+
)
140144
result = Series(ts)
141145
result.at[1] = ts
142146
expected = Series([ts, ts])

pandas/tests/scalar/timestamp/test_constructors.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,6 @@ def test_constructor_with_stringoffset(self):
621621
]
622622

623623
timezones = [
624-
(None, 0),
625624
("UTC", 0),
626625
(pytz.utc, 0),
627626
("Asia/Tokyo", 9),
@@ -1013,6 +1012,18 @@ def test_timestamp_constructed_by_date_and_tz(self, tz):
10131012
assert result.hour == expected.hour
10141013
assert result == expected
10151014

1015+
def test_explicit_tz_none(self):
1016+
# GH#48688
1017+
msg = "Passed data is timezone-aware, incompatible with 'tz=None'"
1018+
with pytest.raises(ValueError, match=msg):
1019+
Timestamp(datetime(2022, 1, 1, tzinfo=timezone.utc), tz=None)
1020+
1021+
with pytest.raises(ValueError, match=msg):
1022+
Timestamp("2022-01-01 00:00:00", tzinfo=timezone.utc, tz=None)
1023+
1024+
with pytest.raises(ValueError, match=msg):
1025+
Timestamp("2022-01-01 00:00:00-0400", tz=None)
1026+
10161027

10171028
def test_constructor_ambiguous_dst():
10181029
# GH 24329

pandas/tests/scalar/timestamp/test_formats.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def test_repr(self, date, freq, tz):
118118
def test_repr_utcoffset(self):
119119
# This can cause the tz field to be populated, but it's redundant to
120120
# include this information in the date-string.
121-
date_with_utc_offset = Timestamp("2014-03-13 00:00:00-0400", tz=None)
121+
date_with_utc_offset = Timestamp("2014-03-13 00:00:00-0400")
122122
assert "2014-03-13 00:00:00-0400" in repr(date_with_utc_offset)
123123
assert "tzoffset" not in repr(date_with_utc_offset)
124124
assert "UTC-04:00" in repr(date_with_utc_offset)

0 commit comments

Comments
 (0)