Skip to content

CLN/TST: address TODOs #44211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ def shift(self, periods=1, fill_value=None, axis=0):
return self._from_backing_data(new_values)

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

def __setitem__(self, key, value):
Expand Down
1 change: 0 additions & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,6 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
self = self.copy() if copy else self
result = self._set_dtype(dtype)

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

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/sparse/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def _subtype_with_str(self):

def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
# TODO for now only handle SparseDtypes and numpy dtypes => extend
# with other compatibtle extension dtypes
# with other compatible extension dtypes
if any(
isinstance(x, ExtensionDtype) and not isinstance(x, SparseDtype)
for x in dtypes
Expand Down
1 change: 0 additions & 1 deletion pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ def _filter_nodes(superclass, all_nodes=_all_nodes):
assert not intersection, _msg


# TODO: Python 3.6.2: replace Callable[..., None] with Callable[..., NoReturn]
def _node_not_implemented(node_name: str) -> Callable[..., None]:
"""
Return a function that raises a NotImplementedError with a passed node name.
Expand Down
1 change: 0 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5356,7 +5356,6 @@ def _replace_columnwise(
"""
Dispatch to Series.replace column-wise.


Parameters
----------
mapping : dict
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4974,7 +4974,7 @@ def _reindex_with_indexers(
if indexer is not None:
indexer = ensure_platform_int(indexer)

# TODO: speed up on homogeneous DataFrame objects
# TODO: speed up on homogeneous DataFrame objects (see _reindex_multi)
new_data = new_data.reindex_indexer(
index,
indexer,
Expand Down Expand Up @@ -6420,7 +6420,7 @@ def fillna(
)
elif isinstance(value, ABCDataFrame) and self.ndim == 2:

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

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1797,7 +1797,7 @@ def count(self) -> Series | DataFrame:
is_series = data.ndim == 1

def hfunc(bvalues: ArrayLike) -> ArrayLike:
# TODO(2DEA): reshape would not be necessary with 2D EAs
# TODO(EA2D): reshape would not be necessary with 2D EAs
if bvalues.ndim == 1:
# EA
masked = mask & ~isna(bvalues).reshape(1, -1)
Expand Down
12 changes: 3 additions & 9 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ def setitem(self, indexer, value):
value = np.nan

# coerce if block dtype can store value
values = self.values
values = cast(np.ndarray, self.values)
if not self._can_hold_element(value):
# current dtype cannot store value, coerce to common dtype
return self.coerce_to_target_dtype(value).setitem(indexer, value)
Expand Down Expand Up @@ -946,11 +946,7 @@ def setitem(self, indexer, value):
values[indexer] = value

else:
# error: Argument 1 to "setitem_datetimelike_compat" has incompatible type
# "Union[ndarray, ExtensionArray]"; expected "ndarray"
value = setitem_datetimelike_compat(
values, len(values[indexer]), value # type: ignore[arg-type]
)
value = setitem_datetimelike_compat(values, len(values[indexer]), value)
values[indexer] = value

if transpose:
Expand Down Expand Up @@ -1729,8 +1725,7 @@ def is_view(self) -> bool:

def setitem(self, indexer, value):
if not self._can_hold_element(value):
# TODO: general case needs casting logic.
return self.astype(_dtype_obj).setitem(indexer, value)
return self.coerce_to_target_dtype(value).setitem(indexer, value)

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

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

cond = extract_bool_array(cond)
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4528,6 +4528,9 @@ def rename(
5 3
dtype: int64
"""
if axis is not None:
axis = self._get_axis_number(axis)
Comment on lines +4531 to +4532
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbrockmendel it doesn't seem like axis is used in this function - why is this necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, makes sense - thanks for explaining!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure thing. will also add a comment to this effect in an upcoming CLN branch


if callable(index) or is_dict_like(index):
return super().rename(
index, copy=copy, inplace=inplace, level=level, errors=errors
Expand Down
11 changes: 10 additions & 1 deletion pandas/tests/arithmetic/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@ def assert_invalid_addsub_type(left, right, msg=None):
right - left


def get_expected_box(box):
"""
Get the box to use for 'expected' in a comparison operation.
"""
if box in [Index, array]:
return np.ndarray
return box


def get_upcast_box(box, vector):
"""
Given two box-types, find the one that takes priority
Given two box-types, find the one that takes priority.
"""
if box is DataFrame or isinstance(vector, DataFrame):
return DataFrame
Expand Down
28 changes: 12 additions & 16 deletions pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from pandas.tests.arithmetic.common import (
assert_invalid_addsub_type,
assert_invalid_comparison,
get_expected_box,
get_upcast_box,
)

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

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

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

xbox = box if box not in [pd.Index, pd.array] else np.ndarray
xbox = get_expected_box(box)

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

def test_dt64arr_timestamp_equality(self, box_with_array):
# GH#11034
xbox = (
box_with_array if box_with_array not in [pd.Index, pd.array] else np.ndarray
)
xbox = get_expected_box(box_with_array)

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

xbox = (
box_with_array if box_with_array not in [pd.Index, pd.array] else np.ndarray
)
xbox = get_expected_box(box_with_array)

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

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

@pytest.mark.parametrize("op", [operator.add, operator.sub])
def test_timedelta64_equal_timedelta_supported_ops(self, op):
def test_timedelta64_equal_timedelta_supported_ops(self, op, box_with_array):
ser = Series(
[
Timestamp("20130301"),
Expand All @@ -2292,6 +2287,7 @@ def test_timedelta64_equal_timedelta_supported_ops(self, op):
Timestamp("20130228 21:00:00"),
]
)
obj = box_with_array(ser)

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

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

tm.assert_series_equal(lhs, rhs)
tm.assert_equal(lhs, rhs)

def test_ops_nat_mixed_datetime64_timedelta64(self):
# GH#11349
Expand Down
20 changes: 9 additions & 11 deletions pandas/tests/arithmetic/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
import pandas._testing as tm
from pandas.core import ops
from pandas.core.arrays import TimedeltaArray
from pandas.tests.arithmetic.common import assert_invalid_comparison
from pandas.tests.arithmetic.common import (
assert_invalid_comparison,
get_expected_box,
)

# ------------------------------------------------------------------
# Comparisons
Expand All @@ -38,9 +41,7 @@ class TestPeriodArrayLikeComparisons:

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

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

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

xbox = np.ndarray if box_with_array in [pd.Index, pd.array] else box_with_array

other = other_box(pi)

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

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

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

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

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

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