Skip to content

CLN: assorted cleanups #37509

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 1 commit into from
Oct 30, 2020
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
3 changes: 3 additions & 0 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ def fillna(self: _T, value=None, method=None, limit=None) -> _T:
new_values = self.copy()
return new_values

# ------------------------------------------------------------------------
# Reductions

def _reduce(self, name: str, skipna: bool = True, **kwargs):
meth = getattr(self, name, None)
if meth:
Expand Down
2 changes: 0 additions & 2 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ def _from_sequence(
return scalars

periods = np.asarray(scalars, dtype=object)
if copy:
periods = periods.copy()

freq = freq or libperiod.extract_freq(periods)
ordinals = libperiod.extract_ordinals(periods, freq)
Expand Down
18 changes: 9 additions & 9 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class TimedeltaArray(dtl.TimelikeOps):
"ceil",
]

# Note: ndim must be defined to ensure NaT.__richcmp(TimedeltaArray)
# Note: ndim must be defined to ensure NaT.__richcmp__(TimedeltaArray)
# operates pointwise.

def _box_func(self, x) -> Union[Timedelta, NaTType]:
Expand Down Expand Up @@ -520,7 +520,7 @@ def __mul__(self, other) -> "TimedeltaArray":
def __truediv__(self, other):
# timedelta / X is well-defined for timedelta-like or numeric X

if isinstance(other, (timedelta, np.timedelta64, Tick)):
if isinstance(other, self._recognized_scalars):
other = Timedelta(other)
if other is NaT:
# specifically timedelta64-NaT
Expand Down Expand Up @@ -577,7 +577,7 @@ def __truediv__(self, other):
@unpack_zerodim_and_defer("__rtruediv__")
def __rtruediv__(self, other):
# X / timedelta is defined only for timedelta-like X
if isinstance(other, (timedelta, np.timedelta64, Tick)):
if isinstance(other, self._recognized_scalars):
other = Timedelta(other)
if other is NaT:
# specifically timedelta64-NaT
Expand Down Expand Up @@ -620,7 +620,7 @@ def __rtruediv__(self, other):
def __floordiv__(self, other):

if is_scalar(other):
if isinstance(other, (timedelta, np.timedelta64, Tick)):
if isinstance(other, self._recognized_scalars):
other = Timedelta(other)
if other is NaT:
# treat this specifically as timedelta-NaT
Expand Down Expand Up @@ -684,7 +684,7 @@ def __floordiv__(self, other):
def __rfloordiv__(self, other):

if is_scalar(other):
if isinstance(other, (timedelta, np.timedelta64, Tick)):
if isinstance(other, self._recognized_scalars):
other = Timedelta(other)
if other is NaT:
# treat this specifically as timedelta-NaT
Expand Down Expand Up @@ -730,21 +730,21 @@ def __rfloordiv__(self, other):
@unpack_zerodim_and_defer("__mod__")
def __mod__(self, other):
# Note: This is a naive implementation, can likely be optimized
if isinstance(other, (timedelta, np.timedelta64, Tick)):
if isinstance(other, self._recognized_scalars):
other = Timedelta(other)
return self - (self // other) * other

@unpack_zerodim_and_defer("__rmod__")
def __rmod__(self, other):
# Note: This is a naive implementation, can likely be optimized
if isinstance(other, (timedelta, np.timedelta64, Tick)):
if isinstance(other, self._recognized_scalars):
other = Timedelta(other)
return other - (other // self) * self

@unpack_zerodim_and_defer("__divmod__")
def __divmod__(self, other):
# Note: This is a naive implementation, can likely be optimized
if isinstance(other, (timedelta, np.timedelta64, Tick)):
if isinstance(other, self._recognized_scalars):
other = Timedelta(other)

res1 = self // other
Expand All @@ -754,7 +754,7 @@ def __divmod__(self, other):
@unpack_zerodim_and_defer("__rdivmod__")
def __rdivmod__(self, other):
# Note: This is a naive implementation, can likely be optimized
if isinstance(other, (timedelta, np.timedelta64, Tick)):
if isinstance(other, self._recognized_scalars):
other = Timedelta(other)

res1 = other // self
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ def _convert_list_indexer(self, keyarr):
return self.get_indexer(keyarr)

@doc(Index._maybe_cast_slice_bound)
def _maybe_cast_slice_bound(self, label, side, kind):
def _maybe_cast_slice_bound(self, label, side: str, kind):
if kind == "loc":
return label

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ def _should_fallback_to_positional(self) -> bool:
# positional in this case
return self.dtype.subtype.kind in ["m", "M"]

def _maybe_cast_slice_bound(self, label, side, kind):
def _maybe_cast_slice_bound(self, label, side: str, kind):
return getattr(self, side)._maybe_cast_slice_bound(label, side, kind)

@Appender(Index._convert_list_indexer.__doc__)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def _validate_dtype(cls, dtype: Dtype) -> None:
# Indexing Methods

@doc(Index._maybe_cast_slice_bound)
def _maybe_cast_slice_bound(self, label, side, kind):
def _maybe_cast_slice_bound(self, label, side: str, kind):
assert kind in ["loc", "getitem", None]

# we will try to coerce to integers
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

from pandas.core.dtypes.common import (
TD64NS_DTYPE,
is_float,
is_integer,
is_scalar,
is_timedelta64_dtype,
is_timedelta64_ns_dtype,
Expand Down Expand Up @@ -246,7 +244,7 @@ def _maybe_cast_slice_bound(self, label, side: str, kind):
return lbound
else:
return lbound + to_offset(parsed.resolution_string) - Timedelta(1, "ns")
elif is_integer(label) or is_float(label):
elif not isinstance(label, self._data._recognized_scalars):
self._invalid_indexer("slice", label)

return label
Expand Down
10 changes: 6 additions & 4 deletions pandas/core/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from pandas.core.construction import extract_array

if TYPE_CHECKING:
from pandas import MultiIndex
from pandas.core.indexes.base import Index

_INT64_MAX = np.iinfo(np.int64).max
Expand Down Expand Up @@ -415,7 +416,9 @@ def nargminmax(values, method: str):
return non_nan_idx[func(non_nans)]


def ensure_key_mapped_multiindex(index, key: Callable, level=None):
def _ensure_key_mapped_multiindex(
index: "MultiIndex", key: Callable, level=None
) -> "MultiIndex":
"""
Returns a new MultiIndex in which key has been applied
to all levels specified in level (or all levels if level
Expand All @@ -441,7 +444,6 @@ def ensure_key_mapped_multiindex(index, key: Callable, level=None):
labels : MultiIndex
Resulting MultiIndex with modified levels.
"""
from pandas.core.indexes.api import MultiIndex

if level is not None:
if isinstance(level, (str, int)):
Expand All @@ -460,7 +462,7 @@ def ensure_key_mapped_multiindex(index, key: Callable, level=None):
for level in range(index.nlevels)
]

labels = MultiIndex.from_arrays(mapped)
labels = type(index).from_arrays(mapped)

return labels

Expand All @@ -484,7 +486,7 @@ def ensure_key_mapped(values, key: Optional[Callable], levels=None):
return values

if isinstance(values, ABCMultiIndex):
return ensure_key_mapped_multiindex(values, key, level=levels)
return _ensure_key_mapped_multiindex(values, key, level=levels)

result = key(values.copy())
if len(result) != len(values):
Expand Down
19 changes: 0 additions & 19 deletions pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,25 +981,6 @@ def test_apply_function_index_return(function):
tm.assert_series_equal(result, expected)


def test_apply_function_with_indexing():
# GH: 33058
df = DataFrame({"col1": ["A", "A", "A", "B", "B", "B"], "col2": [1, 2, 3, 4, 5, 6]})

def fn(x):
x.col2[x.index[-1]] = 0
return x.col2

result = df.groupby(["col1"], as_index=False).apply(fn)
expected = Series(
[1, 2, 0, 4, 5, 0],
index=pd.MultiIndex.from_tuples(
[(0, 0), (0, 1), (0, 2), (1, 3), (1, 4), (1, 5)]
),
name="col2",
)
tm.assert_series_equal(result, expected)


def test_apply_function_with_indexing_return_column():
# GH: 7002
df = DataFrame(
Expand Down