Skip to content

Commit 2534066

Browse files
jbrockmendelquintusdias
authored andcommitted
CLN: more assorted cleanups (pandas-dev#27555)
1 parent 7bc7b0c commit 2534066

File tree

10 files changed

+37
-85
lines changed

10 files changed

+37
-85
lines changed

pandas/_libs/missing.pyx

+2-5
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,8 @@ cpdef bint checknull_old(object val):
8080

8181

8282
cdef inline bint _check_none_nan_inf_neginf(object val):
83-
try:
84-
return val is None or (isinstance(val, float) and
85-
(val != val or val == INF or val == NEGINF))
86-
except ValueError:
87-
return False
83+
return val is None or (isinstance(val, float) and
84+
(val != val or val == INF or val == NEGINF))
8885

8986

9087
@cython.wraparound(False)

pandas/core/arrays/categorical.py

+1-36
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,7 @@
3838
is_timedelta64_dtype,
3939
)
4040
from pandas.core.dtypes.dtypes import CategoricalDtype
41-
from pandas.core.dtypes.generic import (
42-
ABCCategoricalIndex,
43-
ABCDataFrame,
44-
ABCIndexClass,
45-
ABCSeries,
46-
)
41+
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
4742
from pandas.core.dtypes.inference import is_hashable
4843
from pandas.core.dtypes.missing import isna, notna
4944

@@ -166,19 +161,6 @@ def f(self, other):
166161
return f
167162

168163

169-
def _maybe_to_categorical(array):
170-
"""
171-
Coerce to a categorical if a series is given.
172-
173-
Internal use ONLY.
174-
"""
175-
if isinstance(array, (ABCSeries, ABCCategoricalIndex)):
176-
return array._values
177-
elif isinstance(array, np.ndarray):
178-
return Categorical(array)
179-
return array
180-
181-
182164
def contains(cat, key, container):
183165
"""
184166
Helper for membership check for ``key`` in ``cat``.
@@ -1988,23 +1970,6 @@ def take_nd(self, indexer, allow_fill=None, fill_value=None):
19881970

19891971
take = take_nd
19901972

1991-
def _slice(self, slicer):
1992-
"""
1993-
Return a slice of myself.
1994-
1995-
For internal compatibility with numpy arrays.
1996-
"""
1997-
1998-
# only allow 1 dimensional slicing, but can
1999-
# in a 2-d case be passd (slice(None),....)
2000-
if isinstance(slicer, tuple) and len(slicer) == 2:
2001-
if not com.is_null_slice(slicer[0]):
2002-
raise AssertionError("invalid slicing for a 1-ndim " "categorical")
2003-
slicer = slicer[1]
2004-
2005-
codes = self._codes[slicer]
2006-
return self._constructor(values=codes, dtype=self.dtype, fastpath=True)
2007-
20081973
def __len__(self):
20091974
"""
20101975
The length of this Categorical.

pandas/core/arrays/sparse.py

-13
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,6 @@ def __init__(
601601
dtype=None,
602602
copy=False,
603603
):
604-
from pandas.core.internals import SingleBlockManager
605-
606-
if isinstance(data, SingleBlockManager):
607-
data = data.internal_values()
608604

609605
if fill_value is None and isinstance(dtype, SparseDtype):
610606
fill_value = dtype.fill_value
@@ -1859,15 +1855,6 @@ def _formatter(self, boxed=False):
18591855
SparseArray._add_unary_ops()
18601856

18611857

1862-
def _maybe_to_dense(obj):
1863-
"""
1864-
try to convert to dense
1865-
"""
1866-
if hasattr(obj, "to_dense"):
1867-
return obj.to_dense()
1868-
return obj
1869-
1870-
18711858
def make_sparse(arr, kind="block", fill_value=None, dtype=None, copy=False):
18721859
"""
18731860
Convert ndarray to sparse format

pandas/core/generic.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -4892,12 +4892,12 @@ def sample(
48924892
if weights is not None:
48934893

48944894
# If a series, align with frame
4895-
if isinstance(weights, pd.Series):
4895+
if isinstance(weights, ABCSeries):
48964896
weights = weights.reindex(self.axes[axis])
48974897

48984898
# Strings acceptable if a dataframe and axis = 0
48994899
if isinstance(weights, str):
4900-
if isinstance(self, pd.DataFrame):
4900+
if isinstance(self, ABCDataFrame):
49014901
if axis == 0:
49024902
try:
49034903
weights = self[weights]
@@ -6628,7 +6628,7 @@ def replace(
66286628
to_replace = [to_replace]
66296629

66306630
if isinstance(to_replace, (tuple, list)):
6631-
if isinstance(self, pd.DataFrame):
6631+
if isinstance(self, ABCDataFrame):
66326632
return self.apply(
66336633
_single_replace, args=(to_replace, method, inplace, limit)
66346634
)
@@ -7421,7 +7421,7 @@ def _clip_with_one_bound(self, threshold, method, axis, inplace):
74217421
# be transformed to NDFrame from other array like structure.
74227422
if (not isinstance(threshold, ABCSeries)) and is_list_like(threshold):
74237423
if isinstance(self, ABCSeries):
7424-
threshold = pd.Series(threshold, index=self.index)
7424+
threshold = self._constructor(threshold, index=self.index)
74257425
else:
74267426
threshold = _align_method_FRAME(self, threshold, axis)
74277427
return self.where(subset, threshold, axis=axis, inplace=inplace)
@@ -7510,9 +7510,9 @@ def clip(self, lower=None, upper=None, axis=None, inplace=False, *args, **kwargs
75107510
# so ignore
75117511
# GH 19992
75127512
# numpy doesn't drop a list-like bound containing NaN
7513-
if not is_list_like(lower) and np.any(pd.isnull(lower)):
7513+
if not is_list_like(lower) and np.any(isna(lower)):
75147514
lower = None
7515-
if not is_list_like(upper) and np.any(pd.isnull(upper)):
7515+
if not is_list_like(upper) and np.any(isna(upper)):
75167516
upper = None
75177517

75187518
# GH 2747 (arguments were reversed)
@@ -8985,7 +8985,7 @@ def _where(
89858985

89868986
msg = "Boolean array expected for the condition, not {dtype}"
89878987

8988-
if not isinstance(cond, pd.DataFrame):
8988+
if not isinstance(cond, ABCDataFrame):
89898989
# This is a single-dimensional object.
89908990
if not is_bool_dtype(cond):
89918991
raise ValueError(msg.format(dtype=cond.dtype))

pandas/core/groupby/generic.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
is_object_dtype,
3636
is_scalar,
3737
)
38-
from pandas.core.dtypes.missing import isna, notna
38+
from pandas.core.dtypes.missing import _isna_ndarraylike, isna, notna
3939

4040
from pandas._typing import FrameOrSeries
4141
import pandas.core.algorithms as algorithms
@@ -44,8 +44,13 @@
4444
from pandas.core.frame import DataFrame
4545
from pandas.core.generic import ABCDataFrame, ABCSeries, NDFrame, _shared_docs
4646
from pandas.core.groupby import base
47-
from pandas.core.groupby.groupby import GroupBy, _apply_docs, _transform_template
48-
from pandas.core.index import Index, MultiIndex
47+
from pandas.core.groupby.groupby import (
48+
GroupBy,
49+
_apply_docs,
50+
_transform_template,
51+
groupby,
52+
)
53+
from pandas.core.index import Index, MultiIndex, _all_indexes_same
4954
import pandas.core.indexes.base as ibase
5055
from pandas.core.internals import BlockManager, make_block
5156
from pandas.core.series import Series
@@ -162,8 +167,6 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True, min_count=-1):
162167
continue
163168

164169
# call our grouper again with only this block
165-
from pandas.core.groupby.groupby import groupby
166-
167170
obj = self.obj[data.items[locs]]
168171
s = groupby(obj, self.grouper)
169172
try:
@@ -348,8 +351,6 @@ def _decide_output_index(self, output, labels):
348351
return output_keys
349352

350353
def _wrap_applied_output(self, keys, values, not_indexed_same=False):
351-
from pandas.core.index import _all_indexes_same
352-
353354
if len(keys) == 0:
354355
return DataFrame(index=keys)
355356

@@ -1590,13 +1591,14 @@ def count(self):
15901591
DataFrame
15911592
Count of values within each group.
15921593
"""
1593-
from pandas.core.dtypes.missing import _isna_ndarraylike as _isna
1594-
15951594
data, _ = self._get_data_to_aggregate()
15961595
ids, _, ngroups = self.grouper.group_info
15971596
mask = ids != -1
15981597

1599-
val = ((mask & ~_isna(np.atleast_2d(blk.get_values()))) for blk in data.blocks)
1598+
val = (
1599+
(mask & ~_isna_ndarraylike(np.atleast_2d(blk.get_values())))
1600+
for blk in data.blocks
1601+
)
16001602
loc = (blk.mgr_locs for blk in data.blocks)
16011603

16021604
counter = partial(lib.count_level_2d, labels=ids, max_bin=ngroups, axis=1)

pandas/core/internals/blocks.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3074,10 +3074,10 @@ class CategoricalBlock(ExtensionBlock):
30743074
_concatenator = staticmethod(concat_categorical)
30753075

30763076
def __init__(self, values, placement, ndim=None):
3077-
from pandas.core.arrays.categorical import _maybe_to_categorical
3078-
30793077
# coerce to categorical if we can
3080-
super().__init__(_maybe_to_categorical(values), placement=placement, ndim=ndim)
3078+
values = extract_array(values)
3079+
assert isinstance(values, Categorical), type(values)
3080+
super().__init__(values, placement=placement, ndim=ndim)
30813081

30823082
@property
30833083
def _holder(self):

pandas/core/reshape/pivot.py

-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ def pivot_table(
127127
table = agged.unstack(to_unstack)
128128

129129
if not dropna:
130-
from pandas import MultiIndex
131-
132130
if table.index.nlevels > 1:
133131
m = MultiIndex.from_arrays(
134132
cartesian_product(table.index.levels), names=table.index.names

pandas/core/sorting.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,19 @@ def lexsort_indexer(keys, orders=None, na_position="last"):
202202

203203
# we are already a Categorical
204204
if is_categorical_dtype(key):
205-
c = key
205+
cat = key
206206

207207
# create the Categorical
208208
else:
209-
c = Categorical(key, ordered=True)
209+
cat = Categorical(key, ordered=True)
210210

211211
if na_position not in ["last", "first"]:
212212
raise ValueError("invalid na_position: {!r}".format(na_position))
213213

214-
n = len(c.categories)
215-
codes = c.codes.copy()
214+
n = len(cat.categories)
215+
codes = cat.codes.copy()
216216

217-
mask = c.codes == -1
217+
mask = cat.codes == -1
218218
if order: # ascending
219219
if na_position == "last":
220220
codes = np.where(mask, n, codes)

pandas/core/sparse/series.py

+5
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ def __init__(
114114
elif is_scalar(data) and index is not None:
115115
data = np.full(len(index), fill_value=data)
116116

117+
if isinstance(data, SingleBlockManager):
118+
# SparseArray doesn't accept SingleBlockManager
119+
index = data.index
120+
data = data.blocks[0].values
121+
117122
super().__init__(
118123
SparseArray(
119124
data,

pandas/core/window.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import pandas.core.common as com
4141
from pandas.core.generic import _shared_docs
4242
from pandas.core.groupby.base import GroupByMixin
43+
from pandas.core.index import Index, MultiIndex, ensure_index
4344

4445
_shared_docs = dict(**_shared_docs)
4546
_doc_template = """
@@ -281,7 +282,6 @@ def _wrap_results(self, results, blocks, obj, exclude=None) -> FrameOrSeries:
281282
"""
282283

283284
from pandas import Series, concat
284-
from pandas.core.index import ensure_index
285285

286286
final = []
287287
for result, block in zip(results, blocks):
@@ -1691,8 +1691,6 @@ def _on(self):
16911691
if self.on is None:
16921692
return self.obj.index
16931693
elif isinstance(self.obj, ABCDataFrame) and self.on in self.obj.columns:
1694-
from pandas import Index
1695-
16961694
return Index(self.obj[self.on])
16971695
else:
16981696
raise ValueError(
@@ -2670,7 +2668,7 @@ def dataframe_from_int_dict(data, frame_template):
26702668
*_prep_binary(arg1.iloc[:, i], arg2.iloc[:, j])
26712669
)
26722670

2673-
from pandas import MultiIndex, concat
2671+
from pandas import concat
26742672

26752673
result_index = arg1.index.union(arg2.index)
26762674
if len(result_index):

0 commit comments

Comments
 (0)