Skip to content

Commit 6b21e3f

Browse files
authored
CLN: assorted cleanups (#38275)
1 parent b58880c commit 6b21e3f

File tree

6 files changed

+10
-33
lines changed

6 files changed

+10
-33
lines changed

pandas/_libs/reduction.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,9 @@ def apply_frame_axis0(object frame, object f, object names,
367367

368368
try:
369369
piece = f(chunk)
370-
except Exception:
370+
except Exception as err:
371371
# We can't be more specific without knowing something about `f`
372-
raise InvalidApply('Let this error raise above us')
372+
raise InvalidApply("Let this error raise above us") from err
373373

374374
# Need to infer if low level index slider will cause segfaults
375375
require_slow_apply = i == 0 and piece is chunk

pandas/core/groupby/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
import pandas.core.common as com
6464
from pandas.core.construction import create_series_with_explicit_dtype
6565
from pandas.core.frame import DataFrame
66-
from pandas.core.generic import ABCDataFrame, ABCSeries, NDFrame
66+
from pandas.core.generic import NDFrame
6767
from pandas.core.groupby import base
6868
from pandas.core.groupby.groupby import (
6969
GroupBy,
@@ -531,7 +531,7 @@ def _transform_general(self, func, *args, **kwargs):
531531
object.__setattr__(group, "name", name)
532532
res = func(group, *args, **kwargs)
533533

534-
if isinstance(res, (ABCDataFrame, ABCSeries)):
534+
if isinstance(res, (DataFrame, Series)):
535535
res = res._values
536536

537537
results.append(klass(res, index=group.index))

pandas/core/groupby/groupby.py

-16
Original file line numberDiff line numberDiff line change
@@ -966,22 +966,6 @@ def _cumcount_array(self, ascending: bool = True):
966966
rev[sorter] = np.arange(count, dtype=np.intp)
967967
return out[rev].astype(np.int64, copy=False)
968968

969-
def _transform_should_cast(self, func_nm: str) -> bool:
970-
"""
971-
Parameters
972-
----------
973-
func_nm: str
974-
The name of the aggregation function being performed
975-
976-
Returns
977-
-------
978-
bool
979-
Whether transform should attempt to cast the result of aggregation
980-
"""
981-
filled_series = self.grouper.size().fillna(0)
982-
assert filled_series is not None
983-
return filled_series.gt(0).any() and func_nm not in base.cython_cast_blocklist
984-
985969
def _cython_transform(
986970
self, how: str, numeric_only: bool = True, axis: int = 0, **kwargs
987971
):

pandas/core/groupby/grouper.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
is_scalar,
1919
is_timedelta64_dtype,
2020
)
21-
from pandas.core.dtypes.generic import ABCSeries
2221

2322
import pandas.core.algorithms as algorithms
2423
from pandas.core.arrays import Categorical, ExtensionArray
@@ -345,9 +344,7 @@ def _set_grouper(self, obj: FrameOrSeries, sort: bool = False):
345344
if self.key is not None:
346345
key = self.key
347346
# The 'on' is already defined
348-
if getattr(self.grouper, "name", None) == key and isinstance(
349-
obj, ABCSeries
350-
):
347+
if getattr(self.grouper, "name", None) == key and isinstance(obj, Series):
351348
# pandas\core\groupby\grouper.py:348: error: Item "None" of
352349
# "Optional[Any]" has no attribute "take" [union-attr]
353350
ax = self._grouper.take(obj.index) # type: ignore[union-attr]

pandas/core/indexes/base.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
ABCMultiIndex,
7171
ABCPandasArray,
7272
ABCPeriodIndex,
73-
ABCRangeIndex,
7473
ABCSeries,
7574
ABCTimedeltaIndex,
7675
)
@@ -3494,12 +3493,7 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
34943493
target = ensure_has_len(target) # target may be an iterator
34953494

34963495
if not isinstance(target, Index) and len(target) == 0:
3497-
values: Union[range, ExtensionArray, np.ndarray]
3498-
if isinstance(self, ABCRangeIndex):
3499-
values = range(0)
3500-
else:
3501-
values = self._data[:0] # appropriately-dtyped empty array
3502-
target = self._simple_new(values, name=self.name)
3496+
target = self[:0]
35033497
else:
35043498
target = ensure_index(target)
35053499

@@ -3829,6 +3823,7 @@ def _join_non_unique(self, other, how="left", return_indexers=False):
38293823
else:
38303824
return join_index
38313825

3826+
@final
38323827
def _join_level(
38333828
self, other, level, how="left", return_indexers=False, keep_order=True
38343829
):
@@ -3972,6 +3967,7 @@ def _get_leaf_sorter(labels):
39723967
else:
39733968
return join_index
39743969

3970+
@final
39753971
def _join_monotonic(self, other, how="left", return_indexers=False):
39763972
# We only get here with matching dtypes
39773973
assert other.dtype == self.dtype

pandas/tests/series/indexing/test_xs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def test_series_xs_droplevel_false(self):
5656
mi = MultiIndex.from_tuples(
5757
[("a", "x"), ("a", "y"), ("b", "x")], names=["level1", "level2"]
5858
)
59-
df = Series([1, 1, 1], index=mi)
60-
result = df.xs("a", axis=0, drop_level=False)
59+
ser = Series([1, 1, 1], index=mi)
60+
result = ser.xs("a", axis=0, drop_level=False)
6161
expected = Series(
6262
[1, 1],
6363
index=MultiIndex.from_tuples(

0 commit comments

Comments
 (0)