-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
PERF: use isinstance(obj, Foo) instead of ABCFoo #34040
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
jorisvandenbossche
merged 10 commits into
pandas-dev:master
from
jbrockmendel:perf-abc-frame
May 9, 2020
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
af0c51b
PERF: use faster isinstance checks
jbrockmendel 80356a4
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel 4c16955
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel 5d0a1ba
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel 154c068
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel 6ddde3a
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel 0c49925
mypy fixup
jbrockmendel 84d0a1b
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel 6fc7a57
use _typing aliases
jbrockmendel 4aa9bd5
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,12 +106,6 @@ | |
needs_i8_conversion, | ||
pandas_dtype, | ||
) | ||
from pandas.core.dtypes.generic import ( | ||
ABCDataFrame, | ||
ABCIndexClass, | ||
ABCMultiIndex, | ||
ABCSeries, | ||
) | ||
from pandas.core.dtypes.missing import isna, notna | ||
|
||
from pandas.core import algorithms, common as com, nanops, ops | ||
|
@@ -1838,7 +1832,7 @@ def to_records( | |
dtype=[('I', 'S1'), ('A', '<i8'), ('B', '<f8')]) | ||
""" | ||
if index: | ||
if isinstance(self.index, ABCMultiIndex): | ||
if isinstance(self.index, MultiIndex): | ||
# array of tuples to numpy cols. copy copy copy | ||
ix_vals = list(map(np.array, zip(*self.index.values))) | ||
else: | ||
|
@@ -1851,7 +1845,7 @@ def to_records( | |
count = 0 | ||
index_names = list(self.index.names) | ||
|
||
if isinstance(self.index, ABCMultiIndex): | ||
if isinstance(self.index, MultiIndex): | ||
for i, n in enumerate(index_names): | ||
if n is None: | ||
index_names[i] = f"level_{count}" | ||
|
@@ -2782,7 +2776,7 @@ def __getitem__(self, key): | |
# The behavior is inconsistent. It returns a Series, except when | ||
# - the key itself is repeated (test on data.shape, #9519), or | ||
# - we have a MultiIndex on columns (test on self.columns, #21309) | ||
if data.shape[1] == 1 and not isinstance(self.columns, ABCMultiIndex): | ||
if data.shape[1] == 1 and not isinstance(self.columns, MultiIndex): | ||
data = data[key] | ||
|
||
return data | ||
|
@@ -3600,7 +3594,7 @@ def reindexer(value): | |
elif isinstance(value, DataFrame): | ||
# align right-hand-side columns if self.columns | ||
# is multi-index and self[key] is a sub-frame | ||
if isinstance(self.columns, ABCMultiIndex) and key in self.columns: | ||
if isinstance(self.columns, MultiIndex) and key in self.columns: | ||
loc = self.columns.get_loc(key) | ||
if isinstance(loc, (slice, Series, np.ndarray, Index)): | ||
cols = maybe_droplevels(self.columns[loc], key) | ||
|
@@ -3649,7 +3643,7 @@ def reindexer(value): | |
|
||
# broadcast across multiple columns if necessary | ||
if broadcast and key in self.columns and value.ndim == 1: | ||
if not self.columns.is_unique or isinstance(self.columns, ABCMultiIndex): | ||
if not self.columns.is_unique or isinstance(self.columns, MultiIndex): | ||
existing_piece = self[key] | ||
if isinstance(existing_piece, DataFrame): | ||
value = np.tile(value, (len(existing_piece.columns), 1)) | ||
|
@@ -4337,9 +4331,7 @@ def set_index( | |
|
||
missing: List[Label] = [] | ||
for col in keys: | ||
if isinstance( | ||
col, (ABCIndexClass, ABCSeries, np.ndarray, list, abc.Iterator) | ||
): | ||
if isinstance(col, (Index, Series, np.ndarray, list, abc.Iterator)): | ||
# arrays are fine as long as they are one-dimensional | ||
# iterators get converted to list below | ||
if getattr(col, "ndim", 1) != 1: | ||
|
@@ -4368,19 +4360,19 @@ def set_index( | |
names = [] | ||
if append: | ||
names = list(self.index.names) | ||
if isinstance(self.index, ABCMultiIndex): | ||
if isinstance(self.index, MultiIndex): | ||
for i in range(self.index.nlevels): | ||
arrays.append(self.index._get_level_values(i)) | ||
else: | ||
arrays.append(self.index) | ||
|
||
to_remove: List[Label] = [] | ||
for col in keys: | ||
if isinstance(col, ABCMultiIndex): | ||
if isinstance(col, MultiIndex): | ||
for n in range(col.nlevels): | ||
arrays.append(col._get_level_values(n)) | ||
names.extend(col.names) | ||
elif isinstance(col, (ABCIndexClass, ABCSeries)): | ||
elif isinstance(col, (Index, Series)): | ||
# if Index then not MultiIndex (treated above) | ||
arrays.append(col) | ||
names.append(col.name) | ||
|
@@ -4625,7 +4617,7 @@ def _maybe_casted_values(index, labels=None): | |
|
||
if not drop: | ||
to_insert: Iterable[Tuple[Any, Optional[Any]]] | ||
if isinstance(self.index, ABCMultiIndex): | ||
if isinstance(self.index, MultiIndex): | ||
names = [ | ||
(n if n is not None else f"level_{i}") | ||
for i, n in enumerate(self.index.names) | ||
|
@@ -4636,7 +4628,7 @@ def _maybe_casted_values(index, labels=None): | |
names = [default] if self.index.name is None else [self.index.name] | ||
to_insert = ((self.index, None),) | ||
|
||
multi_col = isinstance(self.columns, ABCMultiIndex) | ||
multi_col = isinstance(self.columns, MultiIndex) | ||
for i, (lev, lab) in reversed(list(enumerate(to_insert))): | ||
if not (level is None or i in level): | ||
continue | ||
|
@@ -5240,7 +5232,7 @@ def sort_index( | |
level, ascending=ascending, sort_remaining=sort_remaining | ||
) | ||
|
||
elif isinstance(labels, ABCMultiIndex): | ||
elif isinstance(labels, MultiIndex): | ||
from pandas.core.sorting import lexsort_indexer | ||
|
||
indexer = lexsort_indexer( | ||
|
@@ -5607,14 +5599,14 @@ def swaplevel(self, i=-2, j=-1, axis=0) -> "DataFrame": | |
|
||
axis = self._get_axis_number(axis) | ||
|
||
if not isinstance(result._get_axis(axis), ABCMultiIndex): # pragma: no cover | ||
if not isinstance(result._get_axis(axis), MultiIndex): # pragma: no cover | ||
raise TypeError("Can only swap levels on a hierarchical axis.") | ||
|
||
if axis == 0: | ||
assert isinstance(result.index, ABCMultiIndex) | ||
assert isinstance(result.index, MultiIndex) | ||
result.index = result.index.swaplevel(i, j) | ||
else: | ||
assert isinstance(result.columns, ABCMultiIndex) | ||
assert isinstance(result.columns, MultiIndex) | ||
result.columns = result.columns.swaplevel(i, j) | ||
return result | ||
|
||
|
@@ -5635,16 +5627,16 @@ def reorder_levels(self, order, axis=0) -> "DataFrame": | |
DataFrame | ||
""" | ||
axis = self._get_axis_number(axis) | ||
if not isinstance(self._get_axis(axis), ABCMultiIndex): # pragma: no cover | ||
if not isinstance(self._get_axis(axis), MultiIndex): # pragma: no cover | ||
raise TypeError("Can only reorder levels on a hierarchical axis.") | ||
|
||
result = self.copy() | ||
|
||
if axis == 0: | ||
assert isinstance(result.index, ABCMultiIndex) | ||
assert isinstance(result.index, MultiIndex) | ||
result.index = result.index.reorder_levels(order) | ||
else: | ||
assert isinstance(result.columns, ABCMultiIndex) | ||
assert isinstance(result.columns, MultiIndex) | ||
result.columns = result.columns.reorder_levels(order) | ||
return result | ||
|
||
|
@@ -5913,7 +5905,8 @@ def extract_values(arr): | |
# Does two things: | ||
# 1. maybe gets the values from the Series / Index | ||
# 2. convert datelike to i8 | ||
if isinstance(arr, (ABCIndexClass, ABCSeries)): | ||
# TODO: extract_array? | ||
if isinstance(arr, (Index, Series)): | ||
arr = arr._values | ||
|
||
if needs_i8_conversion(arr.dtype): | ||
|
@@ -5925,7 +5918,8 @@ def extract_values(arr): | |
|
||
def combiner(x, y): | ||
mask = isna(x) | ||
if isinstance(mask, (ABCIndexClass, ABCSeries)): | ||
# TODO: extract_array? | ||
if isinstance(mask, (Index, Series)): | ||
mask = mask._values | ||
|
||
x_values = extract_values(x) | ||
|
@@ -7020,8 +7014,8 @@ def _gotitem( | |
self, | ||
key: Union[str, List[str]], | ||
ndim: int, | ||
subset: Optional[Union[Series, ABCDataFrame]] = None, | ||
) -> Union[Series, ABCDataFrame]: | ||
subset: Optional[Union[Series, "DataFrame"]] = None, | ||
) -> Union[Series, "DataFrame"]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
""" | ||
Sub-classes to define. Return a sliced object. | ||
|
||
|
@@ -8225,7 +8219,7 @@ def _count_level(self, level, axis=0, numeric_only=False): | |
count_axis = frame._get_axis(axis) | ||
agg_axis = frame._get_agg_axis(axis) | ||
|
||
if not isinstance(count_axis, ABCMultiIndex): | ||
if not isinstance(count_axis, MultiIndex): | ||
raise TypeError( | ||
f"Can only count levels on hierarchical {self._get_axis_name(axis)}." | ||
) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_typing.FrameOrSeriesUnion or _typing.FrameOrSeries?