Skip to content

Commit e8faf63

Browse files
authored
CLN/TST: address TODOs (#44211)
1 parent 3c09eca commit e8faf63

24 files changed

+112
-176
lines changed

pandas/core/arrays/_mixins.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ def shift(self, periods=1, fill_value=None, axis=0):
197197
return self._from_backing_data(new_values)
198198

199199
def _validate_shift_value(self, fill_value):
200-
# TODO: after deprecation in datetimelikearraymixin is enforced,
201-
# we can remove this and ust validate_fill_value directly
200+
# TODO(2.0): after deprecation in datetimelikearraymixin is enforced,
201+
# we can remove this and use validate_fill_value directly
202202
return self._validate_scalar(fill_value)
203203

204204
def __setitem__(self, key, value):

pandas/core/arrays/categorical.py

-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,6 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
524524
self = self.copy() if copy else self
525525
result = self._set_dtype(dtype)
526526

527-
# TODO: consolidate with ndarray case?
528527
elif isinstance(dtype, ExtensionDtype):
529528
return super().astype(dtype, copy=copy)
530529

pandas/core/arrays/sparse/dtype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ def _subtype_with_str(self):
371371

372372
def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
373373
# TODO for now only handle SparseDtypes and numpy dtypes => extend
374-
# with other compatibtle extension dtypes
374+
# with other compatible extension dtypes
375375
if any(
376376
isinstance(x, ExtensionDtype) and not isinstance(x, SparseDtype)
377377
for x in dtypes

pandas/core/computation/expr.py

-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ def _filter_nodes(superclass, all_nodes=_all_nodes):
253253
assert not intersection, _msg
254254

255255

256-
# TODO: Python 3.6.2: replace Callable[..., None] with Callable[..., NoReturn]
257256
def _node_not_implemented(node_name: str) -> Callable[..., None]:
258257
"""
259258
Return a function that raises a NotImplementedError with a passed node name.

pandas/core/frame.py

-1
Original file line numberDiff line numberDiff line change
@@ -5356,7 +5356,6 @@ def _replace_columnwise(
53565356
"""
53575357
Dispatch to Series.replace column-wise.
53585358
5359-
53605359
Parameters
53615360
----------
53625361
mapping : dict

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4974,7 +4974,7 @@ def _reindex_with_indexers(
49744974
if indexer is not None:
49754975
indexer = ensure_platform_int(indexer)
49764976

4977-
# TODO: speed up on homogeneous DataFrame objects
4977+
# TODO: speed up on homogeneous DataFrame objects (see _reindex_multi)
49784978
new_data = new_data.reindex_indexer(
49794979
index,
49804980
indexer,
@@ -6420,7 +6420,7 @@ def fillna(
64206420
)
64216421
elif isinstance(value, ABCDataFrame) and self.ndim == 2:
64226422

6423-
new_data = self.where(self.notna(), value)._data
6423+
new_data = self.where(self.notna(), value)._mgr
64246424
else:
64256425
raise ValueError(f"invalid fill value with a {type(value)}")
64266426

pandas/core/groupby/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,7 @@ def count(self) -> Series | DataFrame:
17971797
is_series = data.ndim == 1
17981798

17991799
def hfunc(bvalues: ArrayLike) -> ArrayLike:
1800-
# TODO(2DEA): reshape would not be necessary with 2D EAs
1800+
# TODO(EA2D): reshape would not be necessary with 2D EAs
18011801
if bvalues.ndim == 1:
18021802
# EA
18031803
masked = mask & ~isna(bvalues).reshape(1, -1)

pandas/core/internals/blocks.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ def setitem(self, indexer, value):
917917
value = np.nan
918918

919919
# coerce if block dtype can store value
920-
values = self.values
920+
values = cast(np.ndarray, self.values)
921921
if not self._can_hold_element(value):
922922
# current dtype cannot store value, coerce to common dtype
923923
return self.coerce_to_target_dtype(value).setitem(indexer, value)
@@ -946,11 +946,7 @@ def setitem(self, indexer, value):
946946
values[indexer] = value
947947

948948
else:
949-
# error: Argument 1 to "setitem_datetimelike_compat" has incompatible type
950-
# "Union[ndarray, ExtensionArray]"; expected "ndarray"
951-
value = setitem_datetimelike_compat(
952-
values, len(values[indexer]), value # type: ignore[arg-type]
953-
)
949+
value = setitem_datetimelike_compat(values, len(values[indexer]), value)
954950
values[indexer] = value
955951

956952
if transpose:
@@ -1729,8 +1725,7 @@ def is_view(self) -> bool:
17291725

17301726
def setitem(self, indexer, value):
17311727
if not self._can_hold_element(value):
1732-
# TODO: general case needs casting logic.
1733-
return self.astype(_dtype_obj).setitem(indexer, value)
1728+
return self.coerce_to_target_dtype(value).setitem(indexer, value)
17341729

17351730
values = self.values
17361731
if self.ndim > 1:
@@ -1751,7 +1746,6 @@ def putmask(self, mask, new) -> list[Block]:
17511746
return [self]
17521747

17531748
def where(self, other, cond, errors="raise") -> list[Block]:
1754-
# TODO(EA2D): reshape unnecessary with 2D EAs
17551749
arr = self.values
17561750

17571751
cond = extract_bool_array(cond)

pandas/core/series.py

+3
Original file line numberDiff line numberDiff line change
@@ -4528,6 +4528,9 @@ def rename(
45284528
5 3
45294529
dtype: int64
45304530
"""
4531+
if axis is not None:
4532+
axis = self._get_axis_number(axis)
4533+
45314534
if callable(index) or is_dict_like(index):
45324535
return super().rename(
45334536
index, copy=copy, inplace=inplace, level=level, errors=errors

pandas/tests/arithmetic/common.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,18 @@ def assert_invalid_addsub_type(left, right, msg=None):
3434
right - left
3535

3636

37+
def get_expected_box(box):
38+
"""
39+
Get the box to use for 'expected' in a comparison operation.
40+
"""
41+
if box in [Index, array]:
42+
return np.ndarray
43+
return box
44+
45+
3746
def get_upcast_box(box, vector):
3847
"""
39-
Given two box-types, find the one that takes priority
48+
Given two box-types, find the one that takes priority.
4049
"""
4150
if box is DataFrame or isinstance(vector, DataFrame):
4251
return DataFrame

pandas/tests/arithmetic/test_datetime64.py

+12-16
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from pandas.tests.arithmetic.common import (
4444
assert_invalid_addsub_type,
4545
assert_invalid_comparison,
46+
get_expected_box,
4647
get_upcast_box,
4748
)
4849

@@ -59,9 +60,7 @@ def test_compare_zerodim(self, tz_naive_fixture, box_with_array):
5960
# Test comparison with zero-dimensional array is unboxed
6061
tz = tz_naive_fixture
6162
box = box_with_array
62-
xbox = (
63-
box_with_array if box_with_array not in [pd.Index, pd.array] else np.ndarray
64-
)
63+
xbox = get_expected_box(box)
6564
dti = date_range("20130101", periods=3, tz=tz)
6665

6766
other = np.array(dti.to_numpy()[0])
@@ -148,7 +147,7 @@ def test_dt64arr_nat_comparison(self, tz_naive_fixture, box_with_array):
148147
# GH#22242, GH#22163 DataFrame considered NaT == ts incorrectly
149148
tz = tz_naive_fixture
150149
box = box_with_array
151-
xbox = box if box not in [pd.Index, pd.array] else np.ndarray
150+
xbox = get_expected_box(box)
152151

153152
ts = Timestamp.now(tz)
154153
ser = Series([ts, NaT])
@@ -245,7 +244,7 @@ def test_nat_comparisons_scalar(self, dtype, data, box_with_array):
245244
# on older numpys (since they check object identity)
246245
return
247246

248-
xbox = box if box not in [pd.Index, pd.array] else np.ndarray
247+
xbox = get_expected_box(box)
249248

250249
left = Series(data, dtype=dtype)
251250
left = tm.box_expected(left, box)
@@ -324,9 +323,7 @@ def test_timestamp_compare_series(self, left, right):
324323

325324
def test_dt64arr_timestamp_equality(self, box_with_array):
326325
# GH#11034
327-
xbox = (
328-
box_with_array if box_with_array not in [pd.Index, pd.array] else np.ndarray
329-
)
326+
xbox = get_expected_box(box_with_array)
330327

331328
ser = Series([Timestamp("2000-01-29 01:59:00"), Timestamp("2000-01-30"), NaT])
332329
ser = tm.box_expected(ser, box_with_array)
@@ -424,9 +421,7 @@ def test_dti_cmp_nat(self, dtype, box_with_array):
424421
# on older numpys (since they check object identity)
425422
return
426423

427-
xbox = (
428-
box_with_array if box_with_array not in [pd.Index, pd.array] else np.ndarray
429-
)
424+
xbox = get_expected_box(box_with_array)
430425

431426
left = DatetimeIndex([Timestamp("2011-01-01"), NaT, Timestamp("2011-01-03")])
432427
right = DatetimeIndex([NaT, NaT, Timestamp("2011-01-03")])
@@ -662,7 +657,7 @@ def test_scalar_comparison_tzawareness(
662657
box = box_with_array
663658
tz = tz_aware_fixture
664659
dti = date_range("2016-01-01", periods=2, tz=tz)
665-
xbox = box if box not in [pd.Index, pd.array] else np.ndarray
660+
xbox = get_expected_box(box)
666661

667662
dtarr = tm.box_expected(dti, box_with_array)
668663
if op in [operator.eq, operator.ne]:
@@ -2283,7 +2278,7 @@ def test_sub_dti_dti(self):
22832278
# cleanup, box-parametrization, and de-duplication
22842279

22852280
@pytest.mark.parametrize("op", [operator.add, operator.sub])
2286-
def test_timedelta64_equal_timedelta_supported_ops(self, op):
2281+
def test_timedelta64_equal_timedelta_supported_ops(self, op, box_with_array):
22872282
ser = Series(
22882283
[
22892284
Timestamp("20130301"),
@@ -2292,6 +2287,7 @@ def test_timedelta64_equal_timedelta_supported_ops(self, op):
22922287
Timestamp("20130228 21:00:00"),
22932288
]
22942289
)
2290+
obj = box_with_array(ser)
22952291

22962292
intervals = ["D", "h", "m", "s", "us"]
22972293

@@ -2302,10 +2298,10 @@ def timedelta64(*args):
23022298
for d, h, m, s, us in product(*([range(2)] * 5)):
23032299
nptd = timedelta64(d, h, m, s, us)
23042300
pytd = timedelta(days=d, hours=h, minutes=m, seconds=s, microseconds=us)
2305-
lhs = op(ser, nptd)
2306-
rhs = op(ser, pytd)
2301+
lhs = op(obj, nptd)
2302+
rhs = op(obj, pytd)
23072303

2308-
tm.assert_series_equal(lhs, rhs)
2304+
tm.assert_equal(lhs, rhs)
23092305

23102306
def test_ops_nat_mixed_datetime64_timedelta64(self):
23112307
# GH#11349

pandas/tests/arithmetic/test_period.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
import pandas._testing as tm
2626
from pandas.core import ops
2727
from pandas.core.arrays import TimedeltaArray
28-
from pandas.tests.arithmetic.common import assert_invalid_comparison
28+
from pandas.tests.arithmetic.common import (
29+
assert_invalid_comparison,
30+
get_expected_box,
31+
)
2932

3033
# ------------------------------------------------------------------
3134
# Comparisons
@@ -38,9 +41,7 @@ class TestPeriodArrayLikeComparisons:
3841

3942
def test_compare_zerodim(self, box_with_array):
4043
# GH#26689 make sure we unbox zero-dimensional arrays
41-
xbox = (
42-
box_with_array if box_with_array not in [pd.Index, pd.array] else np.ndarray
43-
)
44+
xbox = get_expected_box(box_with_array)
4445

4546
pi = period_range("2000", periods=4)
4647
other = np.array(pi.to_numpy()[0])
@@ -77,11 +78,10 @@ def test_compare_invalid_listlike(self, box_with_array, other):
7778

7879
@pytest.mark.parametrize("other_box", [list, np.array, lambda x: x.astype(object)])
7980
def test_compare_object_dtype(self, box_with_array, other_box):
81+
xbox = get_expected_box(box_with_array)
8082
pi = period_range("2000", periods=5)
8183
parr = tm.box_expected(pi, box_with_array)
8284

83-
xbox = np.ndarray if box_with_array in [pd.Index, pd.array] else box_with_array
84-
8585
other = other_box(pi)
8686

8787
expected = np.array([True, True, True, True, True])
@@ -187,9 +187,7 @@ def test_pi_cmp_period(self):
187187

188188
# TODO: moved from test_datetime64; de-duplicate with version below
189189
def test_parr_cmp_period_scalar2(self, box_with_array):
190-
xbox = (
191-
box_with_array if box_with_array not in [pd.Index, pd.array] else np.ndarray
192-
)
190+
xbox = get_expected_box(box_with_array)
193191

194192
pi = period_range("2000-01-01", periods=10, freq="D")
195193

@@ -210,7 +208,7 @@ def test_parr_cmp_period_scalar2(self, box_with_array):
210208
@pytest.mark.parametrize("freq", ["M", "2M", "3M"])
211209
def test_parr_cmp_period_scalar(self, freq, box_with_array):
212210
# GH#13200
213-
xbox = np.ndarray if box_with_array in [pd.Index, pd.array] else box_with_array
211+
xbox = get_expected_box(box_with_array)
214212

215213
base = PeriodIndex(["2011-01", "2011-02", "2011-03", "2011-04"], freq=freq)
216214
base = tm.box_expected(base, box_with_array)
@@ -249,7 +247,7 @@ def test_parr_cmp_period_scalar(self, freq, box_with_array):
249247
@pytest.mark.parametrize("freq", ["M", "2M", "3M"])
250248
def test_parr_cmp_pi(self, freq, box_with_array):
251249
# GH#13200
252-
xbox = np.ndarray if box_with_array in [pd.Index, pd.array] else box_with_array
250+
xbox = get_expected_box(box_with_array)
253251

254252
base = PeriodIndex(["2011-01", "2011-02", "2011-03", "2011-04"], freq=freq)
255253
base = tm.box_expected(base, box_with_array)

0 commit comments

Comments
 (0)