Skip to content

Commit 293f2fa

Browse files
authored
CLN: assorted cleanups (#37509)
1 parent a6311fb commit 293f2fa

File tree

9 files changed

+22
-40
lines changed

9 files changed

+22
-40
lines changed

pandas/core/arrays/_mixins.py

+3
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ def fillna(self: _T, value=None, method=None, limit=None) -> _T:
233233
new_values = self.copy()
234234
return new_values
235235

236+
# ------------------------------------------------------------------------
237+
# Reductions
238+
236239
def _reduce(self, name: str, skipna: bool = True, **kwargs):
237240
meth = getattr(self, name, None)
238241
if meth:

pandas/core/arrays/period.py

-2
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,6 @@ def _from_sequence(
207207
return scalars
208208

209209
periods = np.asarray(scalars, dtype=object)
210-
if copy:
211-
periods = periods.copy()
212210

213211
freq = freq or libperiod.extract_freq(periods)
214212
ordinals = libperiod.extract_ordinals(periods, freq)

pandas/core/arrays/timedeltas.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class TimedeltaArray(dtl.TimelikeOps):
120120
"ceil",
121121
]
122122

123-
# Note: ndim must be defined to ensure NaT.__richcmp(TimedeltaArray)
123+
# Note: ndim must be defined to ensure NaT.__richcmp__(TimedeltaArray)
124124
# operates pointwise.
125125

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

523-
if isinstance(other, (timedelta, np.timedelta64, Tick)):
523+
if isinstance(other, self._recognized_scalars):
524524
other = Timedelta(other)
525525
if other is NaT:
526526
# specifically timedelta64-NaT
@@ -577,7 +577,7 @@ def __truediv__(self, other):
577577
@unpack_zerodim_and_defer("__rtruediv__")
578578
def __rtruediv__(self, other):
579579
# X / timedelta is defined only for timedelta-like X
580-
if isinstance(other, (timedelta, np.timedelta64, Tick)):
580+
if isinstance(other, self._recognized_scalars):
581581
other = Timedelta(other)
582582
if other is NaT:
583583
# specifically timedelta64-NaT
@@ -620,7 +620,7 @@ def __rtruediv__(self, other):
620620
def __floordiv__(self, other):
621621

622622
if is_scalar(other):
623-
if isinstance(other, (timedelta, np.timedelta64, Tick)):
623+
if isinstance(other, self._recognized_scalars):
624624
other = Timedelta(other)
625625
if other is NaT:
626626
# treat this specifically as timedelta-NaT
@@ -684,7 +684,7 @@ def __floordiv__(self, other):
684684
def __rfloordiv__(self, other):
685685

686686
if is_scalar(other):
687-
if isinstance(other, (timedelta, np.timedelta64, Tick)):
687+
if isinstance(other, self._recognized_scalars):
688688
other = Timedelta(other)
689689
if other is NaT:
690690
# treat this specifically as timedelta-NaT
@@ -730,21 +730,21 @@ def __rfloordiv__(self, other):
730730
@unpack_zerodim_and_defer("__mod__")
731731
def __mod__(self, other):
732732
# Note: This is a naive implementation, can likely be optimized
733-
if isinstance(other, (timedelta, np.timedelta64, Tick)):
733+
if isinstance(other, self._recognized_scalars):
734734
other = Timedelta(other)
735735
return self - (self // other) * other
736736

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

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

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

760760
res1 = other // self

pandas/core/indexes/category.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def _convert_list_indexer(self, keyarr):
578578
return self.get_indexer(keyarr)
579579

580580
@doc(Index._maybe_cast_slice_bound)
581-
def _maybe_cast_slice_bound(self, label, side, kind):
581+
def _maybe_cast_slice_bound(self, label, side: str, kind):
582582
if kind == "loc":
583583
return label
584584

pandas/core/indexes/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ def _should_fallback_to_positional(self) -> bool:
831831
# positional in this case
832832
return self.dtype.subtype.kind in ["m", "M"]
833833

834-
def _maybe_cast_slice_bound(self, label, side, kind):
834+
def _maybe_cast_slice_bound(self, label, side: str, kind):
835835
return getattr(self, side)._maybe_cast_slice_bound(label, side, kind)
836836

837837
@Appender(Index._convert_list_indexer.__doc__)

pandas/core/indexes/numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def _validate_dtype(cls, dtype: Dtype) -> None:
9696
# Indexing Methods
9797

9898
@doc(Index._maybe_cast_slice_bound)
99-
def _maybe_cast_slice_bound(self, label, side, kind):
99+
def _maybe_cast_slice_bound(self, label, side: str, kind):
100100
assert kind in ["loc", "getitem", None]
101101

102102
# we will try to coerce to integers

pandas/core/indexes/timedeltas.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
from pandas.core.dtypes.common import (
1010
TD64NS_DTYPE,
11-
is_float,
12-
is_integer,
1311
is_scalar,
1412
is_timedelta64_dtype,
1513
is_timedelta64_ns_dtype,
@@ -246,7 +244,7 @@ def _maybe_cast_slice_bound(self, label, side: str, kind):
246244
return lbound
247245
else:
248246
return lbound + to_offset(parsed.resolution_string) - Timedelta(1, "ns")
249-
elif is_integer(label) or is_float(label):
247+
elif not isinstance(label, self._data._recognized_scalars):
250248
self._invalid_indexer("slice", label)
251249

252250
return label

pandas/core/sorting.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from pandas.core.construction import extract_array
3030

3131
if TYPE_CHECKING:
32+
from pandas import MultiIndex
3233
from pandas.core.indexes.base import Index
3334

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

417418

418-
def ensure_key_mapped_multiindex(index, key: Callable, level=None):
419+
def _ensure_key_mapped_multiindex(
420+
index: "MultiIndex", key: Callable, level=None
421+
) -> "MultiIndex":
419422
"""
420423
Returns a new MultiIndex in which key has been applied
421424
to all levels specified in level (or all levels if level
@@ -441,7 +444,6 @@ def ensure_key_mapped_multiindex(index, key: Callable, level=None):
441444
labels : MultiIndex
442445
Resulting MultiIndex with modified levels.
443446
"""
444-
from pandas.core.indexes.api import MultiIndex
445447

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

463-
labels = MultiIndex.from_arrays(mapped)
465+
labels = type(index).from_arrays(mapped)
464466

465467
return labels
466468

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

486488
if isinstance(values, ABCMultiIndex):
487-
return ensure_key_mapped_multiindex(values, key, level=levels)
489+
return _ensure_key_mapped_multiindex(values, key, level=levels)
488490

489491
result = key(values.copy())
490492
if len(result) != len(values):

pandas/tests/groupby/test_apply.py

-19
Original file line numberDiff line numberDiff line change
@@ -981,25 +981,6 @@ def test_apply_function_index_return(function):
981981
tm.assert_series_equal(result, expected)
982982

983983

984-
def test_apply_function_with_indexing():
985-
# GH: 33058
986-
df = DataFrame({"col1": ["A", "A", "A", "B", "B", "B"], "col2": [1, 2, 3, 4, 5, 6]})
987-
988-
def fn(x):
989-
x.col2[x.index[-1]] = 0
990-
return x.col2
991-
992-
result = df.groupby(["col1"], as_index=False).apply(fn)
993-
expected = Series(
994-
[1, 2, 0, 4, 5, 0],
995-
index=pd.MultiIndex.from_tuples(
996-
[(0, 0), (0, 1), (0, 2), (1, 3), (1, 4), (1, 5)]
997-
),
998-
name="col2",
999-
)
1000-
tm.assert_series_equal(result, expected)
1001-
1002-
1003984
def test_apply_function_with_indexing_return_column():
1004985
# GH: 7002
1005986
df = DataFrame(

0 commit comments

Comments
 (0)