Skip to content

CLN: assorted cleanups #38275

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
Dec 3, 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
4 changes: 2 additions & 2 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ def apply_frame_axis0(object frame, object f, object names,

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

# Need to infer if low level index slider will cause segfaults
require_slow_apply = i == 0 and piece is chunk
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
import pandas.core.common as com
from pandas.core.construction import create_series_with_explicit_dtype
from pandas.core.frame import DataFrame
from pandas.core.generic import ABCDataFrame, ABCSeries, NDFrame
from pandas.core.generic import NDFrame
from pandas.core.groupby import base
from pandas.core.groupby.groupby import (
GroupBy,
Expand Down Expand Up @@ -531,7 +531,7 @@ def _transform_general(self, func, *args, **kwargs):
object.__setattr__(group, "name", name)
res = func(group, *args, **kwargs)

if isinstance(res, (ABCDataFrame, ABCSeries)):
if isinstance(res, (DataFrame, Series)):
res = res._values

results.append(klass(res, index=group.index))
Expand Down
16 changes: 0 additions & 16 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,22 +966,6 @@ def _cumcount_array(self, ascending: bool = True):
rev[sorter] = np.arange(count, dtype=np.intp)
return out[rev].astype(np.int64, copy=False)

def _transform_should_cast(self, func_nm: str) -> bool:
"""
Parameters
----------
func_nm: str
The name of the aggregation function being performed

Returns
-------
bool
Whether transform should attempt to cast the result of aggregation
"""
filled_series = self.grouper.size().fillna(0)
assert filled_series is not None
return filled_series.gt(0).any() and func_nm not in base.cython_cast_blocklist

def _cython_transform(
self, how: str, numeric_only: bool = True, axis: int = 0, **kwargs
):
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
is_scalar,
is_timedelta64_dtype,
)
from pandas.core.dtypes.generic import ABCSeries

import pandas.core.algorithms as algorithms
from pandas.core.arrays import Categorical, ExtensionArray
Expand Down Expand Up @@ -345,9 +344,7 @@ def _set_grouper(self, obj: FrameOrSeries, sort: bool = False):
if self.key is not None:
key = self.key
# The 'on' is already defined
if getattr(self.grouper, "name", None) == key and isinstance(
obj, ABCSeries
):
if getattr(self.grouper, "name", None) == key and isinstance(obj, Series):
# pandas\core\groupby\grouper.py:348: error: Item "None" of
# "Optional[Any]" has no attribute "take" [union-attr]
ax = self._grouper.take(obj.index) # type: ignore[union-attr]
Expand Down
10 changes: 3 additions & 7 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
ABCMultiIndex,
ABCPandasArray,
ABCPeriodIndex,
ABCRangeIndex,
ABCSeries,
ABCTimedeltaIndex,
)
Expand Down Expand Up @@ -3494,12 +3493,7 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
target = ensure_has_len(target) # target may be an iterator

if not isinstance(target, Index) and len(target) == 0:
values: Union[range, ExtensionArray, np.ndarray]
if isinstance(self, ABCRangeIndex):
values = range(0)
else:
values = self._data[:0] # appropriately-dtyped empty array
target = self._simple_new(values, name=self.name)
target = self[:0]
else:
target = ensure_index(target)

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

@final
def _join_level(
self, other, level, how="left", return_indexers=False, keep_order=True
):
Expand Down Expand Up @@ -3972,6 +3967,7 @@ def _get_leaf_sorter(labels):
else:
return join_index

@final
def _join_monotonic(self, other, how="left", return_indexers=False):
# We only get here with matching dtypes
assert other.dtype == self.dtype
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/indexing/test_xs.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def test_series_xs_droplevel_false(self):
mi = MultiIndex.from_tuples(
[("a", "x"), ("a", "y"), ("b", "x")], names=["level1", "level2"]
)
df = Series([1, 1, 1], index=mi)
result = df.xs("a", axis=0, drop_level=False)
ser = Series([1, 1, 1], index=mi)
result = ser.xs("a", axis=0, drop_level=False)
expected = Series(
[1, 1],
index=MultiIndex.from_tuples(
Expand Down