Skip to content

CLN: Use ABC classes where possible #29020

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

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 10 additions & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
ABCDataFrame,
ABCDatetimeArray,
ABCDatetimeIndex,
ABCExtensionArray,
ABCIndexClass,
ABCMultiIndex,
ABCPandasArray,
Expand All @@ -65,7 +66,6 @@
from pandas.core import ops
from pandas.core.accessor import CachedAccessor
import pandas.core.algorithms as algos
from pandas.core.arrays import ExtensionArray
from pandas.core.base import IndexOpsMixin, PandasObject
import pandas.core.common as com
from pandas.core.indexers import maybe_convert_indices
Expand Down Expand Up @@ -100,7 +100,7 @@

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

Expand Down Expand Up @@ -3840,7 +3840,7 @@ def values(self):
return self._data.view(np.ndarray)

@property
def _values(self) -> Union[ExtensionArray, ABCIndexClass, np.ndarray]:
def _values(self) -> Union[ABCExtensionArray, ABCIndexClass, np.ndarray]:
# TODO(EA): remove index types as they become extension arrays
"""
The best array representation.
Expand Down Expand Up @@ -4268,7 +4268,12 @@ def _concat_same_dtype(self, to_concat, name):
Concatenate to_concat which has the same class.
"""
# must be overridden in specific classes
klasses = (ABCDatetimeIndex, ABCTimedeltaIndex, ABCPeriodIndex, ExtensionArray)
klasses = (
ABCDatetimeIndex,
ABCTimedeltaIndex,
ABCPeriodIndex,
ABCExtensionArray,
)
to_concat = [
x.astype(object) if isinstance(x, klasses) else x for x in to_concat
]
Expand Down Expand Up @@ -4631,7 +4636,7 @@ def get_value(self, series, key):
# use this, e.g. DatetimeIndex
# Things like `Series._get_value` (via .at) pass the EA directly here.
s = getattr(series, "_values", series)
if isinstance(s, (ExtensionArray, Index)) and is_scalar(key):
if isinstance(s, (ABCExtensionArray, Index)) and is_scalar(key):
# GH 20882, 21257
# Unify Index and ExtensionArray treatment
# First try to convert the key to a location
Expand Down
14 changes: 8 additions & 6 deletions pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import numpy as np

from pandas import DataFrame, Index, MultiIndex, Series
from pandas.core.dtypes.generic import ABCSeries

from pandas import DataFrame, Index, MultiIndex
from pandas.core import common as com
from pandas.core.arrays.categorical import (
_factorize_from_iterable,
Expand Down Expand Up @@ -322,7 +324,7 @@ def __init__(
# consolidate data & figure out what our result ndim is going to be
ndims = set()
for obj in objs:
if not isinstance(obj, (Series, DataFrame)):
if not isinstance(obj, (ABCSeries, DataFrame)):
msg = (
"cannot concatenate object of type '{}';"
" only Series and DataFrame objs are valid".format(type(obj))
Expand All @@ -348,7 +350,7 @@ def __init__(
# filter out the empties if we have not multi-index possibilities
# note to keep empty Series as it affect to result columns / name
non_empties = [
obj for obj in objs if sum(obj.shape) > 0 or isinstance(obj, Series)
obj for obj in objs if sum(obj.shape) > 0 or isinstance(obj, ABCSeries)
]

if len(non_empties) and (
Expand All @@ -362,7 +364,7 @@ def __init__(
self.objs = objs

# Standardize axis parameter to int
if isinstance(sample, Series):
if isinstance(sample, ABCSeries):
axis = DataFrame._get_axis_number(axis)
else:
axis = sample._get_axis_number(axis)
Expand All @@ -372,7 +374,7 @@ def __init__(
if self._is_frame:
axis = 1 if axis == 0 else 0

self._is_series = isinstance(sample, Series)
self._is_series = isinstance(sample, ABCSeries)
if not 0 <= axis <= sample.ndim:
raise AssertionError(
"axis must be between 0 and {ndim}, input was"
Expand Down Expand Up @@ -545,7 +547,7 @@ def _get_concat_axis(self):
num = 0
has_names = False
for i, x in enumerate(self.objs):
if not isinstance(x, Series):
if not isinstance(x, ABCSeries):
raise TypeError(
"Cannot concatenate type 'Series' "
"with object of type {type!r}".format(type=type(x).__name__)
Expand Down