Skip to content

Commit c233649

Browse files
authored
CLN: assorted (#54673)
* CLN: assorted * typo fixup
1 parent 2d5589c commit c233649

File tree

18 files changed

+62
-69
lines changed

18 files changed

+62
-69
lines changed

pandas/core/array_algos/take.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ def take_nd(
6666
"""
6767
Specialized Cython take which sets NaN values in one pass
6868
69-
This dispatches to ``take`` defined on ExtensionArrays. It does not
70-
currently dispatch to ``SparseArray.take`` for sparse ``arr``.
69+
This dispatches to ``take`` defined on ExtensionArrays.
7170
7271
Note: this function assumes that the indexer is a valid(ated) indexer with
7372
no out of bound indices.

pandas/core/arrays/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,7 @@ def _where(self, mask: npt.NDArray[np.bool_], value) -> Self:
20422042
result[~mask] = val
20432043
return result
20442044

2045+
# TODO(3.0): this can be removed once GH#33302 deprecation is enforced
20452046
def _fill_mask_inplace(
20462047
self, method: str, limit: int | None, mask: npt.NDArray[np.bool_]
20472048
) -> None:

pandas/core/arrays/categorical.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -2898,17 +2898,15 @@ def _delegate_method(self, name: str, *args, **kwargs):
28982898
# utility routines
28992899

29002900

2901-
def _get_codes_for_values(values, categories: Index) -> np.ndarray:
2901+
def _get_codes_for_values(
2902+
values: Index | Series | ExtensionArray | np.ndarray,
2903+
categories: Index,
2904+
) -> np.ndarray:
29022905
"""
29032906
utility routine to turn values into codes given the specified categories
29042907
29052908
If `values` is known to be a Categorical, use recode_for_categories instead.
29062909
"""
2907-
if values.ndim > 1:
2908-
flat = values.ravel()
2909-
codes = _get_codes_for_values(flat, categories)
2910-
return codes.reshape(values.shape)
2911-
29122910
codes = categories.get_indexer_for(values)
29132911
return coerce_indexer_dtype(codes, categories)
29142912

pandas/core/config_init.py

+1
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ def is_terminal() -> bool:
420420

421421

422422
def use_inf_as_na_cb(key) -> None:
423+
# TODO(3.0): enforcing this deprecation will close GH#52501
423424
from pandas.core.dtypes.missing import _use_inf_as_na
424425

425426
_use_inf_as_na(key)

pandas/core/dtypes/cast.py

-2
Original file line numberDiff line numberDiff line change
@@ -1707,8 +1707,6 @@ def can_hold_element(arr: ArrayLike, element: Any) -> bool:
17071707
arr._validate_setitem_value(element)
17081708
return True
17091709
except (ValueError, TypeError):
1710-
# TODO: re-use _catch_deprecated_value_error to ensure we are
1711-
# strict about what exceptions we allow through here.
17121710
return False
17131711

17141712
# This is technically incorrect, but maintains the behavior of

pandas/core/dtypes/dtypes.py

+1
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,7 @@ def __new__(cls, freq):
985985

986986
if isinstance(freq, BDay):
987987
# GH#53446
988+
# TODO(3.0): enforcing this will close GH#10575
988989
warnings.warn(
989990
"PeriodDtype[B] is deprecated and will be removed in a future "
990991
"version. Use a DatetimeIndex with freq='B' instead",

pandas/core/generic.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,6 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
255255
"_is_copy",
256256
"_name",
257257
"_metadata",
258-
"__array_struct__",
259-
"__array_interface__",
260258
"_flags",
261259
]
262260
_internal_names_set: set[str] = set(_internal_names)
@@ -6973,6 +6971,9 @@ def _pad_or_backfill(
69736971
method = clean_fill_method(method)
69746972

69756973
if not self._mgr.is_single_block and axis == 1:
6974+
# e.g. test_align_fill_method
6975+
# TODO(3.0): once downcast is removed, we can do the .T
6976+
# in all axis=1 cases, and remove axis kward from mgr.pad_or_backfill.
69766977
if inplace:
69776978
raise NotImplementedError()
69786979
result = self.T._pad_or_backfill(method=method, limit=limit).T

pandas/core/groupby/grouper.py

+2
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ def indexer(self):
441441
@final
442442
@property
443443
def obj(self):
444+
# TODO(3.0): enforcing these deprecations on Grouper should close
445+
# GH#25564, GH#41930
444446
warnings.warn(
445447
f"{type(self).__name__}.obj is deprecated and will be removed "
446448
"in a future version. Use GroupBy.indexer instead.",

pandas/core/indexes/base.py

+18-22
Original file line numberDiff line numberDiff line change
@@ -3782,17 +3782,23 @@ def get_loc(self, key):
37823782
self._check_indexing_error(key)
37833783
raise
37843784

3785-
_index_shared_docs[
3786-
"get_indexer"
3787-
] = """
3785+
@final
3786+
def get_indexer(
3787+
self,
3788+
target,
3789+
method: ReindexMethod | None = None,
3790+
limit: int | None = None,
3791+
tolerance=None,
3792+
) -> npt.NDArray[np.intp]:
3793+
"""
37883794
Compute indexer and mask for new index given the current index.
37893795
37903796
The indexer should be then used as an input to ndarray.take to align the
37913797
current data to the new index.
37923798
37933799
Parameters
37943800
----------
3795-
target : %(target_klass)s
3801+
target : Index
37963802
method : {None, 'pad'/'ffill', 'backfill'/'bfill', 'nearest'}, optional
37973803
* default: exact matches only.
37983804
* pad / ffill: find the PREVIOUS index value if no exact match.
@@ -3819,7 +3825,7 @@ def get_loc(self, key):
38193825
Integers from 0 to n - 1 indicating that the index at these
38203826
positions matches the corresponding target values. Missing values
38213827
in the target are marked by -1.
3822-
%(raises_section)s
3828+
38233829
Notes
38243830
-----
38253831
Returns -1 for unmatched values, for further explanation see the
@@ -3834,16 +3840,6 @@ def get_loc(self, key):
38343840
Notice that the return value is an array of locations in ``index``
38353841
and ``x`` is marked by -1, as it is not in ``index``.
38363842
"""
3837-
3838-
@Appender(_index_shared_docs["get_indexer"] % _index_doc_kwargs)
3839-
@final
3840-
def get_indexer(
3841-
self,
3842-
target,
3843-
method: ReindexMethod | None = None,
3844-
limit: int | None = None,
3845-
tolerance=None,
3846-
) -> npt.NDArray[np.intp]:
38473843
method = clean_reindex_fill_method(method)
38483844
orig_target = target
38493845
target = self._maybe_cast_listlike_indexer(target)
@@ -3898,7 +3894,7 @@ def get_indexer(
38983894

38993895
return ensure_platform_int(indexer)
39003896

3901-
pself, ptarget = self._maybe_promote(target)
3897+
pself, ptarget = self._maybe_downcast_for_indexing(target)
39023898
if pself is not self or ptarget is not target:
39033899
return pself.get_indexer(
39043900
ptarget, method=method, limit=limit, tolerance=tolerance
@@ -4582,7 +4578,7 @@ def join(
45824578

45834579
if not self._is_multi and not other._is_multi:
45844580
# We have specific handling for MultiIndex below
4585-
pself, pother = self._maybe_promote(other)
4581+
pself, pother = self._maybe_downcast_for_indexing(other)
45864582
if pself is not self or pother is not other:
45874583
return pself.join(
45884584
pother, how=how, level=level, return_indexers=True, sort=sort
@@ -6046,7 +6042,7 @@ def get_indexer_non_unique(
60466042
# that can be matched to Interval scalars.
60476043
return self._get_indexer_non_comparable(target, method=None, unique=False)
60486044

6049-
pself, ptarget = self._maybe_promote(target)
6045+
pself, ptarget = self._maybe_downcast_for_indexing(target)
60506046
if pself is not self or ptarget is not target:
60516047
return pself.get_indexer_non_unique(ptarget)
60526048

@@ -6062,8 +6058,8 @@ def get_indexer_non_unique(
60626058
# TODO: get_indexer has fastpaths for both Categorical-self and
60636059
# Categorical-target. Can we do something similar here?
60646060

6065-
# Note: _maybe_promote ensures we never get here with MultiIndex
6066-
# self and non-Multi target
6061+
# Note: _maybe_downcast_for_indexing ensures we never get here
6062+
# with MultiIndex self and non-Multi target
60676063
tgt_values = target._get_engine_target()
60686064
if self._is_multi and target._is_multi:
60696065
engine = self._engine
@@ -6237,7 +6233,7 @@ def _index_as_unique(self) -> bool:
62376233
_requires_unique_msg = "Reindexing only valid with uniquely valued Index objects"
62386234

62396235
@final
6240-
def _maybe_promote(self, other: Index) -> tuple[Index, Index]:
6236+
def _maybe_downcast_for_indexing(self, other: Index) -> tuple[Index, Index]:
62416237
"""
62426238
When dealing with an object-dtype Index and a non-object Index, see
62436239
if we can upcast the object-dtype one to improve performance.
@@ -6278,7 +6274,7 @@ def _maybe_promote(self, other: Index) -> tuple[Index, Index]:
62786274

62796275
if not is_object_dtype(self.dtype) and is_object_dtype(other.dtype):
62806276
# Reverse op so we dont need to re-implement on the subclasses
6281-
other, self = other._maybe_promote(self)
6277+
other, self = other._maybe_downcast_for_indexing(self)
62826278

62836279
return self, other
62846280

pandas/core/internals/blocks.py

+3-23
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
BlockValuesRefs,
2828
)
2929
from pandas._libs.missing import NA
30-
from pandas._libs.tslibs import IncompatibleFrequency
3130
from pandas._typing import (
3231
ArrayLike,
3332
AxisInt,
@@ -1731,9 +1730,7 @@ def setitem(self, indexer, value, using_cow: bool = False):
17311730

17321731
try:
17331732
values[indexer] = value
1734-
except (ValueError, TypeError) as err:
1735-
_catch_deprecated_value_error(err)
1736-
1733+
except (ValueError, TypeError):
17371734
if isinstance(self.dtype, IntervalDtype):
17381735
# see TestSetitemFloatIntervalWithIntIntervalValues
17391736
nb = self.coerce_to_target_dtype(orig_value, warn_on_upcast=True)
@@ -1776,9 +1773,7 @@ def where(
17761773

17771774
try:
17781775
res_values = arr._where(cond, other).T
1779-
except (ValueError, TypeError) as err:
1780-
_catch_deprecated_value_error(err)
1781-
1776+
except (ValueError, TypeError):
17821777
if self.ndim == 1 or self.shape[0] == 1:
17831778
if isinstance(self.dtype, IntervalDtype):
17841779
# TestSetitemFloatIntervalWithIntIntervalValues
@@ -1847,9 +1842,7 @@ def putmask(self, mask, new, using_cow: bool = False) -> list[Block]:
18471842
try:
18481843
# Caller is responsible for ensuring matching lengths
18491844
values._putmask(mask, new)
1850-
except (TypeError, ValueError) as err:
1851-
_catch_deprecated_value_error(err)
1852-
1845+
except (TypeError, ValueError):
18531846
if self.ndim == 1 or self.shape[0] == 1:
18541847
if isinstance(self.dtype, IntervalDtype):
18551848
# Discussion about what we want to support in the general
@@ -2256,19 +2249,6 @@ def is_view(self) -> bool:
22562249
return self.values._ndarray.base is not None
22572250

22582251

2259-
def _catch_deprecated_value_error(err: Exception) -> None:
2260-
"""
2261-
We catch ValueError for now, but only a specific one raised by DatetimeArray
2262-
which will no longer be raised in version 2.0.
2263-
"""
2264-
if isinstance(err, ValueError):
2265-
if isinstance(err, IncompatibleFrequency):
2266-
pass
2267-
elif "'value.closed' is" in str(err):
2268-
# IntervalDtype mismatched 'closed'
2269-
pass
2270-
2271-
22722252
class DatetimeLikeBlock(NDArrayBackedExtensionBlock):
22732253
"""Block for datetime64[ns], timedelta64[ns]."""
22742254

pandas/core/series.py

+3
Original file line numberDiff line numberDiff line change
@@ -3985,6 +3985,8 @@ def argsort(
39853985
mask = isna(values)
39863986

39873987
if mask.any():
3988+
# TODO(3.0): once this deprecation is enforced we can call
3989+
# self.array.argsort directly, which will close GH#43840
39883990
warnings.warn(
39893991
"The behavior of Series.argsort in the presence of NA values is "
39903992
"deprecated. In a future version, NA values will be ordered "
@@ -5199,6 +5201,7 @@ def info(
51995201
show_counts=show_counts,
52005202
)
52015203

5204+
# TODO(3.0): this can be removed once GH#33302 deprecation is enforced
52025205
def _replace_single(self, to_replace, method: str, inplace: bool, limit):
52035206
"""
52045207
Replaces values in a Series using the fill method specified when no

pandas/tests/arrays/string_/test_string.py

+8
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ def test_astype_roundtrip(dtype):
9494
result = casted.astype("datetime64[ns]")
9595
tm.assert_series_equal(result, ser)
9696

97+
# GH#38509 same thing for timedelta64
98+
ser2 = ser - ser.iloc[-1]
99+
casted2 = ser2.astype(dtype)
100+
assert is_dtype_equal(casted2.dtype, dtype)
101+
102+
result2 = casted2.astype(ser2.dtype)
103+
tm.assert_series_equal(result2, ser2)
104+
97105

98106
def test_add(dtype):
99107
a = pd.Series(["a", "b", "c", None, None], dtype=dtype)

pandas/tests/extension/base/reduce.py

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def test_reduce_series_boolean(self, data, all_boolean_reductions, skipna):
8383
ser = pd.Series(data)
8484

8585
if not self._supports_reduction(ser, op_name):
86+
# TODO: the message being checked here isn't actually checking anything
8687
msg = (
8788
"[Cc]annot perform|Categorical is not ordered for operation|"
8889
"does not support reduction|"
@@ -101,6 +102,7 @@ def test_reduce_series_numeric(self, data, all_numeric_reductions, skipna):
101102
ser = pd.Series(data)
102103

103104
if not self._supports_reduction(ser, op_name):
105+
# TODO: the message being checked here isn't actually checking anything
104106
msg = (
105107
"[Cc]annot perform|Categorical is not ordered for operation|"
106108
"does not support reduction|"

pandas/tests/extension/date/array.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,13 @@ def isna(self) -> np.ndarray:
176176
@classmethod
177177
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False):
178178
if isinstance(scalars, dt.date):
179-
pass
179+
raise TypeError
180180
elif isinstance(scalars, DateArray):
181-
pass
181+
if dtype is not None:
182+
return scalars.astype(dtype, copy=copy)
183+
if copy:
184+
return scalars.copy()
185+
return scalars[:]
182186
elif isinstance(scalars, np.ndarray):
183187
scalars = scalars.astype("U10") # 10 chars for yyyy-mm-dd
184188
return DateArray(scalars)

pandas/tests/extension/test_sparse.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,10 @@ def test_fillna_no_op_returns_copy(self, data, request):
220220
super().test_fillna_no_op_returns_copy(data)
221221

222222
@pytest.mark.xfail(reason="Unsupported")
223-
def test_fillna_series(self):
223+
def test_fillna_series(self, data_missing):
224224
# this one looks doable.
225+
# TODO: this fails bc we do not pass through data_missing. If we did,
226+
# the 0-fill case would xpass
225227
super().test_fillna_series()
226228

227229
def test_fillna_frame(self, data_missing):
@@ -349,7 +351,9 @@ def test_map_raises(self, data, na_action):
349351

350352
class TestCasting(BaseSparseTests, base.BaseCastingTests):
351353
@pytest.mark.xfail(raises=TypeError, reason="no sparse StringDtype")
352-
def test_astype_string(self, data):
354+
def test_astype_string(self, data, nullable_string_dtype):
355+
# TODO: this fails bc we do not pass through nullable_string_dtype;
356+
# If we did, the 0-cases would xpass
353357
super().test_astype_string(data)
354358

355359

pandas/tests/extension/test_string.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def test_groupby_extension_apply(self, data_for_grouping, groupby_apply_op):
201201

202202
class Test2DCompat(base.Dim2CompatTests):
203203
@pytest.fixture(autouse=True)
204-
def arrow_not_supported(self, data, request):
204+
def arrow_not_supported(self, data):
205205
if isinstance(data, ArrowStringArray):
206206
pytest.skip(reason="2D support not implemented for ArrowStringArray")
207207

pandas/tests/series/methods/test_reindex.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@
2525
def test_reindex(datetime_series, string_series):
2626
identity = string_series.reindex(string_series.index)
2727

28-
# __array_interface__ is not defined for older numpies
29-
# and on some pythons
30-
try:
31-
assert np.may_share_memory(string_series.index, identity.index)
32-
except AttributeError:
33-
pass
28+
assert np.may_share_memory(string_series.index, identity.index)
3429

3530
assert identity.index.is_(string_series.index)
3631
assert identity.index.identical(string_series.index)

pandas/tests/series/test_arithmetic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ class TestNamePreservation:
777777
@pytest.mark.parametrize("box", [list, tuple, np.array, Index, Series, pd.array])
778778
@pytest.mark.parametrize("flex", [True, False])
779779
def test_series_ops_name_retention(self, flex, box, names, all_binary_operators):
780-
# GH#33930 consistent name renteiton
780+
# GH#33930 consistent name-retention
781781
op = all_binary_operators
782782

783783
left = Series(range(10), name=names[0])

0 commit comments

Comments
 (0)