Skip to content

Commit 35b8046

Browse files
phoflpmhatre1
authored andcommitted
Remove support for errors="ignore" in to_datetime, to_timedelta and to_numeric (pandas-dev#57361)
* Remove support for errors="ignore" in to_datetime, to_timedelta and to_numeric * Remove support for errors="ignore" in to_datetime, to_timedelta and to_numeric * Update * Fixup * Fixup * Update * Update * Update * Fixup * Revert * Update
1 parent 2470477 commit 35b8046

File tree

17 files changed

+80
-551
lines changed

17 files changed

+80
-551
lines changed

asv_bench/benchmarks/inference.py

+11-27
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,20 @@
2222

2323

2424
class ToNumeric:
25-
params = ["ignore", "coerce"]
26-
param_names = ["errors"]
27-
28-
def setup(self, errors):
25+
def setup(self):
2926
N = 10000
3027
self.float = Series(np.random.randn(N))
3128
self.numstr = self.float.astype("str")
3229
self.str = Series(Index([f"i-{i}" for i in range(N)], dtype=object))
3330

34-
def time_from_float(self, errors):
35-
to_numeric(self.float, errors=errors)
31+
def time_from_float(self):
32+
to_numeric(self.float, errors="coerce")
3633

37-
def time_from_numeric_str(self, errors):
38-
to_numeric(self.numstr, errors=errors)
34+
def time_from_numeric_str(self):
35+
to_numeric(self.numstr, errors="coerce")
3936

40-
def time_from_str(self, errors):
41-
to_numeric(self.str, errors=errors)
37+
def time_from_str(self):
38+
to_numeric(self.str, errors="coerce")
4239

4340

4441
class ToNumericDowncast:
@@ -187,7 +184,7 @@ def time_iso8601_tz_spaceformat(self):
187184

188185
def time_iso8601_infer_zero_tz_fromat(self):
189186
# GH 41047
190-
to_datetime(self.strings_zero_tz, infer_datetime_format=True)
187+
to_datetime(self.strings_zero_tz)
191188

192189

193190
class ToDatetimeNONISO8601:
@@ -271,16 +268,6 @@ def time_dup_string_tzoffset_dates(self, cache):
271268
to_datetime(self.dup_string_with_tz, cache=cache)
272269

273270

274-
# GH 43901
275-
class ToDatetimeInferDatetimeFormat:
276-
def setup(self):
277-
rng = date_range(start="1/1/2000", periods=100000, freq="h")
278-
self.strings = rng.strftime("%Y-%m-%d %H:%M:%S").tolist()
279-
280-
def time_infer_datetime_format(self):
281-
to_datetime(self.strings, infer_datetime_format=True)
282-
283-
284271
class ToTimedelta:
285272
def setup(self):
286273
self.ints = np.random.randint(0, 60, size=10000)
@@ -301,16 +288,13 @@ def time_convert_string_seconds(self):
301288

302289

303290
class ToTimedeltaErrors:
304-
params = ["coerce", "ignore"]
305-
param_names = ["errors"]
306-
307-
def setup(self, errors):
291+
def setup(self):
308292
ints = np.random.randint(0, 60, size=10000)
309293
self.arr = [f"{i} days" for i in ints]
310294
self.arr[-1] = "apple"
311295

312-
def time_convert(self, errors):
313-
to_timedelta(self.arr, errors=errors)
296+
def time_convert(self):
297+
to_timedelta(self.arr, errors="coerce")
314298

315299

316300
from .pandas_vb_common import setup # noqa: F401 isort:skip

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Removal of prior version deprecations/changes
126126
- Removed ``use_nullable_dtypes`` from :func:`read_parquet` (:issue:`51853`)
127127
- Removed deprecated argument ``obj`` in :meth:`.DataFrameGroupBy.get_group` and :meth:`.SeriesGroupBy.get_group` (:issue:`53545`)
128128
- Removed deprecated behavior of :meth:`Series.agg` using :meth:`Series.apply` (:issue:`53325`)
129+
- Removed support for ``errors="ignore"`` in :func:`to_datetime`, :func:`to_timedelta` and :func:`to_numeric` (:issue:`55734`)
129130
- Removed the ``ArrayManager`` (:issue:`55043`)
130131
- Removed the ``fastpath`` argument from the :class:`Series` constructor (:issue:`55466`)
131132
- Removed the ``is_boolean``, ``is_integer``, ``is_floating``, ``holds_integer``, ``is_numeric``, ``is_categorical``, ``is_object``, and ``is_interval`` attributes of :class:`Index` (:issue:`50042`)

pandas/_libs/tslib.pyx

+3-11
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,13 @@ def array_with_unit_to_datetime(
272272
"""
273273
cdef:
274274
Py_ssize_t i, n=len(values)
275-
bint is_ignore = errors == "ignore"
276275
bint is_coerce = errors == "coerce"
277276
bint is_raise = errors == "raise"
278277
ndarray[int64_t] iresult
279278
tzinfo tz = None
280279
float fval
281280

282-
assert is_ignore or is_coerce or is_raise
281+
assert is_coerce or is_raise
283282

284283
if unit == "ns":
285284
result, tz = array_to_datetime(
@@ -342,11 +341,6 @@ def array_with_unit_to_datetime(
342341
if is_raise:
343342
err.args = (f"{err}, at position {i}",)
344343
raise
345-
elif is_ignore:
346-
# we have hit an exception
347-
# and are in ignore mode
348-
# redo as object
349-
return _array_with_unit_to_datetime_object_fallback(values, unit)
350344
else:
351345
# is_coerce
352346
iresult[i] = NPY_NAT
@@ -461,7 +455,6 @@ cpdef array_to_datetime(
461455
bint utc_convert = bool(utc)
462456
bint seen_datetime_offset = False
463457
bint is_raise = errors == "raise"
464-
bint is_ignore = errors == "ignore"
465458
bint is_coerce = errors == "coerce"
466459
bint is_same_offsets
467460
_TSObject tsobj
@@ -475,7 +468,7 @@ cpdef array_to_datetime(
475468
str abbrev
476469

477470
# specify error conditions
478-
assert is_raise or is_ignore or is_coerce
471+
assert is_raise or is_coerce
479472

480473
if infer_reso:
481474
abbrev = "ns"
@@ -687,7 +680,6 @@ cdef _array_to_datetime_object(
687680
cdef:
688681
Py_ssize_t i, n = values.size
689682
object val
690-
bint is_ignore = errors == "ignore"
691683
bint is_coerce = errors == "coerce"
692684
bint is_raise = errors == "raise"
693685
ndarray oresult_nd
@@ -696,7 +688,7 @@ cdef _array_to_datetime_object(
696688
cnp.broadcast mi
697689
_TSObject tsobj
698690

699-
assert is_raise or is_ignore or is_coerce
691+
assert is_raise or is_coerce
700692

701693
oresult_nd = cnp.PyArray_EMPTY(values.ndim, values.shape, cnp.NPY_OBJECT, 0)
702694
mi = cnp.PyArray_MultiIterNew2(oresult_nd, values)

pandas/_libs/tslibs/strptime.pyx

+4-7
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def array_strptime(
310310
values : ndarray of string-like objects
311311
fmt : string-like regex
312312
exact : matches must be exact if True, search if False
313-
errors : string specifying error handling, {'raise', 'ignore', 'coerce'}
313+
errors : string specifying error handling, {'raise', 'coerce'}
314314
creso : NPY_DATETIMEUNIT, default NPY_FR_ns
315315
Set to NPY_FR_GENERIC to infer a resolution.
316316
"""
@@ -322,7 +322,6 @@ def array_strptime(
322322
object val
323323
bint seen_datetime_offset = False
324324
bint is_raise = errors=="raise"
325-
bint is_ignore = errors=="ignore"
326325
bint is_coerce = errors=="coerce"
327326
bint is_same_offsets
328327
set out_tzoffset_vals = set()
@@ -334,7 +333,7 @@ def array_strptime(
334333
bint infer_reso = creso == NPY_DATETIMEUNIT.NPY_FR_GENERIC
335334
DatetimeParseState state = DatetimeParseState(creso)
336335

337-
assert is_raise or is_ignore or is_coerce
336+
assert is_raise or is_coerce
338337

339338
_validate_fmt(fmt)
340339
format_regex, locale_time = _get_format_regex(fmt)
@@ -806,14 +805,13 @@ def _array_strptime_object_fallback(
806805
object val
807806
tzinfo tz
808807
bint is_raise = errors=="raise"
809-
bint is_ignore = errors=="ignore"
810808
bint is_coerce = errors=="coerce"
811809
bint iso_format = format_is_iso(fmt)
812810
NPY_DATETIMEUNIT creso, out_bestunit, item_reso
813811
int out_local = 0, out_tzoffset = 0
814812
bint string_to_dts_succeeded = 0
815813

816-
assert is_raise or is_ignore or is_coerce
814+
assert is_raise or is_coerce
817815

818816
item_reso = NPY_DATETIMEUNIT.NPY_FR_GENERIC
819817
format_regex, locale_time = _get_format_regex(fmt)
@@ -922,9 +920,8 @@ def _array_strptime_object_fallback(
922920
if is_coerce:
923921
result[i] = NaT
924922
continue
925-
elif is_raise:
923+
else:
926924
raise
927-
return values
928925

929926
import warnings
930927

pandas/_typing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ def closed(self) -> bool:
434434

435435
# datetime and NaTType
436436
DatetimeNaTType = Union[datetime, "NaTType"]
437-
DateTimeErrorChoices = Union[IgnoreRaise, Literal["coerce"]]
437+
DateTimeErrorChoices = Literal["raise", "coerce"]
438438

439439
# sort_index
440440
SortKind = Literal["quicksort", "mergesort", "heapsort", "stable"]

pandas/core/arrays/datetimes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2394,7 +2394,7 @@ def objects_to_datetime64(
23942394
yearfirst : bool
23952395
utc : bool, default False
23962396
Whether to convert/localize timestamps to UTC.
2397-
errors : {'raise', 'ignore', 'coerce'}
2397+
errors : {'raise', 'coerce'}
23982398
allow_object : bool
23992399
Whether to return an object-dtype ndarray instead of raising if the
24002400
data contains more than one timezone.
@@ -2414,7 +2414,7 @@ def objects_to_datetime64(
24142414
ValueError : if data cannot be converted to datetimes
24152415
TypeError : When a type cannot be converted to datetime
24162416
"""
2417-
assert errors in ["raise", "ignore", "coerce"]
2417+
assert errors in ["raise", "coerce"]
24182418

24192419
# if str-dtype, convert
24202420
data = np.array(data, copy=False, dtype=np.object_)

pandas/core/tools/datetimes.py

+4-48
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def _convert_listlike_datetimes(
337337
unit : str
338338
None or string of the frequency of the passed data
339339
errors : str
340-
error handing behaviors from to_datetime, 'raise', 'coerce', 'ignore'
340+
error handing behaviors from to_datetime, 'raise', 'coerce'
341341
dayfirst : bool
342342
dayfirst parsing behavior from to_datetime
343343
yearfirst : bool
@@ -387,7 +387,6 @@ def _convert_listlike_datetimes(
387387
if not is_supported_dtype(arg_dtype):
388388
# We go to closest supported reso, i.e. "s"
389389
arg = astype_overflowsafe(
390-
# TODO: looks like we incorrectly raise with errors=="ignore"
391390
np.asarray(arg),
392391
np.dtype("M8[s]"),
393392
is_coerce=errors == "coerce",
@@ -418,9 +417,6 @@ def _convert_listlike_datetimes(
418417
if errors == "coerce":
419418
npvalues = np.array(["NaT"], dtype="datetime64[ns]").repeat(len(arg))
420419
return DatetimeIndex(npvalues, name=name)
421-
elif errors == "ignore":
422-
idx = Index(arg, name=name)
423-
return idx
424420
raise
425421

426422
arg = ensure_object(arg)
@@ -525,12 +521,7 @@ def _to_datetime_with_unit(arg, unit, name, utc: bool, errors: str) -> Index:
525521
arg = arg.astype(object, copy=False)
526522
arr, tz_parsed = tslib.array_with_unit_to_datetime(arg, unit, errors=errors)
527523

528-
if errors == "ignore":
529-
# Index constructor _may_ infer to DatetimeIndex
530-
result = Index._with_infer(arr, name=name)
531-
else:
532-
result = DatetimeIndex(arr, name=name)
533-
524+
result = DatetimeIndex(arr, name=name)
534525
if not isinstance(result, DatetimeIndex):
535526
return result
536527

@@ -629,7 +620,6 @@ def to_datetime(
629620
format: str | None = ...,
630621
exact: bool = ...,
631622
unit: str | None = ...,
632-
infer_datetime_format: bool = ...,
633623
origin=...,
634624
cache: bool = ...,
635625
) -> Timestamp:
@@ -646,7 +636,6 @@ def to_datetime(
646636
format: str | None = ...,
647637
exact: bool = ...,
648638
unit: str | None = ...,
649-
infer_datetime_format: bool = ...,
650639
origin=...,
651640
cache: bool = ...,
652641
) -> Series:
@@ -663,7 +652,6 @@ def to_datetime(
663652
format: str | None = ...,
664653
exact: bool = ...,
665654
unit: str | None = ...,
666-
infer_datetime_format: bool = ...,
667655
origin=...,
668656
cache: bool = ...,
669657
) -> DatetimeIndex:
@@ -679,7 +667,6 @@ def to_datetime(
679667
format: str | None = None,
680668
exact: bool | lib.NoDefault = lib.no_default,
681669
unit: str | None = None,
682-
infer_datetime_format: lib.NoDefault | bool = lib.no_default,
683670
origin: str = "unix",
684671
cache: bool = True,
685672
) -> DatetimeIndex | Series | DatetimeScalar | NaTType | None:
@@ -696,10 +683,9 @@ def to_datetime(
696683
method expects minimally the following columns: :const:`"year"`,
697684
:const:`"month"`, :const:`"day"`. The column "year"
698685
must be specified in 4-digit format.
699-
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
686+
errors : {'raise', 'coerce'}, default 'raise'
700687
- If :const:`'raise'`, then invalid parsing will raise an exception.
701688
- If :const:`'coerce'`, then invalid parsing will be set as :const:`NaT`.
702-
- If :const:`'ignore'`, then invalid parsing will return the input.
703689
dayfirst : bool, default False
704690
Specify a date parse order if `arg` is str or is list-like.
705691
If :const:`True`, parses dates with the day first, e.g. :const:`"10/11/12"`
@@ -780,16 +766,6 @@ def to_datetime(
780766
integer or float number. This will be based off the origin.
781767
Example, with ``unit='ms'`` and ``origin='unix'``, this would calculate
782768
the number of milliseconds to the unix epoch start.
783-
infer_datetime_format : bool, default False
784-
If :const:`True` and no `format` is given, attempt to infer the format
785-
of the datetime strings based on the first non-NaN element,
786-
and if it can be inferred, switch to a faster method of parsing them.
787-
In some cases this can increase the parsing speed by ~5-10x.
788-
789-
.. deprecated:: 2.0.0
790-
A strict version of this argument is now the default, passing it has
791-
no effect.
792-
793769
origin : scalar, default 'unix'
794770
Define the reference date. The numeric values would be parsed as number
795771
of units (defined by `unit`) since this reference date.
@@ -1012,25 +988,6 @@ def to_datetime(
1012988
"""
1013989
if exact is not lib.no_default and format in {"mixed", "ISO8601"}:
1014990
raise ValueError("Cannot use 'exact' when 'format' is 'mixed' or 'ISO8601'")
1015-
if infer_datetime_format is not lib.no_default:
1016-
warnings.warn(
1017-
"The argument 'infer_datetime_format' is deprecated and will "
1018-
"be removed in a future version. "
1019-
"A strict version of it is now the default, see "
1020-
"https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. "
1021-
"You can safely remove this argument.",
1022-
stacklevel=find_stack_level(),
1023-
)
1024-
if errors == "ignore":
1025-
# GH#54467
1026-
warnings.warn(
1027-
"errors='ignore' is deprecated and will raise in a future version. "
1028-
"Use to_datetime without passing `errors` and catch exceptions "
1029-
"explicitly instead",
1030-
FutureWarning,
1031-
stacklevel=find_stack_level(),
1032-
)
1033-
1034991
if arg is None:
1035992
return None
1036993

@@ -1141,11 +1098,10 @@ def _assemble_from_unit_mappings(
11411098
Parameters
11421099
----------
11431100
arg : DataFrame
1144-
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
1101+
errors : {'raise', 'coerce'}, default 'raise'
11451102
11461103
- If :const:`'raise'`, then invalid parsing will raise an exception
11471104
- If :const:`'coerce'`, then invalid parsing will be set as :const:`NaT`
1148-
- If :const:`'ignore'`, then invalid parsing will return the input
11491105
utc : bool
11501106
Whether to convert/localize timestamps to UTC.
11511107

0 commit comments

Comments
 (0)