diff --git a/asv_bench/benchmarks/inference.py b/asv_bench/benchmarks/inference.py index d5c58033c1157..30cab00e4d3f9 100644 --- a/asv_bench/benchmarks/inference.py +++ b/asv_bench/benchmarks/inference.py @@ -22,23 +22,20 @@ class ToNumeric: - params = ["ignore", "coerce"] - param_names = ["errors"] - - def setup(self, errors): + def setup(self): N = 10000 self.float = Series(np.random.randn(N)) self.numstr = self.float.astype("str") self.str = Series(Index([f"i-{i}" for i in range(N)], dtype=object)) - def time_from_float(self, errors): - to_numeric(self.float, errors=errors) + def time_from_float(self): + to_numeric(self.float, errors="coerce") - def time_from_numeric_str(self, errors): - to_numeric(self.numstr, errors=errors) + def time_from_numeric_str(self): + to_numeric(self.numstr, errors="coerce") - def time_from_str(self, errors): - to_numeric(self.str, errors=errors) + def time_from_str(self): + to_numeric(self.str, errors="coerce") class ToNumericDowncast: @@ -187,7 +184,7 @@ def time_iso8601_tz_spaceformat(self): def time_iso8601_infer_zero_tz_fromat(self): # GH 41047 - to_datetime(self.strings_zero_tz, infer_datetime_format=True) + to_datetime(self.strings_zero_tz) class ToDatetimeNONISO8601: @@ -271,16 +268,6 @@ def time_dup_string_tzoffset_dates(self, cache): to_datetime(self.dup_string_with_tz, cache=cache) -# GH 43901 -class ToDatetimeInferDatetimeFormat: - def setup(self): - rng = date_range(start="1/1/2000", periods=100000, freq="h") - self.strings = rng.strftime("%Y-%m-%d %H:%M:%S").tolist() - - def time_infer_datetime_format(self): - to_datetime(self.strings, infer_datetime_format=True) - - class ToTimedelta: def setup(self): self.ints = np.random.randint(0, 60, size=10000) @@ -301,16 +288,13 @@ def time_convert_string_seconds(self): class ToTimedeltaErrors: - params = ["coerce", "ignore"] - param_names = ["errors"] - - def setup(self, errors): + def setup(self): ints = np.random.randint(0, 60, size=10000) self.arr = [f"{i} days" for i in ints] self.arr[-1] = "apple" - def time_convert(self, errors): - to_timedelta(self.arr, errors=errors) + def time_convert(self): + to_timedelta(self.arr, errors="coerce") from .pandas_vb_common import setup # noqa: F401 isort:skip diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index aa378faac2a00..25cd669a48c36 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -123,6 +123,7 @@ Removal of prior version deprecations/changes - Removed ``read_gbq`` and ``DataFrame.to_gbq``. Use ``pandas_gbq.read_gbq`` and ``pandas_gbq.to_gbq`` instead https://pandas-gbq.readthedocs.io/en/latest/api.html (:issue:`55525`) - Removed deprecated argument ``obj`` in :meth:`.DataFrameGroupBy.get_group` and :meth:`.SeriesGroupBy.get_group` (:issue:`53545`) - Removed deprecated behavior of :meth:`Series.agg` using :meth:`Series.apply` (:issue:`53325`) +- Removed support for ``errors="ignore"`` in :func:`to_datetime`, :func:`to_timedelta` and :func:`to_numeric` (:issue:`55734`) - Removed the ``ArrayManager`` (:issue:`55043`) - Removed the ``fastpath`` argument from the :class:`Series` constructor (:issue:`55466`) - 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`) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 017fdc4bc834f..4f70b77780c2b 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -272,14 +272,13 @@ def array_with_unit_to_datetime( """ cdef: Py_ssize_t i, n=len(values) - bint is_ignore = errors == "ignore" bint is_coerce = errors == "coerce" bint is_raise = errors == "raise" ndarray[int64_t] iresult tzinfo tz = None float fval - assert is_ignore or is_coerce or is_raise + assert is_coerce or is_raise if unit == "ns": result, tz = array_to_datetime( @@ -342,11 +341,6 @@ def array_with_unit_to_datetime( if is_raise: err.args = (f"{err}, at position {i}",) raise - elif is_ignore: - # we have hit an exception - # and are in ignore mode - # redo as object - return _array_with_unit_to_datetime_object_fallback(values, unit) else: # is_coerce iresult[i] = NPY_NAT @@ -461,7 +455,6 @@ cpdef array_to_datetime( bint utc_convert = bool(utc) bint seen_datetime_offset = False bint is_raise = errors == "raise" - bint is_ignore = errors == "ignore" bint is_coerce = errors == "coerce" bint is_same_offsets _TSObject tsobj @@ -475,7 +468,7 @@ cpdef array_to_datetime( str abbrev # specify error conditions - assert is_raise or is_ignore or is_coerce + assert is_raise or is_coerce if infer_reso: abbrev = "ns" @@ -687,7 +680,6 @@ cdef _array_to_datetime_object( cdef: Py_ssize_t i, n = values.size object val - bint is_ignore = errors == "ignore" bint is_coerce = errors == "coerce" bint is_raise = errors == "raise" ndarray oresult_nd @@ -696,7 +688,7 @@ cdef _array_to_datetime_object( cnp.broadcast mi _TSObject tsobj - assert is_raise or is_ignore or is_coerce + assert is_raise or is_coerce oresult_nd = cnp.PyArray_EMPTY(values.ndim, values.shape, cnp.NPY_OBJECT, 0) mi = cnp.PyArray_MultiIterNew2(oresult_nd, values) diff --git a/pandas/_libs/tslibs/strptime.pyx b/pandas/_libs/tslibs/strptime.pyx index c09835c9661f3..cfced4ab44aa0 100644 --- a/pandas/_libs/tslibs/strptime.pyx +++ b/pandas/_libs/tslibs/strptime.pyx @@ -310,7 +310,7 @@ def array_strptime( values : ndarray of string-like objects fmt : string-like regex exact : matches must be exact if True, search if False - errors : string specifying error handling, {'raise', 'ignore', 'coerce'} + errors : string specifying error handling, {'raise', 'coerce'} creso : NPY_DATETIMEUNIT, default NPY_FR_ns Set to NPY_FR_GENERIC to infer a resolution. """ @@ -322,7 +322,6 @@ def array_strptime( object val bint seen_datetime_offset = False bint is_raise = errors=="raise" - bint is_ignore = errors=="ignore" bint is_coerce = errors=="coerce" bint is_same_offsets set out_tzoffset_vals = set() @@ -334,7 +333,7 @@ def array_strptime( bint infer_reso = creso == NPY_DATETIMEUNIT.NPY_FR_GENERIC DatetimeParseState state = DatetimeParseState(creso) - assert is_raise or is_ignore or is_coerce + assert is_raise or is_coerce _validate_fmt(fmt) format_regex, locale_time = _get_format_regex(fmt) @@ -806,14 +805,13 @@ def _array_strptime_object_fallback( object val tzinfo tz bint is_raise = errors=="raise" - bint is_ignore = errors=="ignore" bint is_coerce = errors=="coerce" bint iso_format = format_is_iso(fmt) NPY_DATETIMEUNIT creso, out_bestunit, item_reso int out_local = 0, out_tzoffset = 0 bint string_to_dts_succeeded = 0 - assert is_raise or is_ignore or is_coerce + assert is_raise or is_coerce item_reso = NPY_DATETIMEUNIT.NPY_FR_GENERIC format_regex, locale_time = _get_format_regex(fmt) @@ -922,9 +920,8 @@ def _array_strptime_object_fallback( if is_coerce: result[i] = NaT continue - elif is_raise: + else: raise - return values import warnings diff --git a/pandas/_typing.py b/pandas/_typing.py index 00135bffbd435..9465631e9fe20 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -434,7 +434,7 @@ def closed(self) -> bool: # datetime and NaTType DatetimeNaTType = Union[datetime, "NaTType"] -DateTimeErrorChoices = Union[IgnoreRaise, Literal["coerce"]] +DateTimeErrorChoices = Literal["raise", "coerce"] # sort_index SortKind = Literal["quicksort", "mergesort", "heapsort", "stable"] diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index bc8d170b73fd0..29681539d146b 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -2394,7 +2394,7 @@ def objects_to_datetime64( yearfirst : bool utc : bool, default False Whether to convert/localize timestamps to UTC. - errors : {'raise', 'ignore', 'coerce'} + errors : {'raise', 'coerce'} allow_object : bool Whether to return an object-dtype ndarray instead of raising if the data contains more than one timezone. @@ -2414,7 +2414,7 @@ def objects_to_datetime64( ValueError : if data cannot be converted to datetimes TypeError : When a type cannot be converted to datetime """ - assert errors in ["raise", "ignore", "coerce"] + assert errors in ["raise", "coerce"] # if str-dtype, convert data = np.array(data, copy=False, dtype=np.object_) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 6c8c2c7e5009e..325ba90c21c29 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -337,7 +337,7 @@ def _convert_listlike_datetimes( unit : str None or string of the frequency of the passed data errors : str - error handing behaviors from to_datetime, 'raise', 'coerce', 'ignore' + error handing behaviors from to_datetime, 'raise', 'coerce' dayfirst : bool dayfirst parsing behavior from to_datetime yearfirst : bool @@ -387,7 +387,6 @@ def _convert_listlike_datetimes( if not is_supported_dtype(arg_dtype): # We go to closest supported reso, i.e. "s" arg = astype_overflowsafe( - # TODO: looks like we incorrectly raise with errors=="ignore" np.asarray(arg), np.dtype("M8[s]"), is_coerce=errors == "coerce", @@ -418,9 +417,6 @@ def _convert_listlike_datetimes( if errors == "coerce": npvalues = np.array(["NaT"], dtype="datetime64[ns]").repeat(len(arg)) return DatetimeIndex(npvalues, name=name) - elif errors == "ignore": - idx = Index(arg, name=name) - return idx raise arg = ensure_object(arg) @@ -525,12 +521,7 @@ def _to_datetime_with_unit(arg, unit, name, utc: bool, errors: str) -> Index: arg = arg.astype(object, copy=False) arr, tz_parsed = tslib.array_with_unit_to_datetime(arg, unit, errors=errors) - if errors == "ignore": - # Index constructor _may_ infer to DatetimeIndex - result = Index._with_infer(arr, name=name) - else: - result = DatetimeIndex(arr, name=name) - + result = DatetimeIndex(arr, name=name) if not isinstance(result, DatetimeIndex): return result @@ -629,7 +620,6 @@ def to_datetime( format: str | None = ..., exact: bool = ..., unit: str | None = ..., - infer_datetime_format: bool = ..., origin=..., cache: bool = ..., ) -> Timestamp: @@ -646,7 +636,6 @@ def to_datetime( format: str | None = ..., exact: bool = ..., unit: str | None = ..., - infer_datetime_format: bool = ..., origin=..., cache: bool = ..., ) -> Series: @@ -663,7 +652,6 @@ def to_datetime( format: str | None = ..., exact: bool = ..., unit: str | None = ..., - infer_datetime_format: bool = ..., origin=..., cache: bool = ..., ) -> DatetimeIndex: @@ -679,7 +667,6 @@ def to_datetime( format: str | None = None, exact: bool | lib.NoDefault = lib.no_default, unit: str | None = None, - infer_datetime_format: lib.NoDefault | bool = lib.no_default, origin: str = "unix", cache: bool = True, ) -> DatetimeIndex | Series | DatetimeScalar | NaTType | None: @@ -696,10 +683,9 @@ def to_datetime( method expects minimally the following columns: :const:`"year"`, :const:`"month"`, :const:`"day"`. The column "year" must be specified in 4-digit format. - errors : {'ignore', 'raise', 'coerce'}, default 'raise' + errors : {'raise', 'coerce'}, default 'raise' - If :const:`'raise'`, then invalid parsing will raise an exception. - If :const:`'coerce'`, then invalid parsing will be set as :const:`NaT`. - - If :const:`'ignore'`, then invalid parsing will return the input. dayfirst : bool, default False Specify a date parse order if `arg` is str or is list-like. If :const:`True`, parses dates with the day first, e.g. :const:`"10/11/12"` @@ -780,16 +766,6 @@ def to_datetime( integer or float number. This will be based off the origin. Example, with ``unit='ms'`` and ``origin='unix'``, this would calculate the number of milliseconds to the unix epoch start. - infer_datetime_format : bool, default False - If :const:`True` and no `format` is given, attempt to infer the format - of the datetime strings based on the first non-NaN element, - and if it can be inferred, switch to a faster method of parsing them. - In some cases this can increase the parsing speed by ~5-10x. - - .. deprecated:: 2.0.0 - A strict version of this argument is now the default, passing it has - no effect. - origin : scalar, default 'unix' Define the reference date. The numeric values would be parsed as number of units (defined by `unit`) since this reference date. @@ -1012,25 +988,6 @@ def to_datetime( """ if exact is not lib.no_default and format in {"mixed", "ISO8601"}: raise ValueError("Cannot use 'exact' when 'format' is 'mixed' or 'ISO8601'") - if infer_datetime_format is not lib.no_default: - warnings.warn( - "The argument 'infer_datetime_format' is deprecated and will " - "be removed in a future version. " - "A strict version of it is now the default, see " - "https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. " - "You can safely remove this argument.", - stacklevel=find_stack_level(), - ) - if errors == "ignore": - # GH#54467 - warnings.warn( - "errors='ignore' is deprecated and will raise in a future version. " - "Use to_datetime without passing `errors` and catch exceptions " - "explicitly instead", - FutureWarning, - stacklevel=find_stack_level(), - ) - if arg is None: return None @@ -1141,11 +1098,10 @@ def _assemble_from_unit_mappings( Parameters ---------- arg : DataFrame - errors : {'ignore', 'raise', 'coerce'}, default 'raise' + errors : {'raise', 'coerce'}, default 'raise' - If :const:`'raise'`, then invalid parsing will raise an exception - If :const:`'coerce'`, then invalid parsing will be set as :const:`NaT` - - If :const:`'ignore'`, then invalid parsing will return the input utc : bool Whether to convert/localize timestamps to UTC. diff --git a/pandas/core/tools/numeric.py b/pandas/core/tools/numeric.py index 2ae57d3c8508e..3d28a73df99d1 100644 --- a/pandas/core/tools/numeric.py +++ b/pandas/core/tools/numeric.py @@ -4,12 +4,10 @@ TYPE_CHECKING, Literal, ) -import warnings import numpy as np from pandas._libs import lib -from pandas.util._exceptions import find_stack_level from pandas.util._validators import check_dtype_backend from pandas.core.dtypes.cast import maybe_downcast_numeric @@ -66,14 +64,9 @@ def to_numeric( ---------- arg : scalar, list, tuple, 1-d array, or Series Argument to be converted. - errors : {'ignore', 'raise', 'coerce'}, default 'raise' + errors : {'raise', 'coerce'}, default 'raise' - If 'raise', then invalid parsing will raise an exception. - If 'coerce', then invalid parsing will be set as NaN. - - If 'ignore', then invalid parsing will return the input. - - .. versionchanged:: 2.2 - - "ignore" is deprecated. Catch exceptions explicitly instead. downcast : str, default None Can be 'integer', 'signed', 'unsigned', or 'float'. @@ -166,17 +159,8 @@ def to_numeric( if downcast not in (None, "integer", "signed", "unsigned", "float"): raise ValueError("invalid downcasting method provided") - if errors not in ("ignore", "raise", "coerce"): + if errors not in ("raise", "coerce"): raise ValueError("invalid error value specified") - if errors == "ignore": - # GH#54467 - warnings.warn( - "errors='ignore' is deprecated and will raise in a future version. " - "Use to_numeric without passing `errors` and catch exceptions " - "explicitly instead", - FutureWarning, - stacklevel=find_stack_level(), - ) check_dtype_backend(dtype_backend) @@ -207,8 +191,6 @@ def to_numeric( else: values = arg - orig_values = values - # GH33013: for IntegerArray & FloatingArray extract non-null values for casting # save mask to reconstruct the full array after casting mask: npt.NDArray[np.bool_] | None = None @@ -227,20 +209,15 @@ def to_numeric( values = values.view(np.int64) else: values = ensure_object(values) - coerce_numeric = errors not in ("ignore", "raise") - try: - values, new_mask = lib.maybe_convert_numeric( # type: ignore[call-overload] - values, - set(), - coerce_numeric=coerce_numeric, - convert_to_masked_nullable=dtype_backend is not lib.no_default - or isinstance(values_dtype, StringDtype) - and not values_dtype.storage == "pyarrow_numpy", - ) - except (ValueError, TypeError): - if errors == "raise": - raise - values = orig_values + coerce_numeric = errors != "raise" + values, new_mask = lib.maybe_convert_numeric( # type: ignore[call-overload] + values, + set(), + coerce_numeric=coerce_numeric, + convert_to_masked_nullable=dtype_backend is not lib.no_default + or isinstance(values_dtype, StringDtype) + and not values_dtype.storage == "pyarrow_numpy", + ) if new_mask is not None: # Remove unnecessary values, is expected later anyway and enables diff --git a/pandas/core/tools/timedeltas.py b/pandas/core/tools/timedeltas.py index 47dfae3c6cadd..5f3963c3d405e 100644 --- a/pandas/core/tools/timedeltas.py +++ b/pandas/core/tools/timedeltas.py @@ -8,7 +8,6 @@ Any, overload, ) -import warnings import numpy as np @@ -22,7 +21,6 @@ disallow_ambiguous_unit, parse_timedelta_unit, ) -from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import is_list_like from pandas.core.dtypes.dtypes import ArrowDtype @@ -90,7 +88,6 @@ def to_timedelta( | Series, unit: UnitChoices | None = None, errors: DateTimeErrorChoices = "raise", - # returning Any for errors="ignore" ) -> Timedelta | TimedeltaIndex | Series | NaTType | Any: """ Convert argument to timedelta. @@ -130,10 +127,9 @@ def to_timedelta( in a future version. Please use 'h', 'min', 's', 'ms', 'us', and 'ns' instead of 'H', 'T', 'S', 'L', 'U' and 'N'. - errors : {'ignore', 'raise', 'coerce'}, default 'raise' + errors : {'raise', 'coerce'}, default 'raise' - If 'raise', then invalid parsing will raise an exception. - If 'coerce', then invalid parsing will be set as NaT. - - If 'ignore', then invalid parsing will return the input. Returns ------- @@ -185,17 +181,8 @@ def to_timedelta( unit = parse_timedelta_unit(unit) disallow_ambiguous_unit(unit) - if errors not in ("ignore", "raise", "coerce"): - raise ValueError("errors must be one of 'ignore', 'raise', or 'coerce'.") - if errors == "ignore": - # GH#54467 - warnings.warn( - "errors='ignore' is deprecated and will raise in a future version. " - "Use to_timedelta without passing `errors` and catch exceptions " - "explicitly instead", - FutureWarning, - stacklevel=find_stack_level(), - ) + if errors not in ("raise", "coerce"): + raise ValueError("errors must be one of 'raise', or 'coerce'.") if arg is None: return arg @@ -236,9 +223,6 @@ def _coerce_scalar_to_timedelta_type( except ValueError: if errors == "raise": raise - if errors == "ignore": - return r - # coerce result = NaT @@ -254,30 +238,11 @@ def _convert_listlike( """Convert a list of objects to a timedelta index object.""" arg_dtype = getattr(arg, "dtype", None) if isinstance(arg, (list, tuple)) or arg_dtype is None: - # This is needed only to ensure that in the case where we end up - # returning arg (errors == "ignore"), and where the input is a - # generator, we return a useful list-like instead of a - # used-up generator - if not hasattr(arg, "__array__"): - arg = list(arg) arg = np.array(arg, dtype=object) elif isinstance(arg_dtype, ArrowDtype) and arg_dtype.kind == "m": return arg - try: - td64arr = sequence_to_td64ns(arg, unit=unit, errors=errors, copy=False)[0] - except ValueError: - if errors == "ignore": - return arg - else: - # This else-block accounts for the cases when errors='raise' - # and errors='coerce'. If errors == 'raise', these errors - # should be raised. If errors == 'coerce', we shouldn't - # expect any errors to be raised, since all parsing errors - # cause coercion to pd.NaT. However, if an error / bug is - # introduced that causes an Exception to be raised, we would - # like to surface it. - raise + td64arr = sequence_to_td64ns(arg, unit=unit, errors=errors, copy=False)[0] from pandas import TimedeltaIndex diff --git a/pandas/core/tools/times.py b/pandas/core/tools/times.py index d77bcc91df709..f6a610ebc36ab 100644 --- a/pandas/core/tools/times.py +++ b/pandas/core/tools/times.py @@ -5,12 +5,10 @@ time, ) from typing import TYPE_CHECKING -import warnings import numpy as np from pandas._libs.lib import is_list_like -from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.generic import ( ABCIndex, @@ -45,24 +43,16 @@ def to_time( infer_time_format: bool, default False Infer the time format based on the first non-NaN element. If all strings are in the same format, this will speed up conversion. - errors : {'ignore', 'raise', 'coerce'}, default 'raise' + errors : {'raise', 'coerce'}, default 'raise' - If 'raise', then invalid parsing will raise an exception - If 'coerce', then invalid parsing will be set as None - - If 'ignore', then invalid parsing will return the input Returns ------- datetime.time """ - if errors == "ignore": - # GH#54467 - warnings.warn( - "errors='ignore' is deprecated and will raise in a future version. " - "Use to_time without passing `errors` and catch exceptions " - "explicitly instead", - FutureWarning, - stacklevel=find_stack_level(), - ) + if errors not in ("raise", "coerce"): + raise ValueError("errors must be one of 'raise', or 'coerce'.") def _convert_listlike(arg, format): if isinstance(arg, (list, tuple)): @@ -90,10 +80,7 @@ def _convert_listlike(arg, format): f"format {format}" ) raise ValueError(msg) from err - if errors == "ignore": - return arg - else: - times.append(None) + times.append(None) else: formats = _time_formats[:] format_found = False @@ -118,8 +105,6 @@ def _convert_listlike(arg, format): times.append(time_object) elif errors == "raise": raise ValueError(f"Cannot convert arg {arg} to a time") - elif errors == "ignore": - return arg else: times.append(None) diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 08f99a4d3093a..70a111a8f5c56 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -79,7 +79,6 @@ ) from pandas._typing import ( - DateTimeErrorChoices, DtypeArg, DtypeBackend, IndexLabel, @@ -111,14 +110,7 @@ def _handle_date_column( # read_sql like functions. # Format can take on custom to_datetime argument values such as # {"errors": "coerce"} or {"dayfirst": True} - error: DateTimeErrorChoices = format.pop("errors", None) or "ignore" - if error == "ignore": - try: - return to_datetime(col, **format) - except (TypeError, ValueError): - # TODO: not reached 2023-10-27; needed? - return col - return to_datetime(col, errors=error, **format) + return to_datetime(col, **format) else: # Allow passing of formatting string for integers # GH17855 diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index 8bb67fac19c65..c8f4d68230e5b 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -1776,7 +1776,7 @@ def test_api_date_parsing(conn, request): @pytest.mark.parametrize("conn", all_connectable_types) -@pytest.mark.parametrize("error", ["ignore", "raise", "coerce"]) +@pytest.mark.parametrize("error", ["raise", "coerce"]) @pytest.mark.parametrize( "read_sql, text, mode", [ diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index 0cbdba874c5f7..0ed61fdd0ce45 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -57,10 +57,6 @@ r"alongside this." ) -pytestmark = pytest.mark.filterwarnings( - "ignore:errors='ignore' is deprecated:FutureWarning" -) - class TestTimeConversionFormats: def test_to_datetime_readonly(self, writable): @@ -154,28 +150,6 @@ def test_to_datetime_format_YYYYMM_with_nat(self, cache): result = to_datetime(ser, format="%Y%m", cache=cache) tm.assert_series_equal(result, expected) - def test_to_datetime_format_YYYYMMDD_ignore(self, cache): - # coercion - # GH 7930, GH 14487 - ser = Series([20121231, 20141231, 99991231]) - result = to_datetime(ser, format="%Y%m%d", errors="ignore", cache=cache) - expected = Series( - [20121231, 20141231, 99991231], - dtype=object, - ) - tm.assert_series_equal(result, expected) - - def test_to_datetime_format_YYYYMMDD_ignore_with_outofbounds(self, cache): - # https://github.com/pandas-dev/pandas/issues/26493 - result = to_datetime( - ["15010101", "20150101", np.nan], - format="%Y%m%d", - errors="ignore", - cache=cache, - ) - expected = Index(["15010101", "20150101", np.nan], dtype=object) - tm.assert_index_equal(result, expected) - def test_to_datetime_format_YYYYMMDD_coercion(self, cache): # coercion # GH 7930 @@ -282,28 +256,6 @@ def test_to_datetime_format_integer(self, cache): result = to_datetime(ser, format="%Y%m", cache=cache) tm.assert_series_equal(result, expected) - @pytest.mark.parametrize( - "int_date, expected", - [ - # valid date, length == 8 - [20121030, datetime(2012, 10, 30)], - # short valid date, length == 6 - [199934, datetime(1999, 3, 4)], - # long integer date partially parsed to datetime(2012,1,1), length > 8 - [2012010101, 2012010101], - # invalid date partially parsed to datetime(2012,9,9), length == 8 - [20129930, 20129930], - # short integer date partially parsed to datetime(2012,9,9), length < 8 - [2012993, 2012993], - # short invalid date, length == 4 - [2121, 2121], - ], - ) - def test_int_to_datetime_format_YYYYMMDD_typeerror(self, int_date, expected): - # GH 26583 - result = to_datetime(int_date, format="%Y%m%d", errors="ignore") - assert result == expected - def test_to_datetime_format_microsecond(self, cache): month_abbr = calendar.month_abbr[4] val = f"01-{month_abbr}-2011 00:00:01.978" @@ -584,11 +536,6 @@ def test_to_datetime_overflow(self): res = to_datetime([arg], errors="coerce") tm.assert_index_equal(res, Index([NaT])) - res = to_datetime(arg, errors="ignore") - assert isinstance(res, str) and res == arg - res = to_datetime([arg], errors="ignore") - tm.assert_index_equal(res, Index([arg], dtype=object)) - def test_to_datetime_mixed_datetime_and_string(self): # GH#47018 adapted old doctest with new behavior d1 = datetime(2020, 1, 1, 17, tzinfo=timezone(-timedelta(hours=1))) @@ -961,7 +908,7 @@ def test_to_datetime_iso_week_year_format(self, s, _format, dt): ], ], ) - @pytest.mark.parametrize("errors", ["raise", "coerce", "ignore"]) + @pytest.mark.parametrize("errors", ["raise", "coerce"]) def test_error_iso_week_year(self, msg, s, _format, errors): # See GH#16607, GH#50308 # This test checks for errors thrown when giving the wrong format @@ -1019,11 +966,6 @@ def test_to_datetime_YYYYMMDD(self): actual = to_datetime("20080115") assert actual == datetime(2008, 1, 15) - def test_to_datetime_unparsable_ignore(self): - # unparsable - ser = "Month 1, 1999" - assert to_datetime(ser, errors="ignore") == ser - @td.skip_if_windows # `tm.set_timezone` does not work in windows def test_to_datetime_now(self): # See GH#18666 @@ -1118,7 +1060,7 @@ def test_to_datetime_dt64s_and_str(self, arg, format): @pytest.mark.parametrize( "dt", [np.datetime64("1000-01-01"), np.datetime64("5000-01-02")] ) - @pytest.mark.parametrize("errors", ["raise", "ignore", "coerce"]) + @pytest.mark.parametrize("errors", ["raise", "coerce"]) def test_to_datetime_dt64s_out_of_ns_bounds(self, cache, dt, errors): # GH#50369 We cast to the nearest supported reso, i.e. "s" ts = to_datetime(dt, errors=errors, cache=cache) @@ -1178,31 +1120,6 @@ def test_to_datetime_array_of_dt64s(self, cache, unit): expected = DatetimeIndex(np.array(dts_with_oob, dtype="M8[s]")) tm.assert_index_equal(result, expected) - # With errors='ignore', out of bounds datetime64s - # are converted to their .item(), which depending on the version of - # numpy is either a python datetime.datetime or datetime.date - result = to_datetime(dts_with_oob, errors="ignore", cache=cache) - if not cache: - # FIXME: shouldn't depend on cache! - expected = Index(dts_with_oob) - tm.assert_index_equal(result, expected) - - def test_out_of_bounds_errors_ignore(self): - # https://github.com/pandas-dev/pandas/issues/50587 - result = to_datetime(np.datetime64("9999-01-01"), errors="ignore") - expected = np.datetime64("9999-01-01") - assert result == expected - - def test_out_of_bounds_errors_ignore2(self): - # GH#12424 - msg = "errors='ignore' is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): - res = to_datetime( - Series(["2362-01-01", np.nan], dtype=object), errors="ignore" - ) - exp = Series(["2362-01-01", np.nan], dtype=object) - tm.assert_series_equal(res, exp) - def test_to_datetime_tz(self, cache): # xref 8260 # uniform returns a DatetimeIndex @@ -1230,17 +1147,6 @@ def test_to_datetime_tz_mixed(self, cache): with pytest.raises(ValueError, match=msg): to_datetime(arr, cache=cache) - depr_msg = "errors='ignore' is deprecated" - with tm.assert_produces_warning(FutureWarning, match=depr_msg): - result = to_datetime(arr, cache=cache, errors="ignore") - expected = Index( - [ - Timestamp("2013-01-01 13:00:00-08:00"), - Timestamp("2013-01-02 14:00:00-05:00"), - ], - dtype="object", - ) - tm.assert_index_equal(result, expected) result = to_datetime(arr, cache=cache, errors="coerce") expected = DatetimeIndex( ["2013-01-01 13:00:00-08:00", "NaT"], dtype="datetime64[ns, US/Pacific]" @@ -1390,7 +1296,6 @@ def test_datetime_bool(self, cache, arg): with pytest.raises(TypeError, match=msg): to_datetime(arg) assert to_datetime(arg, errors="coerce", cache=cache) is NaT - assert to_datetime(arg, errors="ignore", cache=cache) is arg def test_datetime_bool_arrays_mixed(self, cache): msg = f"{type(cache)} is not convertible to datetime" @@ -1418,7 +1323,7 @@ def test_datetime_invalid_datatype(self, arg): with pytest.raises(TypeError, match=msg): to_datetime(arg) - @pytest.mark.parametrize("errors", ["coerce", "raise", "ignore"]) + @pytest.mark.parametrize("errors", ["coerce", "raise"]) def test_invalid_format_raises(self, errors): # https://github.com/pandas-dev/pandas/issues/50255 with pytest.raises( @@ -1430,9 +1335,6 @@ def test_invalid_format_raises(self, errors): @pytest.mark.parametrize("format", [None, "%H:%M:%S"]) def test_datetime_invalid_scalar(self, value, format): # GH24763 - res = to_datetime(value, errors="ignore", format=format) - assert res == value - res = to_datetime(value, errors="coerce", format=format) assert res is NaT @@ -1453,9 +1355,6 @@ def test_datetime_invalid_scalar(self, value, format): @pytest.mark.parametrize("format", [None, "%H:%M:%S"]) def test_datetime_outofbounds_scalar(self, value, format): # GH24763 - res = to_datetime(value, errors="ignore", format=format) - assert res == value - res = to_datetime(value, errors="coerce", format=format) assert res is NaT @@ -1480,11 +1379,6 @@ def test_datetime_invalid_index(self, values, format): warn = UserWarning else: warn = None - with tm.assert_produces_warning( - warn, match="Could not infer format", raise_on_extra_warnings=False - ): - res = to_datetime(values, errors="ignore", format=format) - tm.assert_index_equal(res, Index(values, dtype=object)) with tm.assert_produces_warning( warn, match="Could not infer format", raise_on_extra_warnings=False @@ -1657,22 +1551,15 @@ def test_to_datetime_coerce_oob(self, string_arg, format, outofbounds): expected = DatetimeIndex([datetime(2018, 3, 1), NaT]) tm.assert_index_equal(result, expected) - @pytest.mark.parametrize( - "errors, expected", - [ - ("coerce", Index([NaT, NaT])), - ("ignore", Index(["200622-12-31", "111111-24-11"], dtype=object)), - ], - ) - def test_to_datetime_malformed_no_raise(self, errors, expected): + def test_to_datetime_malformed_no_raise(self): # GH 28299 # GH 48633 ts_strings = ["200622-12-31", "111111-24-11"] with tm.assert_produces_warning( UserWarning, match="Could not infer format", raise_on_extra_warnings=False ): - result = to_datetime(ts_strings, errors=errors) - tm.assert_index_equal(result, expected) + result = to_datetime(ts_strings, errors="coerce") + tm.assert_index_equal(result, Index([NaT, NaT])) def test_to_datetime_malformed_raise(self): # GH 48633 @@ -1876,11 +1763,6 @@ def test_to_datetime_month_or_year_unit_non_round_float(self, cache, unit): with tm.assert_produces_warning(FutureWarning, match=warn_msg): to_datetime(["1.5"], unit=unit, errors="raise") - # with errors="ignore" we also end up raising within the Timestamp - # constructor; this may not be ideal - with pytest.raises(ValueError, match=msg): - to_datetime([1.5], unit=unit, errors="ignore") - res = to_datetime([1.5], unit=unit, errors="coerce") expected = Index([NaT], dtype="M8[ns]") tm.assert_index_equal(res, expected) @@ -1903,21 +1785,6 @@ def test_unit(self, cache): def test_unit_array_mixed_nans(self, cache): values = [11111111111111111, 1, 1.0, iNaT, NaT, np.nan, "NaT", ""] - result = to_datetime(values, unit="D", errors="ignore", cache=cache) - expected = Index( - [ - 11111111111111111, - Timestamp("1970-01-02"), - Timestamp("1970-01-02"), - NaT, - NaT, - NaT, - NaT, - NaT, - ], - dtype=object, - ) - tm.assert_index_equal(result, expected) result = to_datetime(values, unit="D", errors="coerce", cache=cache) expected = DatetimeIndex( @@ -1933,10 +1800,6 @@ def test_unit_array_mixed_nans(self, cache): def test_unit_array_mixed_nans_large_int(self, cache): values = [1420043460000000000000000, iNaT, NaT, np.nan, "NaT"] - result = to_datetime(values, errors="ignore", unit="s", cache=cache) - expected = Index([1420043460000000000000000, NaT, NaT, NaT, NaT], dtype=object) - tm.assert_index_equal(result, expected) - result = to_datetime(values, errors="coerce", unit="s", cache=cache) expected = DatetimeIndex(["NaT", "NaT", "NaT", "NaT", "NaT"], dtype="M8[ns]") tm.assert_index_equal(result, expected) @@ -1952,7 +1815,7 @@ def test_to_datetime_invalid_str_not_out_of_bounds_valuerror(self, cache): with pytest.raises(ValueError, match=msg): to_datetime("foo", errors="raise", unit="s", cache=cache) - @pytest.mark.parametrize("error", ["raise", "coerce", "ignore"]) + @pytest.mark.parametrize("error", ["raise", "coerce"]) def test_unit_consistency(self, cache, error): # consistency of conversions expected = Timestamp("1970-05-09 14:25:11") @@ -1960,7 +1823,7 @@ def test_unit_consistency(self, cache, error): assert result == expected assert isinstance(result, Timestamp) - @pytest.mark.parametrize("errors", ["ignore", "raise", "coerce"]) + @pytest.mark.parametrize("errors", ["raise", "coerce"]) @pytest.mark.parametrize("dtype", ["float64", "int64"]) def test_unit_with_numeric(self, cache, errors, dtype): # GH 13180 @@ -2030,18 +1893,6 @@ def test_unit_rounding(self, cache): alt = Timestamp(value, unit="s") assert alt == result - def test_unit_ignore_keeps_name(self, cache): - # GH 21697 - expected = Index([15e9] * 2, name="name") - result = to_datetime(expected, errors="ignore", unit="s", cache=cache) - tm.assert_index_equal(result, expected) - - def test_to_datetime_errors_ignore_utc_true(self): - # GH#23758 - result = to_datetime([1], unit="s", utc=True, errors="ignore") - expected = DatetimeIndex(["1970-01-01 00:00:01"], dtype="M8[ns, UTC]") - tm.assert_index_equal(result, expected) - @pytest.mark.parametrize("dtype", [int, float]) def test_to_datetime_unit(self, dtype): epoch = 1370745748 @@ -2119,7 +1970,7 @@ def test_float_to_datetime_raise_near_bounds(self): [0, tsmax_in_days - 0.005, -tsmax_in_days + 0.005], dtype=float ) expected = (should_succeed * oneday_in_ns).astype(np.int64) - for error_mode in ["raise", "coerce", "ignore"]: + for error_mode in ["raise", "coerce"]: result1 = to_datetime(should_succeed, unit="D", errors=error_mode) # Cast to `np.float64` so that `rtol` and inexact checking kick in # (`check_exact` doesn't take place for integer dtypes) @@ -2548,8 +2399,6 @@ def test_to_datetime_with_space_in_series(self, cache): result_coerce = to_datetime(ser, errors="coerce", cache=cache) expected_coerce = Series([datetime(2006, 10, 18), datetime(2008, 10, 18), NaT]) tm.assert_series_equal(result_coerce, expected_coerce) - result_ignore = to_datetime(ser, errors="ignore", cache=cache) - tm.assert_series_equal(result_ignore, ser) @td.skip_if_not_us_locale def test_to_datetime_with_apply(self, cache): @@ -2568,7 +2417,7 @@ def test_to_datetime_timezone_name(self): assert result == expected @td.skip_if_not_us_locale - @pytest.mark.parametrize("errors", ["raise", "coerce", "ignore"]) + @pytest.mark.parametrize("errors", ["raise", "coerce"]) def test_to_datetime_with_apply_with_empty_str(self, cache, errors): # this is only locale tested with US/None locales # GH 5195, GH50251 @@ -2616,19 +2465,10 @@ def test_to_datetime_strings_vs_constructor(self, result): def test_to_datetime_unprocessable_input(self, cache): # GH 4928 # GH 21864 - result = to_datetime([1, "1"], errors="ignore", cache=cache) - - expected = Index(np.array([1, "1"], dtype="O")) - tm.assert_equal(result, expected) msg = '^Given date string "1" not likely a datetime, at position 1$' with pytest.raises(ValueError, match=msg): to_datetime([1, "1"], errors="raise", cache=cache) - def test_to_datetime_unhashable_input(self, cache): - series = Series([["a"]] * 100) - result = to_datetime(series, errors="ignore", cache=cache) - tm.assert_series_equal(series, result) - def test_to_datetime_other_datetime64_units(self): # 5/25/2012 scalar = np.int64(1337904000000000).view("M8[us]") @@ -2691,11 +2531,6 @@ def test_string_na_nat_conversion_malformed(self, cache): with pytest.raises(ValueError, match=msg): to_datetime(malformed, errors="raise", cache=cache) - result = to_datetime(malformed, errors="ignore", cache=cache) - # GH 21864 - expected = Index(malformed, dtype=object) - tm.assert_index_equal(result, expected) - with pytest.raises(ValueError, match=msg): to_datetime(malformed, errors="raise", cache=cache) @@ -2968,14 +2803,6 @@ def test_to_datetime_iso8601_noleading_0s(self, cache, format): result = to_datetime(ser, format=format, cache=cache) tm.assert_series_equal(result, expected) - def test_parse_dates_infer_datetime_format_warning(self): - # GH 49024 - with tm.assert_produces_warning( - UserWarning, - match="The argument 'infer_datetime_format' is deprecated", - ): - to_datetime(["10-10-2000"], infer_datetime_format=True) - class TestDaysInMonth: # tests for issue #10154 @@ -3039,18 +2866,6 @@ def test_day_not_in_month_raise_value(self, cache, arg, format, msg): with pytest.raises(ValueError, match=msg): to_datetime(arg, errors="raise", format=format, cache=cache) - @pytest.mark.parametrize( - "expected, format", - [ - ["2015-02-29", None], - ["2015-02-29", "%Y-%m-%d"], - ["2015-04-31", "%Y-%m-%d"], - ], - ) - def test_day_not_in_month_ignore(self, cache, expected, format): - result = to_datetime(expected, errors="ignore", format=format, cache=cache) - assert result == expected - class TestDatetimeParsingWrappers: @pytest.mark.parametrize( @@ -3537,7 +3352,7 @@ def test_na_to_datetime(nulls_fixture, klass): assert result[0] is NaT -@pytest.mark.parametrize("errors", ["raise", "coerce", "ignore"]) +@pytest.mark.parametrize("errors", ["raise", "coerce"]) @pytest.mark.parametrize( "args, format", [ @@ -3598,15 +3413,6 @@ def test_to_datetime_cache_coerce_50_lines_outofbounds(series_length): tm.assert_series_equal(result1, expected1) - result2 = to_datetime(ser, errors="ignore", utc=True) - - expected2 = Series( - [datetime.fromisoformat("1446-04-12 00:00:00+00:00")] - + ([datetime.fromisoformat("1991-10-20 00:00:00+00:00")] * series_length) - ) - - tm.assert_series_equal(result2, expected2) - with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond timestamp"): to_datetime(ser, errors="raise", utc=True) @@ -3659,17 +3465,12 @@ def test_to_datetime_mixed_not_necessarily_iso8601_raise(): to_datetime(["2020-01-01", "01-01-2000"], format="ISO8601") -@pytest.mark.parametrize( - ("errors", "expected"), - [ - ("coerce", DatetimeIndex(["2020-01-01 00:00:00", NaT])), - ("ignore", Index(["2020-01-01", "01-01-2000"], dtype=object)), - ], -) -def test_to_datetime_mixed_not_necessarily_iso8601_coerce(errors, expected): +def test_to_datetime_mixed_not_necessarily_iso8601_coerce(): # https://github.com/pandas-dev/pandas/issues/50411 - result = to_datetime(["2020-01-01", "01-01-2000"], format="ISO8601", errors=errors) - tm.assert_index_equal(result, expected) + result = to_datetime( + ["2020-01-01", "01-01-2000"], format="ISO8601", errors="coerce" + ) + tm.assert_index_equal(result, DatetimeIndex(["2020-01-01 00:00:00", NaT])) def test_unknown_tz_raises(): diff --git a/pandas/tests/tools/test_to_numeric.py b/pandas/tests/tools/test_to_numeric.py index 105aaedb21b27..585b7ca94f730 100644 --- a/pandas/tests/tools/test_to_numeric.py +++ b/pandas/tests/tools/test_to_numeric.py @@ -18,7 +18,7 @@ import pandas._testing as tm -@pytest.fixture(params=[None, "ignore", "raise", "coerce"]) +@pytest.fixture(params=[None, "raise", "coerce"]) def errors(request): return request.param @@ -116,15 +116,11 @@ def test_error(data, msg): to_numeric(ser, errors="raise") -@pytest.mark.parametrize( - "errors,exp_data", [("ignore", [1, -3.14, "apple"]), ("coerce", [1, -3.14, np.nan])] -) -@pytest.mark.filterwarnings("ignore:errors='ignore' is deprecated:FutureWarning") -def test_ignore_error(errors, exp_data): +def test_ignore_error(): ser = Series([1, -3.14, "apple"]) - result = to_numeric(ser, errors=errors) + result = to_numeric(ser, errors="coerce") - expected = Series(exp_data) + expected = Series([1, -3.14, np.nan]) tm.assert_series_equal(result, expected) @@ -132,12 +128,10 @@ def test_ignore_error(errors, exp_data): "errors,exp", [ ("raise", 'Unable to parse string "apple" at position 2'), - ("ignore", [True, False, "apple"]), # Coerces to float. ("coerce", [1.0, 0.0, np.nan]), ], ) -@pytest.mark.filterwarnings("ignore:errors='ignore' is deprecated:FutureWarning") def test_bool_handling(errors, exp): ser = Series([True, False, "apple"]) @@ -236,7 +230,6 @@ def test_all_nan(): tm.assert_series_equal(result, expected) -@pytest.mark.filterwarnings("ignore:errors='ignore' is deprecated:FutureWarning") def test_type_check(errors): # see gh-11776 df = DataFrame({"a": [1, -3.14, 7], "b": ["4", "5", "6"]}) @@ -251,7 +244,6 @@ def test_scalar(val, signed, transform): assert to_numeric(transform(val)) == float(val) -@pytest.mark.filterwarnings("ignore:errors='ignore' is deprecated:FutureWarning") def test_really_large_scalar(large_val, signed, transform, errors): # see gh-24910 kwargs = {"errors": errors} if errors is not None else {} @@ -269,7 +261,6 @@ def test_really_large_scalar(large_val, signed, transform, errors): tm.assert_almost_equal(to_numeric(val, **kwargs), expected) -@pytest.mark.filterwarnings("ignore:errors='ignore' is deprecated:FutureWarning") def test_really_large_in_arr(large_val, signed, transform, multiple_elts, errors): # see gh-24910 kwargs = {"errors": errors} if errors is not None else {} @@ -309,7 +300,6 @@ def test_really_large_in_arr(large_val, signed, transform, multiple_elts, errors tm.assert_almost_equal(result, np.array(expected, dtype=exp_dtype)) -@pytest.mark.filterwarnings("ignore:errors='ignore' is deprecated:FutureWarning") def test_really_large_in_arr_consistent(large_val, signed, multiple_elts, errors): # see gh-24910 # @@ -329,13 +319,8 @@ def test_really_large_in_arr_consistent(large_val, signed, multiple_elts, errors to_numeric(arr, **kwargs) else: result = to_numeric(arr, **kwargs) - - if errors == "coerce": - expected = [float(i) for i in arr] - exp_dtype = float - else: - expected = arr - exp_dtype = object + expected = [float(i) for i in arr] + exp_dtype = float tm.assert_almost_equal(result, np.array(expected, dtype=exp_dtype)) @@ -344,11 +329,9 @@ def test_really_large_in_arr_consistent(large_val, signed, multiple_elts, errors "errors,checker", [ ("raise", 'Unable to parse string "fail" at position 0'), - ("ignore", lambda x: x == "fail"), ("coerce", lambda x: np.isnan(x)), ], ) -@pytest.mark.filterwarnings("ignore:errors='ignore' is deprecated:FutureWarning") def test_scalar_fail(errors, checker): scalar = "fail" @@ -420,11 +403,9 @@ def test_period(request, transform_assert_equal): "errors,expected", [ ("raise", "Invalid object type at position 0"), - ("ignore", Series([[10.0, 2], 1.0, "apple"])), ("coerce", Series([np.nan, 1.0, np.nan])), ], ) -@pytest.mark.filterwarnings("ignore:errors='ignore' is deprecated:FutureWarning") def test_non_hashable(errors, expected): # see gh-13324 ser = Series([[10.0, 2], 1.0, "apple"]) @@ -502,19 +483,6 @@ def test_signed_downcast(data, signed_downcast): tm.assert_numpy_array_equal(res, expected) -def test_ignore_downcast_invalid_data(): - # If we can't successfully cast the given - # data to a numeric dtype, do not bother - # with the downcast parameter. - data = ["foo", 2, 3] - expected = np.array(data, dtype=object) - - msg = "errors='ignore' is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): - res = to_numeric(data, errors="ignore", downcast="unsigned") - tm.assert_numpy_array_equal(res, expected) - - def test_ignore_downcast_neg_to_unsigned(): # Cannot cast to an unsigned integer # because we have a negative number. @@ -526,7 +494,6 @@ def test_ignore_downcast_neg_to_unsigned(): # Warning in 32 bit platforms -@pytest.mark.filterwarnings("ignore:invalid value encountered in cast:RuntimeWarning") @pytest.mark.parametrize("downcast", ["integer", "signed", "unsigned"]) @pytest.mark.parametrize( "data,expected", @@ -628,26 +595,14 @@ def test_coerce_uint64_conflict(data, exp_data): tm.assert_series_equal(result, expected) -@pytest.mark.parametrize( - "errors,exp", - [ - ("ignore", Series(["12345678901234567890", "1234567890", "ITEM"])), - ("raise", "Unable to parse string"), - ], -) -@pytest.mark.filterwarnings("ignore:errors='ignore' is deprecated:FutureWarning") -def test_non_coerce_uint64_conflict(errors, exp): +def test_non_coerce_uint64_conflict(): # see gh-17007 and gh-17125 # # For completeness. ser = Series(["12345678901234567890", "1234567890", "ITEM"]) - if isinstance(exp, str): - with pytest.raises(ValueError, match=exp): - to_numeric(ser, errors=errors) - else: - result = to_numeric(ser, errors=errors) - tm.assert_series_equal(result, ser) + with pytest.raises(ValueError, match="Unable to parse string"): + to_numeric(ser, errors="raise") @pytest.mark.parametrize("dc1", ["integer", "float", "unsigned"]) @@ -757,17 +712,6 @@ def test_to_numeric_from_nullable_string_coerce(nullable_string_dtype): tm.assert_series_equal(result, expected) -def test_to_numeric_from_nullable_string_ignore(nullable_string_dtype): - # GH#52146 - values = ["a", "1"] - ser = Series(values, dtype=nullable_string_dtype) - expected = ser.copy() - msg = "errors='ignore' is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): - result = to_numeric(ser, errors="ignore") - tm.assert_series_equal(result, expected) - - @pytest.mark.parametrize( "data, input_dtype, downcast, expected_dtype", ( @@ -934,11 +878,6 @@ def test_to_numeric_dtype_backend_error(dtype_backend): with pytest.raises(ValueError, match="Unable to parse string"): to_numeric(ser, dtype_backend=dtype_backend) - msg = "errors='ignore' is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): - result = to_numeric(ser, dtype_backend=dtype_backend, errors="ignore") - tm.assert_series_equal(result, expected) - result = to_numeric(ser, dtype_backend=dtype_backend, errors="coerce") if dtype_backend == "pyarrow": dtype = "double[pyarrow]" diff --git a/pandas/tests/tools/test_to_time.py b/pandas/tests/tools/test_to_time.py index b673bd9c2ec71..5e61f5e1a3029 100644 --- a/pandas/tests/tools/test_to_time.py +++ b/pandas/tests/tools/test_to_time.py @@ -54,10 +54,8 @@ def test_arraylike(self): assert to_time(arg, infer_time_format=True) == expected_arr assert to_time(arg, format="%I:%M%p", errors="coerce") == [None, None] - msg = "errors='ignore' is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): - res = to_time(arg, format="%I:%M%p", errors="ignore") - tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_)) + with pytest.raises(ValueError, match="errors must be"): + to_time(arg, format="%I:%M%p", errors="ignore") msg = "Cannot convert.+to a time with given format" with pytest.raises(ValueError, match=msg): diff --git a/pandas/tests/tools/test_to_timedelta.py b/pandas/tests/tools/test_to_timedelta.py index 073533c38f430..c052ca58f5873 100644 --- a/pandas/tests/tools/test_to_timedelta.py +++ b/pandas/tests/tools/test_to_timedelta.py @@ -99,8 +99,7 @@ def test_to_timedelta_oob_non_nano(self): TimedeltaArray._from_sequence(arr, dtype="m8[s]") @pytest.mark.parametrize("box", [lambda x: x, pd.DataFrame]) - @pytest.mark.parametrize("errors", ["ignore", "raise", "coerce"]) - @pytest.mark.filterwarnings("ignore:errors='ignore' is deprecated:FutureWarning") + @pytest.mark.parametrize("errors", ["raise", "coerce"]) def test_to_timedelta_dataframe(self, box, errors): # GH 11776 arg = box(np.arange(10).reshape(2, 5)) @@ -145,32 +144,6 @@ def test_to_timedelta_bad_value_coerce(self): to_timedelta(["1 day", "bar", "1 min"], errors="coerce"), ) - def test_to_timedelta_invalid_errors_ignore(self): - # gh-13613: these should not error because errors='ignore' - msg = "errors='ignore' is deprecated" - invalid_data = "apple" - - with tm.assert_produces_warning(FutureWarning, match=msg): - result = to_timedelta(invalid_data, errors="ignore") - assert invalid_data == result - - invalid_data = ["apple", "1 days"] - expected = np.array(invalid_data, dtype=object) - - with tm.assert_produces_warning(FutureWarning, match=msg): - result = to_timedelta(invalid_data, errors="ignore") - tm.assert_numpy_array_equal(expected, result) - - invalid_data = pd.Index(["apple", "1 days"]) - with tm.assert_produces_warning(FutureWarning, match=msg): - result = to_timedelta(invalid_data, errors="ignore") - tm.assert_index_equal(invalid_data, result) - - invalid_data = Series(["apple", "1 days"]) - with tm.assert_produces_warning(FutureWarning, match=msg): - result = to_timedelta(invalid_data, errors="ignore") - tm.assert_series_equal(invalid_data, result) - @pytest.mark.parametrize( "val, errors", [ @@ -255,13 +228,6 @@ def test_to_timedelta_coerce_strings_unit(self): expected = to_timedelta([1, 2, pd.NaT], unit="ns") tm.assert_index_equal(result, expected) - def test_to_timedelta_ignore_strings_unit(self): - arr = np.array([1, 2, "error"], dtype=object) - msg = "errors='ignore' is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): - result = to_timedelta(arr, unit="ns", errors="ignore") - tm.assert_numpy_array_equal(result, arr) - @pytest.mark.parametrize( "expected_val, result_val", [[timedelta(days=2), 2], [None, None]] ) diff --git a/pandas/tests/tslibs/test_array_to_datetime.py b/pandas/tests/tslibs/test_array_to_datetime.py index 3798e68b3780b..f8939d1d8ccd4 100644 --- a/pandas/tests/tslibs/test_array_to_datetime.py +++ b/pandas/tests/tslibs/test_array_to_datetime.py @@ -215,20 +215,6 @@ def test_parsing_different_timezone_offsets(): assert result_tz is None -@pytest.mark.parametrize( - "data", [["-352.737091", "183.575577"], ["1", "2", "3", "4", "5"]] -) -def test_number_looking_strings_not_into_datetime(data): - # see gh-4601 - # - # These strings don't look like datetimes, so - # they shouldn't be attempted to be converted. - arr = np.array(data, dtype=object) - result, _ = tslib.array_to_datetime(arr, errors="ignore") - - tm.assert_numpy_array_equal(result, arr) - - @pytest.mark.parametrize( "invalid_date", [ @@ -266,22 +252,12 @@ def test_coerce_outside_ns_bounds_one_valid(): tm.assert_numpy_array_equal(result, expected) -@pytest.mark.parametrize("errors", ["ignore", "coerce"]) -def test_coerce_of_invalid_datetimes(errors): +def test_coerce_of_invalid_datetimes(): arr = np.array(["01-01-2013", "not_a_date", "1"], dtype=object) - kwargs = {"values": arr, "errors": errors} - - if errors == "ignore": - # Without coercing, the presence of any invalid - # dates prevents any values from being converted. - result, _ = tslib.array_to_datetime(**kwargs) - tm.assert_numpy_array_equal(result, arr) - else: # coerce. - # With coercing, the invalid dates becomes iNaT - result, _ = tslib.array_to_datetime(arr, errors="coerce") - expected = ["2013-01-01T00:00:00.000000000", iNaT, iNaT] - - tm.assert_numpy_array_equal(result, np.array(expected, dtype="M8[ns]")) + # With coercing, the invalid dates becomes iNaT + result, _ = tslib.array_to_datetime(arr, errors="coerce") + expected = ["2013-01-01T00:00:00.000000000", iNaT, iNaT] + tm.assert_numpy_array_equal(result, np.array(expected, dtype="M8[ns]")) def test_to_datetime_barely_out_of_bounds():