Skip to content

Commit 10df94e

Browse files
committed
Fix tests
1 parent ff76e49 commit 10df94e

File tree

20 files changed

+82
-85
lines changed

20 files changed

+82
-85
lines changed

doc/source/user_guide/timeseries.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,7 +2569,7 @@ Ambiguous times when localizing
25692569
because daylight savings time (DST) in a local time zone causes some times to occur
25702570
twice within one day ("clocks fall back"). The following options are available:
25712571

2572-
* ``'raise'``: Raises a ``pytz.AmbiguousTimeError`` (the default behavior)
2572+
* ``'raise'``: Raises a ``ValueError`` (the default behavior)
25732573
* ``'infer'``: Attempt to determine the correct offset base on the monotonicity of the timestamps
25742574
* ``'NaT'``: Replaces ambiguous times with ``NaT``
25752575
* ``bool``: ``True`` represents a DST time, ``False`` represents non-DST time. An array-like of ``bool`` values is supported for a sequence of times.
@@ -2604,7 +2604,7 @@ A DST transition may also shift the local time ahead by 1 hour creating nonexist
26042604
local times ("clocks spring forward"). The behavior of localizing a timeseries with nonexistent times
26052605
can be controlled by the ``nonexistent`` argument. The following options are available:
26062606

2607-
* ``'raise'``: Raises a ``pytz.NonExistentTimeError`` (the default behavior)
2607+
* ``'raise'``: Raises a ``ValueError`` (the default behavior)
26082608
* ``'NaT'``: Replaces nonexistent times with ``NaT``
26092609
* ``'shift_forward'``: Shifts nonexistent times forward to the closest real time
26102610
* ``'shift_backward'``: Shifts nonexistent times backward to the closest real time

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ with the pip extra ``pip install pandas[timezone]``.
251251

252252

253253
Additionally, pandas no longer throws ``pytz`` exceptions for timezone operations leading to ambiguous or nonexistent
254-
times. These operations will now yield
254+
times. These cases will now raise a ``ValueError``.
255255

256256
.. _whatsnew_300.api_breaking.other:
257257

pandas/_libs/tslibs/conversion.pyx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,9 +744,15 @@ cdef datetime _localize_pydatetime(datetime dt, tzinfo tz):
744744
It also assumes that the `tz` input is not None.
745745
"""
746746
if treat_tz_as_pytz(tz):
747+
import pytz
748+
747749
# datetime.replace with pytz may be incorrect result
748750
# TODO: try to respect `fold` attribute
749-
return tz.localize(dt, is_dst=None)
751+
try:
752+
return tz.localize(dt, is_dst=None)
753+
except (pytz.AmbiguousTimeError, pytz.NonExistentTimeError) as err:
754+
# As of pandas 3.0, we raise ValueErrors instead of pytz exceptions
755+
raise ValueError(str(err)) from err
750756
else:
751757
return dt.replace(tzinfo=tz)
752758

pandas/_libs/tslibs/nattype.pyx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ class NaTType(_NaT):
10181018
* bool contains flags to determine if time is dst or not (note
10191019
that this flag is only applicable for ambiguous fall dst dates).
10201020
* 'NaT' will return NaT for an ambiguous time.
1021-
* 'raise' will raise an AmbiguousTimeError for an ambiguous time.
1021+
* 'raise' will raise a ValueError for an ambiguous time.
10221022
10231023
nonexistent : {'raise', 'shift_forward', 'shift_backward, 'NaT', \
10241024
timedelta}, default 'raise'
@@ -1031,7 +1031,7 @@ timedelta}, default 'raise'
10311031
closest existing time.
10321032
* 'NaT' will return NaT where there are nonexistent times.
10331033
* timedelta objects will shift nonexistent times by the timedelta.
1034-
* 'raise' will raise an NonExistentTimeError if there are
1034+
* 'raise' will raise a ValueError if there are
10351035
nonexistent times.
10361036
10371037
Returns
@@ -1119,7 +1119,7 @@ timedelta}, default 'raise'
11191119
* bool contains flags to determine if time is dst or not (note
11201120
that this flag is only applicable for ambiguous fall dst dates).
11211121
* 'NaT' will return NaT for an ambiguous time.
1122-
* 'raise' will raise an AmbiguousTimeError for an ambiguous time.
1122+
* 'raise' will raise a ValueError for an ambiguous time.
11231123
11241124
nonexistent : {'raise', 'shift_forward', 'shift_backward, 'NaT', \
11251125
timedelta}, default 'raise'
@@ -1132,7 +1132,7 @@ timedelta}, default 'raise'
11321132
closest existing time.
11331133
* 'NaT' will return NaT where there are nonexistent times.
11341134
* timedelta objects will shift nonexistent times by the timedelta.
1135-
* 'raise' will raise an NonExistentTimeError if there are
1135+
* 'raise' will raise a ValueError if there are
11361136
nonexistent times.
11371137
11381138
Raises
@@ -1214,7 +1214,7 @@ timedelta}, default 'raise'
12141214
* bool contains flags to determine if time is dst or not (note
12151215
that this flag is only applicable for ambiguous fall dst dates).
12161216
* 'NaT' will return NaT for an ambiguous time.
1217-
* 'raise' will raise an AmbiguousTimeError for an ambiguous time.
1217+
* 'raise' will raise a ValueError for an ambiguous time.
12181218
12191219
nonexistent : {'raise', 'shift_forward', 'shift_backward, 'NaT', \
12201220
timedelta}, default 'raise'
@@ -1227,7 +1227,7 @@ timedelta}, default 'raise'
12271227
closest existing time.
12281228
* 'NaT' will return NaT where there are nonexistent times.
12291229
* timedelta objects will shift nonexistent times by the timedelta.
1230-
* 'raise' will raise an NonExistentTimeError if there are
1230+
* 'raise' will raise a ValueError if there are
12311231
nonexistent times.
12321232
12331233
Raises
@@ -1378,7 +1378,7 @@ timedelta}, default 'raise'
13781378
* bool contains flags to determine if time is dst or not (note
13791379
that this flag is only applicable for ambiguous fall dst dates).
13801380
* 'NaT' will return NaT for an ambiguous time.
1381-
* 'raise' will raise an AmbiguousTimeError for an ambiguous time.
1381+
* 'raise' will raise a ValueError for an ambiguous time.
13821382
13831383
nonexistent : 'shift_forward', 'shift_backward, 'NaT', timedelta, \
13841384
default 'raise'
@@ -1393,7 +1393,7 @@ default 'raise'
13931393
closest existing time.
13941394
* 'NaT' will return NaT where there are nonexistent times.
13951395
* timedelta objects will shift nonexistent times by the timedelta.
1396-
* 'raise' will raise an NonExistentTimeError if there are
1396+
* 'raise' will raise a ValueError if there are
13971397
nonexistent times.
13981398
13991399
Returns

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,7 +2084,7 @@ class Timestamp(_Timestamp):
20842084
* bool contains flags to determine if time is dst or not (note
20852085
that this flag is only applicable for ambiguous fall dst dates).
20862086
* 'NaT' will return NaT for an ambiguous time.
2087-
* 'raise' will raise an AmbiguousTimeError for an ambiguous time.
2087+
* 'raise' will raise a ValueError for an ambiguous time.
20882088
20892089
nonexistent : {'raise', 'shift_forward', 'shift_backward, 'NaT', \
20902090
timedelta}, default 'raise'
@@ -2097,7 +2097,7 @@ timedelta}, default 'raise'
20972097
closest existing time.
20982098
* 'NaT' will return NaT where there are nonexistent times.
20992099
* timedelta objects will shift nonexistent times by the timedelta.
2100-
* 'raise' will raise an NonExistentTimeError if there are
2100+
* 'raise' will raise a ValueError if there are
21012101
nonexistent times.
21022102
21032103
Returns
@@ -2187,7 +2187,7 @@ timedelta}, default 'raise'
21872187
* bool contains flags to determine if time is dst or not (note
21882188
that this flag is only applicable for ambiguous fall dst dates).
21892189
* 'NaT' will return NaT for an ambiguous time.
2190-
* 'raise' will raise an AmbiguousTimeError for an ambiguous time.
2190+
* 'raise' will raise a ValueError for an ambiguous time.
21912191
21922192
nonexistent : {'raise', 'shift_forward', 'shift_backward, 'NaT', \
21932193
timedelta}, default 'raise'
@@ -2200,7 +2200,7 @@ timedelta}, default 'raise'
22002200
closest existing time.
22012201
* 'NaT' will return NaT where there are nonexistent times.
22022202
* timedelta objects will shift nonexistent times by the timedelta.
2203-
* 'raise' will raise an NonExistentTimeError if there are
2203+
* 'raise' will raise a ValueError if there are
22042204
nonexistent times.
22052205
22062206
Raises
@@ -2282,7 +2282,7 @@ timedelta}, default 'raise'
22822282
* bool contains flags to determine if time is dst or not (note
22832283
that this flag is only applicable for ambiguous fall dst dates).
22842284
* 'NaT' will return NaT for an ambiguous time.
2285-
* 'raise' will raise an AmbiguousTimeError for an ambiguous time.
2285+
* 'raise' will raise a ValueError for an ambiguous time.
22862286
22872287
nonexistent : {'raise', 'shift_forward', 'shift_backward, 'NaT', \
22882288
timedelta}, default 'raise'
@@ -2295,7 +2295,7 @@ timedelta}, default 'raise'
22952295
closest existing time.
22962296
* 'NaT' will return NaT where there are nonexistent times.
22972297
* timedelta objects will shift nonexistent times by the timedelta.
2298-
* 'raise' will raise an NonExistentTimeError if there are
2298+
* 'raise' will raise a ValueError if there are
22992299
nonexistent times.
23002300
23012301
Raises
@@ -2410,7 +2410,7 @@ timedelta}, default 'raise'
24102410
* bool contains flags to determine if time is dst or not (note
24112411
that this flag is only applicable for ambiguous fall dst dates).
24122412
* 'NaT' will return NaT for an ambiguous time.
2413-
* 'raise' will raise an AmbiguousTimeError for an ambiguous time.
2413+
* 'raise' will raise a ValueError for an ambiguous time.
24142414
24152415
nonexistent : 'shift_forward', 'shift_backward, 'NaT', timedelta, \
24162416
default 'raise'
@@ -2425,7 +2425,7 @@ default 'raise'
24252425
closest existing time.
24262426
* 'NaT' will return NaT where there are nonexistent times.
24272427
* timedelta objects will shift nonexistent times by the timedelta.
2428-
* 'raise' will raise an NonExistentTimeError if there are
2428+
* 'raise' will raise a ValueError if there are
24292429
nonexistent times.
24302430
24312431
Returns

pandas/core/arrays/datetimelike.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]:
17801780
a non-DST time (note that this flag is only applicable for
17811781
ambiguous times)
17821782
- 'NaT' will return NaT where there are ambiguous times
1783-
- 'raise' will raise an AmbiguousTimeError if there are ambiguous
1783+
- 'raise' will raise a ValueError if there are ambiguous
17841784
times.
17851785
17861786
nonexistent : 'shift_forward', 'shift_backward', 'NaT', timedelta, default 'raise'
@@ -1793,7 +1793,7 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]:
17931793
closest existing time
17941794
- 'NaT' will return NaT where there are nonexistent times
17951795
- timedelta objects will shift nonexistent times by the timedelta
1796-
- 'raise' will raise an NonExistentTimeError if there are
1796+
- 'raise' will raise a ValueError if there are
17971797
nonexistent times.
17981798
17991799
Returns

pandas/core/arrays/datetimes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ def tz_localize(
972972
non-DST time (note that this flag is only applicable for
973973
ambiguous times)
974974
- 'NaT' will return NaT where there are ambiguous times
975-
- 'raise' will raise an AmbiguousTimeError if there are ambiguous
975+
- 'raise' will raise a ValueError if there are ambiguous
976976
times.
977977
978978
nonexistent : 'shift_forward', 'shift_backward, 'NaT', timedelta, \
@@ -986,7 +986,7 @@ def tz_localize(
986986
closest existing time
987987
- 'NaT' will return NaT where there are nonexistent times
988988
- timedelta objects will shift nonexistent times by the timedelta
989-
- 'raise' will raise an NonExistentTimeError if there are
989+
- 'raise' will raise a ValueError if there are
990990
nonexistent times.
991991
992992
Returns

pandas/core/generic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10554,7 +10554,7 @@ def tz_localize(
1055410554
a non-DST time (note that this flag is only applicable for
1055510555
ambiguous times)
1055610556
- 'NaT' will return NaT where there are ambiguous times
10557-
- 'raise' will raise an AmbiguousTimeError if there are ambiguous
10557+
- 'raise' will raise a ValueError if there are ambiguous
1055810558
times.
1055910559
nonexistent : str, default 'raise'
1056010560
A nonexistent time does not exist in a particular timezone
@@ -10566,7 +10566,7 @@ def tz_localize(
1056610566
closest existing time
1056710567
- 'NaT' will return NaT where there are nonexistent times
1056810568
- timedelta objects will shift nonexistent times by the timedelta
10569-
- 'raise' will raise an NonExistentTimeError if there are
10569+
- 'raise' will raise a ValueError if there are
1057010570
nonexistent times.
1057110571
1057210572
Returns

pandas/core/indexes/datetimes.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import warnings
77

88
import numpy as np
9-
import pytz
109

1110
from pandas._libs import (
1211
NaT,
@@ -162,7 +161,7 @@ class DatetimeIndex(DatetimeTimedeltaMixin):
162161
non-DST time (note that this flag is only applicable for ambiguous
163162
times)
164163
- 'NaT' will return NaT where there are ambiguous times
165-
- 'raise' will raise an AmbiguousTimeError if there are ambiguous times.
164+
- 'raise' will raise a ValueError if there are ambiguous times.
166165
dayfirst : bool, default False
167166
If True, parse dates in `data` with the day first order.
168167
yearfirst : bool, default False
@@ -591,7 +590,7 @@ def get_loc(self, key):
591590
elif isinstance(key, str):
592591
try:
593592
parsed, reso = self._parse_with_reso(key)
594-
except (ValueError, pytz.NonExistentTimeError) as err:
593+
except ValueError as err:
595594
raise KeyError(key) from err
596595
self._disallow_mismatched_indexing(parsed)
597596

pandas/tests/indexes/datetimes/methods/test_tz_localize.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from dateutil.tz import gettz
1010
import numpy as np
1111
import pytest
12-
import pytz
1312

1413
from pandas import (
1514
DatetimeIndex,
@@ -69,10 +68,10 @@ def test_dti_tz_localize_nonexistent_raise_coerce(self):
6968
times = ["2015-03-08 01:00", "2015-03-08 02:00", "2015-03-08 03:00"]
7069
index = DatetimeIndex(times)
7170
tz = "US/Eastern"
72-
with pytest.raises(pytz.NonExistentTimeError, match="|".join(times)):
71+
with pytest.raises(ValueError, match="|".join(times)):
7372
index.tz_localize(tz=tz)
7473

75-
with pytest.raises(pytz.NonExistentTimeError, match="|".join(times)):
74+
with pytest.raises(ValueError, match="|".join(times)):
7675
index.tz_localize(tz=tz, nonexistent="raise")
7776

7877
result = index.tz_localize(tz=tz, nonexistent="NaT")
@@ -85,7 +84,7 @@ def test_dti_tz_localize_ambiguous_infer(self, tz):
8584
# November 6, 2011, fall back, repeat 2 AM hour
8685
# With no repeated hours, we cannot infer the transition
8786
dr = date_range(datetime(2011, 11, 6, 0), periods=5, freq=offsets.Hour())
88-
with pytest.raises(pytz.AmbiguousTimeError, match="Cannot infer dst time"):
87+
with pytest.raises(ValueError, match="Cannot infer dst time"):
8988
dr.tz_localize(tz)
9089

9190
def test_dti_tz_localize_ambiguous_infer2(self, tz, unit):
@@ -117,7 +116,7 @@ def test_dti_tz_localize_ambiguous_infer3(self, tz):
117116
def test_dti_tz_localize_ambiguous_times(self, tz):
118117
# March 13, 2011, spring forward, skip from 2 AM to 3 AM
119118
dr = date_range(datetime(2011, 3, 13, 1, 30), periods=3, freq=offsets.Hour())
120-
with pytest.raises(pytz.NonExistentTimeError, match="2011-03-13 02:30:00"):
119+
with pytest.raises(ValueError, match="2011-03-13 02:30:00"):
121120
dr.tz_localize(tz)
122121

123122
# after dst transition, it works
@@ -127,7 +126,7 @@ def test_dti_tz_localize_ambiguous_times(self, tz):
127126

128127
# November 6, 2011, fall back, repeat 2 AM hour
129128
dr = date_range(datetime(2011, 11, 6, 1, 30), periods=3, freq=offsets.Hour())
130-
with pytest.raises(pytz.AmbiguousTimeError, match="Cannot infer dst time"):
129+
with pytest.raises(ValueError, match="Cannot infer dst time"):
131130
dr.tz_localize(tz)
132131

133132
# UTC is OK
@@ -163,11 +162,11 @@ def test_dti_tz_localize(self, prefix):
163162
tm.assert_numpy_array_equal(dti3.values, dti_utc.values)
164163

165164
dti = date_range(start="11/6/2011 1:59", end="11/6/2011 2:00", freq="ms")
166-
with pytest.raises(pytz.AmbiguousTimeError, match="Cannot infer dst time"):
165+
with pytest.raises(ValueError, match="Cannot infer dst time"):
167166
dti.tz_localize(tzstr)
168167

169168
dti = date_range(start="3/13/2011 1:59", end="3/13/2011 2:00", freq="ms")
170-
with pytest.raises(pytz.NonExistentTimeError, match="2011-03-13 02:00:00"):
169+
with pytest.raises(ValueError, match="2011-03-13 02:00:00"):
171170
dti.tz_localize(tzstr)
172171

173172
def test_dti_tz_localize_utc_conversion(self, tz):
@@ -184,7 +183,7 @@ def test_dti_tz_localize_utc_conversion(self, tz):
184183
# DST ambiguity, this should fail
185184
rng = date_range("3/11/2012", "3/12/2012", freq="30min")
186185
# Is this really how it should fail??
187-
with pytest.raises(pytz.NonExistentTimeError, match="2012-03-11 02:00:00"):
186+
with pytest.raises(ValueError, match="2012-03-11 02:00:00"):
188187
rng.tz_localize(tz)
189188

190189
def test_dti_tz_localize_roundtrip(self, tz_aware_fixture):

0 commit comments

Comments
 (0)