Skip to content

Commit 321d5db

Browse files
mroeschkeproost
authored andcommitted
DEPR: Remove errors argument in tz_localize (pandas-dev#29911)
1 parent 72151d1 commit 321d5db

File tree

7 files changed

+11
-126
lines changed

7 files changed

+11
-126
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
458458
- Changed the default value for the `raw` argument in :func:`Series.rolling().apply() <pandas.core.window.Rolling.apply>`, :func:`DataFrame.rolling().apply() <pandas.core.window.Rolling.apply>`,
459459
- :func:`Series.expanding().apply() <pandas.core.window.Expanding.apply>`, and :func:`DataFrame.expanding().apply() <pandas.core.window.Expanding.apply>` to ``False`` (:issue:`20584`)
460460
- Removed previously deprecated :attr:`Timestamp.weekday_name`, :attr:`DatetimeIndex.weekday_name`, and :attr:`Series.dt.weekday_name` (:issue:`18164`)
461+
- Removed previously deprecated ``errors`` argument in :meth:`Timestamp.tz_localize`, :meth:`DatetimeIndex.tz_localize`, and :meth:`Series.tz_localize` (:issue:`22644`)
461462
-
462463

463464
.. _whatsnew_1000.performance:

pandas/_libs/tslibs/nattype.pyx

-12
Original file line numberDiff line numberDiff line change
@@ -720,18 +720,6 @@ default 'raise'
720720
nonexistent times.
721721
722722
.. versionadded:: 0.24.0
723-
errors : 'raise', 'coerce', default None
724-
Determine how errors should be handled.
725-
726-
The behavior is as follows:
727-
728-
* 'raise' will raise a NonExistentTimeError if a timestamp is not
729-
valid in the specified timezone (e.g. due to a transition from
730-
or to DST time). Use ``nonexistent='raise'`` instead.
731-
* 'coerce' will return NaT if the timestamp can not be converted
732-
into the specified timezone. Use ``nonexistent='NaT'`` instead.
733-
734-
.. deprecated:: 0.24.0
735723
736724
Returns
737725
-------

pandas/_libs/tslibs/timestamps.pyx

+1-27
Original file line numberDiff line numberDiff line change
@@ -753,8 +753,7 @@ timedelta}, default 'raise'
753753
# GH#21336, GH#21365
754754
return Timedelta(nanoseconds=1)
755755

756-
def tz_localize(self, tz, ambiguous='raise', nonexistent='raise',
757-
errors=None):
756+
def tz_localize(self, tz, ambiguous='raise', nonexistent='raise'):
758757
"""
759758
Convert naive Timestamp to local time zone, or remove
760759
timezone from tz-aware Timestamp.
@@ -797,18 +796,6 @@ default 'raise'
797796
nonexistent times.
798797
799798
.. versionadded:: 0.24.0
800-
errors : 'raise', 'coerce', default None
801-
Determine how errors should be handled.
802-
803-
The behavior is as follows:
804-
805-
* 'raise' will raise a NonExistentTimeError if a timestamp is not
806-
valid in the specified timezone (e.g. due to a transition from
807-
or to DST time). Use ``nonexistent='raise'`` instead.
808-
* 'coerce' will return NaT if the timestamp can not be converted
809-
into the specified timezone. Use ``nonexistent='NaT'`` instead.
810-
811-
.. deprecated:: 0.24.0
812799
813800
Returns
814801
-------
@@ -822,19 +809,6 @@ default 'raise'
822809
if ambiguous == 'infer':
823810
raise ValueError('Cannot infer offset with only one time.')
824811

825-
if errors is not None:
826-
warnings.warn("The errors argument is deprecated and will be "
827-
"removed in a future release. Use "
828-
"nonexistent='NaT' or nonexistent='raise' "
829-
"instead.", FutureWarning)
830-
if errors == 'coerce':
831-
nonexistent = 'NaT'
832-
elif errors == 'raise':
833-
nonexistent = 'raise'
834-
else:
835-
raise ValueError("The errors argument must be either 'coerce' "
836-
"or 'raise'.")
837-
838812
nonexistent_options = ('raise', 'NaT', 'shift_forward',
839813
'shift_backward')
840814
if nonexistent not in nonexistent_options and not isinstance(

pandas/core/arrays/datetimes.py

+1-29
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ def tz_convert(self, tz):
955955
dtype = tz_to_dtype(tz)
956956
return self._simple_new(self.asi8, dtype=dtype, freq=self.freq)
957957

958-
def tz_localize(self, tz, ambiguous="raise", nonexistent="raise", errors=None):
958+
def tz_localize(self, tz, ambiguous="raise", nonexistent="raise"):
959959
"""
960960
Localize tz-naive Datetime Array/Index to tz-aware
961961
Datetime Array/Index.
@@ -1004,17 +1004,6 @@ def tz_localize(self, tz, ambiguous="raise", nonexistent="raise", errors=None):
10041004
10051005
.. versionadded:: 0.24.0
10061006
1007-
errors : {'raise', 'coerce'}, default None
1008-
The method to handle errors:
1009-
1010-
- 'raise' will raise a NonExistentTimeError if a timestamp is not
1011-
valid in the specified time zone (e.g. due to a transition from
1012-
or to DST time). Use ``nonexistent='raise'`` instead.
1013-
- 'coerce' will return NaT if the timestamp can not be converted
1014-
to the specified time zone. Use ``nonexistent='NaT'`` instead.
1015-
1016-
.. deprecated:: 0.24.0
1017-
10181007
Returns
10191008
-------
10201009
Same type as self
@@ -1105,23 +1094,6 @@ def tz_localize(self, tz, ambiguous="raise", nonexistent="raise", errors=None):
11051094
1 2015-03-29 03:30:00+02:00
11061095
dtype: datetime64[ns, 'Europe/Warsaw']
11071096
"""
1108-
if errors is not None:
1109-
warnings.warn(
1110-
"The errors argument is deprecated and will be "
1111-
"removed in a future release. Use "
1112-
"nonexistent='NaT' or nonexistent='raise' "
1113-
"instead.",
1114-
FutureWarning,
1115-
)
1116-
if errors == "coerce":
1117-
nonexistent = "NaT"
1118-
elif errors == "raise":
1119-
nonexistent = "raise"
1120-
else:
1121-
raise ValueError(
1122-
"The errors argument must be either 'coerce' or 'raise'."
1123-
)
1124-
11251097
nonexistent_options = ("raise", "NaT", "shift_forward", "shift_backward")
11261098
if nonexistent not in nonexistent_options and not isinstance(
11271099
nonexistent, timedelta

pandas/tests/indexes/datetimes/test_timezones.py

+2-20
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,9 @@ def test_dti_tz_localize_nonexistent_raise_coerce(self):
323323
index.tz_localize(tz=tz)
324324

325325
with pytest.raises(pytz.NonExistentTimeError):
326-
with tm.assert_produces_warning(FutureWarning):
327-
index.tz_localize(tz=tz, errors="raise")
326+
index.tz_localize(tz=tz, nonexistent="raise")
328327

329-
with tm.assert_produces_warning(
330-
FutureWarning, clear=FutureWarning, check_stacklevel=False
331-
):
332-
result = index.tz_localize(tz=tz, errors="coerce")
328+
result = index.tz_localize(tz=tz, nonexistent="NaT")
333329
test_times = ["2015-03-08 01:00-05:00", "NaT", "2015-03-08 03:00-04:00"]
334330
dti = to_datetime(test_times, utc=True)
335331
expected = dti.tz_convert("US/Eastern")
@@ -704,20 +700,6 @@ def test_dti_tz_localize_nonexistent_shift_invalid(self, offset, tz_type):
704700
with pytest.raises(ValueError, match=msg):
705701
dti.tz_localize(tz, nonexistent=timedelta(seconds=offset))
706702

707-
@pytest.mark.filterwarnings("ignore::FutureWarning")
708-
def test_dti_tz_localize_errors_deprecation(self):
709-
# GH 22644
710-
tz = "Europe/Warsaw"
711-
n = 60
712-
dti = date_range(start="2015-03-29 02:00:00", periods=n, freq="min")
713-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
714-
with pytest.raises(ValueError):
715-
dti.tz_localize(tz, errors="foo")
716-
# make sure errors='coerce' gets mapped correctly to nonexistent
717-
result = dti.tz_localize(tz, errors="coerce")
718-
expected = dti.tz_localize(tz, nonexistent="NaT")
719-
tm.assert_index_equal(result, expected)
720-
721703
# -------------------------------------------------------------
722704
# DatetimeIndex.normalize
723705

pandas/tests/scalar/timestamp/test_timezones.py

+6-23
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import pandas.util._test_decorators as td
1515

1616
from pandas import NaT, Timestamp
17-
import pandas.util.testing as tm
1817

1918

2019
class TestTimestampTZOperations:
@@ -80,44 +79,28 @@ def test_tz_localize_ambiguous(self):
8079
("2015-03-29 02:30", "Europe/Belgrade"),
8180
],
8281
)
83-
@pytest.mark.filterwarnings("ignore::FutureWarning")
8482
def test_tz_localize_nonexistent(self, stamp, tz):
8583
# GH#13057
8684
ts = Timestamp(stamp)
8785
with pytest.raises(NonExistentTimeError):
8886
ts.tz_localize(tz)
8987
# GH 22644
9088
with pytest.raises(NonExistentTimeError):
91-
with tm.assert_produces_warning(FutureWarning):
92-
ts.tz_localize(tz, errors="raise")
93-
with tm.assert_produces_warning(FutureWarning):
94-
assert ts.tz_localize(tz, errors="coerce") is NaT
89+
ts.tz_localize(tz, nonexistent="raise")
90+
assert ts.tz_localize(tz, nonexistent="NaT") is NaT
9591

96-
def test_tz_localize_errors_ambiguous(self):
92+
def test_tz_localize_ambiguous_raise(self):
9793
# GH#13057
9894
ts = Timestamp("2015-11-1 01:00")
9995
with pytest.raises(AmbiguousTimeError):
100-
with tm.assert_produces_warning(FutureWarning):
101-
ts.tz_localize("US/Pacific", errors="coerce")
96+
ts.tz_localize("US/Pacific", ambiguous="raise")
10297

103-
@pytest.mark.filterwarnings("ignore::FutureWarning")
104-
def test_tz_localize_errors_invalid_arg(self):
98+
def test_tz_localize_nonexistent_invalid_arg(self):
10599
# GH 22644
106100
tz = "Europe/Warsaw"
107101
ts = Timestamp("2015-03-29 02:00:00")
108102
with pytest.raises(ValueError):
109-
with tm.assert_produces_warning(FutureWarning):
110-
ts.tz_localize(tz, errors="foo")
111-
112-
def test_tz_localize_errors_coerce(self):
113-
# GH 22644
114-
# make sure errors='coerce' gets mapped correctly to nonexistent
115-
tz = "Europe/Warsaw"
116-
ts = Timestamp("2015-03-29 02:00:00")
117-
with tm.assert_produces_warning(FutureWarning):
118-
result = ts.tz_localize(tz, errors="coerce")
119-
expected = ts.tz_localize(tz, nonexistent="NaT")
120-
assert result is expected
103+
ts.tz_localize(tz, nonexistent="foo")
121104

122105
@pytest.mark.parametrize(
123106
"stamp",

pandas/tests/series/test_timezones.py

-15
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,6 @@ def test_series_tz_localize(self):
3333
with pytest.raises(TypeError, match="Already tz-aware"):
3434
ts.tz_localize("US/Eastern")
3535

36-
@pytest.mark.filterwarnings("ignore::FutureWarning")
37-
def test_tz_localize_errors_deprecation(self):
38-
# GH 22644
39-
tz = "Europe/Warsaw"
40-
n = 60
41-
rng = date_range(start="2015-03-29 02:00:00", periods=n, freq="min")
42-
ts = Series(rng)
43-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
44-
with pytest.raises(ValueError):
45-
ts.dt.tz_localize(tz, errors="foo")
46-
# make sure errors='coerce' gets mapped correctly to nonexistent
47-
result = ts.dt.tz_localize(tz, errors="coerce")
48-
expected = ts.dt.tz_localize(tz, nonexistent="NaT")
49-
tm.assert_series_equal(result, expected)
50-
5136
def test_series_tz_localize_ambiguous_bool(self):
5237
# make sure that we are correctly accepting bool values as ambiguous
5338

0 commit comments

Comments
 (0)