Skip to content

Commit 2ce2f4f

Browse files
kgoehner-amazonEC2 Default User
authored and
EC2 Default User
committed
CLN: Use ABC classes where possible
1 parent 9f03837 commit 2ce2f4f

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

pandas/core/indexes/base.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
ABCDataFrame,
5454
ABCDatetimeArray,
5555
ABCDatetimeIndex,
56+
ABCExtensionArray,
5657
ABCIndexClass,
5758
ABCMultiIndex,
5859
ABCPandasArray,
@@ -65,7 +66,6 @@
6566
from pandas.core import ops
6667
from pandas.core.accessor import CachedAccessor
6768
import pandas.core.algorithms as algos
68-
from pandas.core.arrays import ExtensionArray
6969
from pandas.core.base import IndexOpsMixin, PandasObject
7070
import pandas.core.common as com
7171
from pandas.core.indexers import maybe_convert_indices
@@ -100,7 +100,7 @@
100100

101101
def _make_comparison_op(op, cls):
102102
def cmp_method(self, other):
103-
if isinstance(other, (np.ndarray, Index, ABCSeries, ExtensionArray)):
103+
if isinstance(other, (np.ndarray, Index, ABCSeries, ABCExtensionArray)):
104104
if other.ndim > 0 and len(self) != len(other):
105105
raise ValueError("Lengths must match to compare")
106106

@@ -3840,7 +3840,7 @@ def values(self):
38403840
return self._data.view(np.ndarray)
38413841

38423842
@property
3843-
def _values(self) -> Union[ExtensionArray, ABCIndexClass, np.ndarray]:
3843+
def _values(self) -> Union[ABCExtensionArray, ABCIndexClass, np.ndarray]:
38443844
# TODO(EA): remove index types as they become extension arrays
38453845
"""
38463846
The best array representation.
@@ -4268,7 +4268,12 @@ def _concat_same_dtype(self, to_concat, name):
42684268
Concatenate to_concat which has the same class.
42694269
"""
42704270
# must be overridden in specific classes
4271-
klasses = (ABCDatetimeIndex, ABCTimedeltaIndex, ABCPeriodIndex, ExtensionArray)
4271+
klasses = (
4272+
ABCDatetimeIndex,
4273+
ABCTimedeltaIndex,
4274+
ABCPeriodIndex,
4275+
ABCExtensionArray,
4276+
)
42724277
to_concat = [
42734278
x.astype(object) if isinstance(x, klasses) else x for x in to_concat
42744279
]
@@ -4631,7 +4636,7 @@ def get_value(self, series, key):
46314636
# use this, e.g. DatetimeIndex
46324637
# Things like `Series._get_value` (via .at) pass the EA directly here.
46334638
s = getattr(series, "_values", series)
4634-
if isinstance(s, (ExtensionArray, Index)) and is_scalar(key):
4639+
if isinstance(s, (ABCExtensionArray, Index)) and is_scalar(key):
46354640
# GH 20882, 21257
46364641
# Unify Index and ExtensionArray treatment
46374642
# First try to convert the key to a location

pandas/core/reshape/concat.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
import numpy as np
88

9-
from pandas import DataFrame, Index, MultiIndex, Series
9+
from pandas.core.dtypes.generic import ABCSeries
10+
11+
from pandas import DataFrame, Index, MultiIndex
1012
from pandas.core import common as com
1113
from pandas.core.arrays.categorical import (
1214
_factorize_from_iterable,
@@ -322,7 +324,7 @@ def __init__(
322324
# consolidate data & figure out what our result ndim is going to be
323325
ndims = set()
324326
for obj in objs:
325-
if not isinstance(obj, (Series, DataFrame)):
327+
if not isinstance(obj, (ABCSeries, DataFrame)):
326328
msg = (
327329
"cannot concatenate object of type '{}';"
328330
" only Series and DataFrame objs are valid".format(type(obj))
@@ -348,7 +350,7 @@ def __init__(
348350
# filter out the empties if we have not multi-index possibilities
349351
# note to keep empty Series as it affect to result columns / name
350352
non_empties = [
351-
obj for obj in objs if sum(obj.shape) > 0 or isinstance(obj, Series)
353+
obj for obj in objs if sum(obj.shape) > 0 or isinstance(obj, ABCSeries)
352354
]
353355

354356
if len(non_empties) and (
@@ -362,7 +364,7 @@ def __init__(
362364
self.objs = objs
363365

364366
# Standardize axis parameter to int
365-
if isinstance(sample, Series):
367+
if isinstance(sample, ABCSeries):
366368
axis = DataFrame._get_axis_number(axis)
367369
else:
368370
axis = sample._get_axis_number(axis)
@@ -372,7 +374,7 @@ def __init__(
372374
if self._is_frame:
373375
axis = 1 if axis == 0 else 0
374376

375-
self._is_series = isinstance(sample, Series)
377+
self._is_series = isinstance(sample, ABCSeries)
376378
if not 0 <= axis <= sample.ndim:
377379
raise AssertionError(
378380
"axis must be between 0 and {ndim}, input was"
@@ -545,7 +547,7 @@ def _get_concat_axis(self):
545547
num = 0
546548
has_names = False
547549
for i, x in enumerate(self.objs):
548-
if not isinstance(x, Series):
550+
if not isinstance(x, ABCSeries):
549551
raise TypeError(
550552
"Cannot concatenate type 'Series' "
551553
"with object of type {type!r}".format(type=type(x).__name__)

0 commit comments

Comments
 (0)