Skip to content

Commit 3e63e17

Browse files
jbrockmendelphofl
authored andcommitted
DEPR: enforce a bunch (pandas-dev#49552)
* DEPR: enforce a bunch * docstring fixup * disable pylint check * pylint fixup * Fix gh ref in whatsnew
1 parent 738275f commit 3e63e17

File tree

13 files changed

+76
-268
lines changed

13 files changed

+76
-268
lines changed

doc/source/whatsnew/v2.0.0.rst

+6
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,15 @@ Removal of prior version deprecations/changes
299299
- Removed deprecated :meth:`Index.is_all_dates` (:issue:`36697`)
300300
- Enforced deprecation disallowing passing a timezone-aware :class:`Timestamp` and ``dtype="datetime64[ns]"`` to :class:`Series` or :class:`DataFrame` constructors (:issue:`41555`)
301301
- Enforced deprecation disallowing passing a sequence of timezone-aware values and ``dtype="datetime64[ns]"`` to to :class:`Series` or :class:`DataFrame` constructors (:issue:`41555`)
302+
- Enforced deprecation disallowing ``numpy.ma.mrecords.MaskedRecords`` in the :class:`DataFrame` constructor; pass ``"{name: data[name] for name in data.dtype.names}`` instead (:issue:`40363`)
302303
- Enforced deprecation disallowing unit-less "datetime64" dtype in :meth:`Series.astype` and :meth:`DataFrame.astype` (:issue:`47844`)
303304
- Enforced deprecation disallowing using ``.astype`` to convert a ``datetime64[ns]`` :class:`Series`, :class:`DataFrame`, or :class:`DatetimeIndex` to timezone-aware dtype, use ``obj.tz_localize`` or ``ser.dt.tz_localize`` instead (:issue:`39258`)
304305
- Enforced deprecation disallowing using ``.astype`` to convert a timezone-aware :class:`Series`, :class:`DataFrame`, or :class:`DatetimeIndex` to timezone-naive ``datetime64[ns]`` dtype, use ``obj.tz_localize(None)`` or ``obj.tz_convert("UTC").tz_localize(None)`` instead (:issue:`39258`)
305306
- Enforced deprecation disallowing passing non boolean argument to sort in :func:`concat` (:issue:`44629`)
306307
- Removed Date parser functions :func:`~pandas.io.date_converters.parse_date_time`,
307308
:func:`~pandas.io.date_converters.parse_date_fields`, :func:`~pandas.io.date_converters.parse_all_fields`
308309
and :func:`~pandas.io.date_converters.generic_parser` (:issue:`24518`)
310+
- Removed argument ``index`` from the :class:`core.arrays.SparseArray` constructor (:issue:`43523`)
309311
- Remove argument ``squeeze`` from :meth:`DataFrame.groupby` and :meth:`Series.groupby` (:issue:`32380`)
310312
- Removed deprecated ``apply``, ``apply_index``, ``__call__``, ``onOffset``, and ``isAnchored`` attributes from :class:`DateOffset` (:issue:`34171`)
311313
- Removed ``keep_tz`` argument in :meth:`DatetimeIndex.to_series` (:issue:`29731`)
@@ -315,6 +317,7 @@ Removal of prior version deprecations/changes
315317
- Removed argument ``line_terminator`` from :meth:`DataFrame.to_csv` and :meth:`Series.to_csv`, use ``lineterminator`` instead (:issue:`45302`)
316318
- Removed argument ``inplace`` from :meth:`DataFrame.set_axis` and :meth:`Series.set_axis`, use ``obj = obj.set_axis(..., copy=False)`` instead (:issue:`48130`)
317319
- Disallow passing positional arguments to :meth:`MultiIndex.set_levels` and :meth:`MultiIndex.set_codes` (:issue:`41485`)
320+
- Disallow parsing to Timedelta strings with components with units "Y", "y", or "M", as these do not represent unambiguous durations (:issue:`36838`)
318321
- Removed :meth:`MultiIndex.is_lexsorted` and :meth:`MultiIndex.lexsort_depth` (:issue:`38701`)
319322
- Removed argument ``how`` from :meth:`PeriodIndex.astype`, use :meth:`PeriodIndex.to_timestamp` instead (:issue:`37982`)
320323
- Removed argument ``try_cast`` from :meth:`DataFrame.mask`, :meth:`DataFrame.where`, :meth:`Series.mask` and :meth:`Series.where` (:issue:`38836`)
@@ -375,6 +378,7 @@ Removal of prior version deprecations/changes
375378
- Removed :attr:`Rolling.win_type` returning ``"freq"`` (:issue:`38963`)
376379
- Removed :attr:`Rolling.is_datetimelike` (:issue:`38963`)
377380
- Removed deprecated :meth:`Timedelta.delta`, :meth:`Timedelta.is_populated`, and :attr:`Timedelta.freq` (:issue:`46430`, :issue:`46476`)
381+
- Removed deprecated :attr:`NaT.freq` (:issue:`45071`)
378382
- Removed deprecated :meth:`Categorical.replace`, use :meth:`Series.replace` instead (:issue:`44929`)
379383
- Removed the ``numeric_only`` keyword from :meth:`Categorical.min` and :meth:`Categorical.max` in favor of ``skipna`` (:issue:`48821`)
380384
- Changed behavior of :meth:`DataFrame.median` and :meth:`DataFrame.mean` with ``numeric_only=None`` to not exclude datetime-like columns THIS NOTE WILL BE IRRELEVANT ONCE ``numeric_only=None`` DEPRECATION IS ENFORCED (:issue:`29941`)
@@ -433,6 +437,7 @@ Removal of prior version deprecations/changes
433437
- Changed behavior of :class:`DataFrame` constructor when passed a ``dtype`` (other than int) that the data cannot be cast to; it now raises instead of silently ignoring the dtype (:issue:`41733`)
434438
- Changed the behavior of :class:`Series` constructor, it will no longer infer a datetime64 or timedelta64 dtype from string entries (:issue:`41731`)
435439
- Changed behavior of :class:`Timestamp` constructor with a ``np.datetime64`` object and a ``tz`` passed to interpret the input as a wall-time as opposed to a UTC time (:issue:`42288`)
440+
- Changed behavior of :meth:`Timestamp.utcfromtimestamp` to return a timezone-aware object satisfying ``Timestamp.utcfromtimestamp(val).timestamp() == val`` (:issue:`45083`)
436441
- Changed behavior of :class:`Index` constructor when passed a ``SparseArray`` or ``SparseDtype`` to retain that dtype instead of casting to ``numpy.ndarray`` (:issue:`43930`)
437442
- Changed behavior of setitem-like operations (``__setitem__``, ``fillna``, ``where``, ``mask``, ``replace``, ``insert``, fill_value for ``shift``) on an object with :class:`DatetimeTZDtype` when using a value with a non-matching timezone, the value will be cast to the object's timezone instead of casting both to object-dtype (:issue:`44243`)
438443
- Changed behavior of :class:`Index`, :class:`Series`, :class:`DataFrame` constructors with floating-dtype data and a :class:`DatetimeTZDtype`, the data are now interpreted as UTC-times instead of wall-times, consistent with how integer-dtype data are treated (:issue:`45573`)
@@ -441,6 +446,7 @@ Removal of prior version deprecations/changes
441446
- Change the default argument of ``regex`` for :meth:`Series.str.replace` from ``True`` to ``False``. Additionally, a single character ``pat`` with ``regex=True`` is now treated as a regular expression instead of a string literal. (:issue:`36695`, :issue:`24804`)
442447
- Changed behavior of :meth:`DataFrame.any` and :meth:`DataFrame.all` with ``bool_only=True``; object-dtype columns with all-bool values will no longer be included, manually cast to ``bool`` dtype first (:issue:`46188`)
443448
- Changed behavior of comparison of a :class:`Timestamp` with a ``datetime.date`` object; these now compare as un-equal and raise on inequality comparisons, matching the ``datetime.datetime`` behavior (:issue:`36131`)
449+
- Changed behavior of comparison of ``NaT`` with a ``datetime.date`` object; these now raise on inequality comparisons (:issue:`39196`)
444450
- Enforced deprecation of silently dropping columns that raised a ``TypeError`` in :class:`Series.transform` and :class:`DataFrame.transform` when used with a list or dictionary (:issue:`43740`)
445451
- Change behavior of :meth:`DataFrame.apply` with list-like so that any partial failure will raise an error (:issue:`43740`)
446452
- Removed ``na_sentinel`` argument from :func:`factorize`, :meth:`.Index.factorize`, and :meth:`.ExtensionArray.factorize` (:issue:`47157`)

pandas/_libs/tslibs/nattype.pyx

+8-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import warnings
2-
3-
from pandas.util._exceptions import find_stack_level
4-
51
from cpython.datetime cimport (
62
PyDate_Check,
73
PyDateTime_Check,
@@ -128,14 +124,7 @@ cdef class _NaT(datetime):
128124
return False
129125
if op == Py_NE:
130126
return True
131-
warnings.warn(
132-
"Comparison of NaT with datetime.date is deprecated in "
133-
"order to match the standard library behavior. "
134-
"In a future version these will be considered non-comparable.",
135-
FutureWarning,
136-
stacklevel=find_stack_level(),
137-
)
138-
return False
127+
raise TypeError("Cannot compare NaT with datetime.date object")
139128

140129
return NotImplemented
141130

@@ -374,15 +363,6 @@ class NaTType(_NaT):
374363

375364
return base
376365

377-
@property
378-
def freq(self):
379-
warnings.warn(
380-
"NaT.freq is deprecated and will be removed in a future version.",
381-
FutureWarning,
382-
stacklevel=find_stack_level(),
383-
)
384-
return None
385-
386366
def __reduce_ex__(self, protocol):
387367
# python 3.6 compat
388368
# https://bugs.python.org/issue28730
@@ -566,12 +546,17 @@ class NaTType(_NaT):
566546
"""
567547
Timestamp.utcfromtimestamp(ts)
568548
569-
Construct a naive UTC datetime from a POSIX timestamp.
549+
Construct a timezone-aware UTC datetime from a POSIX timestamp.
550+
551+
Notes
552+
-----
553+
Timestamp.utcfromtimestamp behavior differs from datetime.utcfromtimestamp
554+
in returning a timezone-aware object.
570555
571556
Examples
572557
--------
573558
>>> pd.Timestamp.utcfromtimestamp(1584199972)
574-
Timestamp('2020-03-14 15:32:52')
559+
Timestamp('2020-03-14 15:32:52+0000', tz='UTC')
575560
""",
576561
)
577562
fromtimestamp = _make_error_func(

pandas/_libs/tslibs/timedeltas.pyx

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import collections
2-
import warnings
3-
4-
from pandas.util._exceptions import find_stack_level
52

63
cimport cython
74
from cpython.object cimport (
@@ -688,11 +685,9 @@ cdef inline timedelta_from_spec(object number, object frac, object unit):
688685

689686
unit = ''.join(unit)
690687
if unit in ["M", "Y", "y"]:
691-
warnings.warn(
692-
"Units 'M', 'Y' and 'y' do not represent unambiguous "
693-
"timedelta values and will be removed in a future version.",
694-
FutureWarning,
695-
stacklevel=find_stack_level(),
688+
raise ValueError(
689+
"Units 'M', 'Y' and 'y' do not represent unambiguous timedelta "
690+
"values and are not supported."
696691
)
697692

698693
if unit == 'M':

pandas/_libs/tslibs/timestamps.pyx

+8-12
Original file line numberDiff line numberDiff line change
@@ -1303,24 +1303,20 @@ class Timestamp(_Timestamp):
13031303
"""
13041304
Timestamp.utcfromtimestamp(ts)
13051305
1306-
Construct a naive UTC datetime from a POSIX timestamp.
1306+
Construct a timezone-aware UTC datetime from a POSIX timestamp.
1307+
1308+
Notes
1309+
-----
1310+
Timestamp.utcfromtimestamp behavior differs from datetime.utcfromtimestamp
1311+
in returning a timezone-aware object.
13071312
13081313
Examples
13091314
--------
13101315
>>> pd.Timestamp.utcfromtimestamp(1584199972)
1311-
Timestamp('2020-03-14 15:32:52')
1316+
Timestamp('2020-03-14 15:32:52+0000', tz='UTC')
13121317
"""
13131318
# GH#22451
1314-
warnings.warn(
1315-
"The behavior of Timestamp.utcfromtimestamp is deprecated, in a "
1316-
"future version will return a timezone-aware Timestamp with UTC "
1317-
"timezone. To keep the old behavior, use "
1318-
"Timestamp.utcfromtimestamp(ts).tz_localize(None). "
1319-
"To get the future behavior, use Timestamp.fromtimestamp(ts, 'UTC')",
1320-
FutureWarning,
1321-
stacklevel=find_stack_level(),
1322-
)
1323-
return cls(datetime.utcfromtimestamp(ts))
1319+
return cls.fromtimestamp(ts, tz="UTC")
13241320

13251321
@classmethod
13261322
def fromtimestamp(cls, ts, tz=None):

pandas/core/arrays/sparse/array.py

+1-20
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ def __init__(
378378
self,
379379
data,
380380
sparse_index=None,
381-
index=None,
382381
fill_value=None,
383382
kind: SparseIndexKind = "integer",
384383
dtype: Dtype | None = None,
@@ -413,26 +412,8 @@ def __init__(
413412
fill_value = dtype.fill_value
414413
dtype = dtype.subtype
415414

416-
if index is not None:
417-
warnings.warn(
418-
"The index argument has been deprecated and will be "
419-
"removed in a future version. Use a function like np.full "
420-
"to construct an array with the desired repeats of the "
421-
"scalar value instead.\n\n",
422-
FutureWarning,
423-
stacklevel=find_stack_level(),
424-
)
425-
426-
if index is not None and not is_scalar(data):
427-
raise Exception("must only pass scalars with an index")
428-
429415
if is_scalar(data):
430-
if index is not None and data is None:
431-
data = np.nan
432-
433-
if index is not None:
434-
npoints = len(index)
435-
elif sparse_index is None:
416+
if sparse_index is None:
436417
npoints = 1
437418
else:
438419
npoints = sparse_index.length

pandas/core/frame.py

+13-24
Original file line numberDiff line numberDiff line change
@@ -686,33 +686,22 @@ def __init__(
686686

687687
# masked recarray
688688
if isinstance(data, mrecords.MaskedRecords):
689-
mgr = rec_array_to_mgr(
690-
data,
691-
index,
692-
columns,
693-
dtype,
694-
copy,
695-
typ=manager,
696-
)
697-
warnings.warn(
698-
"Support for MaskedRecords is deprecated and will be "
699-
"removed in a future version. Pass "
700-
"{name: data[name] for name in data.dtype.names} instead.",
701-
FutureWarning,
702-
stacklevel=find_stack_level(),
689+
raise TypeError(
690+
"MaskedRecords are not supported. Pass "
691+
"{name: data[name] for name in data.dtype.names} "
692+
"instead"
703693
)
704694

705695
# a masked array
706-
else:
707-
data = sanitize_masked_array(data)
708-
mgr = ndarray_to_mgr(
709-
data,
710-
index,
711-
columns,
712-
dtype=dtype,
713-
copy=copy,
714-
typ=manager,
715-
)
696+
data = sanitize_masked_array(data)
697+
mgr = ndarray_to_mgr(
698+
data,
699+
index,
700+
columns,
701+
dtype=dtype,
702+
copy=copy,
703+
typ=manager,
704+
)
716705

717706
elif isinstance(data, (np.ndarray, Series, Index, ExtensionArray)):
718707
if data.dtype.names:

pandas/core/internals/construction.py

+2-42
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66

77
from collections import abc
88
from typing import (
9-
TYPE_CHECKING,
109
Any,
1110
Hashable,
1211
Sequence,
13-
cast,
1412
)
1513
import warnings
1614

@@ -32,7 +30,6 @@
3230
maybe_cast_to_datetime,
3331
maybe_convert_platform,
3432
maybe_infer_to_datetimelike,
35-
maybe_upcast,
3633
)
3734
from pandas.core.dtypes.common import (
3835
is_1d_only_ea_dtype,
@@ -91,10 +88,6 @@
9188
create_block_manager_from_column_arrays,
9289
)
9390

94-
if TYPE_CHECKING:
95-
from numpy.ma.mrecords import MaskedRecords
96-
97-
9891
# ---------------------------------------------------------------------
9992
# BlockManager Interface
10093

@@ -163,7 +156,7 @@ def arrays_to_mgr(
163156

164157

165158
def rec_array_to_mgr(
166-
data: MaskedRecords | np.recarray | np.ndarray,
159+
data: np.recarray | np.ndarray,
167160
index,
168161
columns,
169162
dtype: DtypeObj | None,
@@ -184,24 +177,9 @@ def rec_array_to_mgr(
184177
columns = ensure_index(columns)
185178
arrays, arr_columns = to_arrays(fdata, columns)
186179

187-
# fill if needed
188-
if isinstance(data, np.ma.MaskedArray):
189-
# GH#42200 we only get here with MaskedRecords, but check for the
190-
# parent class MaskedArray to avoid the need to import MaskedRecords
191-
data = cast("MaskedRecords", data)
192-
new_arrays = fill_masked_arrays(data, arr_columns)
193-
else:
194-
# error: Incompatible types in assignment (expression has type
195-
# "List[ExtensionArray]", variable has type "List[ndarray]")
196-
new_arrays = arrays # type: ignore[assignment]
197-
198180
# create the manager
199181

200-
# error: Argument 1 to "reorder_arrays" has incompatible type "List[ndarray]";
201-
# expected "List[Union[ExtensionArray, ndarray]]"
202-
arrays, arr_columns = reorder_arrays(
203-
new_arrays, arr_columns, columns, len(index) # type: ignore[arg-type]
204-
)
182+
arrays, arr_columns = reorder_arrays(arrays, arr_columns, columns, len(index))
205183
if columns is None:
206184
columns = arr_columns
207185

@@ -212,24 +190,6 @@ def rec_array_to_mgr(
212190
return mgr
213191

214192

215-
def fill_masked_arrays(data: MaskedRecords, arr_columns: Index) -> list[np.ndarray]:
216-
"""
217-
Convert numpy MaskedRecords to ensure mask is softened.
218-
"""
219-
new_arrays = []
220-
221-
for col in arr_columns:
222-
arr = data[col]
223-
fv = arr.fill_value
224-
225-
mask = ma.getmaskarray(arr)
226-
if mask.any():
227-
arr, fv = maybe_upcast(arr, fill_value=fv, copy=True)
228-
arr[mask] = fv
229-
new_arrays.append(arr)
230-
return new_arrays
231-
232-
233193
def mgr_to_mgr(mgr, typ: str, copy: bool = True):
234194
"""
235195
Convert to specific type of Manager. Does not copy if the type is already

pandas/tests/arrays/sparse/test_constructors.py

-22
Original file line numberDiff line numberDiff line change
@@ -218,28 +218,6 @@ def test_from_spmatrix_raises(self):
218218
with pytest.raises(ValueError, match="not '4'"):
219219
SparseArray.from_spmatrix(mat)
220220

221-
@pytest.mark.parametrize(
222-
"scalar,dtype",
223-
[
224-
(False, SparseDtype(bool, False)),
225-
(0.0, SparseDtype("float64", 0)),
226-
(1, SparseDtype("int64", 1)),
227-
("z", SparseDtype("object", "z")),
228-
],
229-
)
230-
def test_scalar_with_index_infer_dtype(self, scalar, dtype):
231-
# GH#19163
232-
with tm.assert_produces_warning(
233-
FutureWarning, match="The index argument has been deprecated"
234-
):
235-
arr = SparseArray(scalar, index=[1, 2, 3], fill_value=scalar)
236-
exp = SparseArray([scalar, scalar, scalar], fill_value=scalar)
237-
238-
tm.assert_sp_array_equal(arr, exp)
239-
240-
assert arr.dtype == dtype
241-
assert exp.dtype == dtype
242-
243221
def test_constructor_from_too_large_array(self):
244222
with pytest.raises(TypeError, match="expected dimension <= 1 data"):
245223
SparseArray(np.arange(10).reshape((2, 5)))

0 commit comments

Comments
 (0)