Skip to content

Commit ac69333

Browse files
jbrockmendeljreback
authored andcommitted
CLN: collected cleanups from other branches (#27723)
1 parent 2f775b0 commit ac69333

File tree

9 files changed

+54
-31
lines changed

9 files changed

+54
-31
lines changed

pandas/_libs/index.pyx

-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ cpdef get_value_at(ndarray arr, object loc, object tz=None):
4747
return util.get_value_at(arr, loc)
4848

4949

50-
def get_value_box(arr: ndarray, loc: object) -> object:
51-
return get_value_at(arr, loc, tz=None)
52-
53-
5450
# Don't populate hash tables in monotonic indexes larger than this
5551
_SIZE_CUTOFF = 1000000
5652

pandas/core/arrays/numpy_.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ def __init__(self, values, copy=False):
125125
if isinstance(values, type(self)):
126126
values = values._ndarray
127127
if not isinstance(values, np.ndarray):
128-
raise ValueError("'values' must be a NumPy array.")
128+
raise ValueError(
129+
"'values' must be a NumPy array, not {typ}".format(
130+
typ=type(values).__name__
131+
)
132+
)
129133

130134
if values.ndim != 1:
131135
raise ValueError("PandasArray must be 1-dimensional.")

pandas/core/arrays/timedeltas.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ class TimedeltaArray(dtl.DatetimeLikeArrayMixin, dtl.TimelikeOps):
173173
"ceil",
174174
]
175175

176-
# Needed so that NaT.__richcmp__(DateTimeArray) operates pointwise
177-
ndim = 1
176+
# Note: ndim must be defined to ensure NaT.__richcmp(TimedeltaArray)
177+
# operates pointwise.
178178

179179
@property
180180
def _box_func(self):

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4714,7 +4714,7 @@ def get_value(self, series, key):
47144714
raise
47154715

47164716
try:
4717-
return libindex.get_value_box(s, key)
4717+
return libindex.get_value_at(s, key)
47184718
except IndexError:
47194719
raise
47204720
except TypeError:

pandas/core/internals/blocks.py

+38-16
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ def f(m, v, i):
434434

435435
return self.split_and_operate(mask, f, inplace)
436436

437-
def split_and_operate(self, mask, f, inplace):
437+
def split_and_operate(self, mask, f, inplace: bool):
438438
"""
439439
split the block per-column, and apply the callable f
440440
per-column, return a new block for each. Handle
@@ -493,17 +493,15 @@ def make_a_block(nv, ref_loc):
493493

494494
return new_blocks
495495

496-
def _maybe_downcast(self, blocks, downcast=None):
496+
def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"]:
497497

498498
# no need to downcast our float
499499
# unless indicated
500-
if downcast is None and self.is_float:
501-
return blocks
502-
elif downcast is None and (self.is_timedelta or self.is_datetime):
500+
if downcast is None and (
501+
self.is_float or self.is_timedelta or self.is_datetime
502+
):
503503
return blocks
504504

505-
if not isinstance(blocks, list):
506-
blocks = [blocks]
507505
return _extend_blocks([b.downcast(downcast) for b in blocks])
508506

509507
def downcast(self, dtypes=None):
@@ -1343,7 +1341,15 @@ def shift(self, periods, axis=0, fill_value=None):
13431341

13441342
return [self.make_block(new_values)]
13451343

1346-
def where(self, other, cond, align=True, errors="raise", try_cast=False, axis=0):
1344+
def where(
1345+
self,
1346+
other,
1347+
cond,
1348+
align=True,
1349+
errors="raise",
1350+
try_cast: bool = False,
1351+
axis: int = 0,
1352+
) -> List["Block"]:
13471353
"""
13481354
evaluate the block; return result block(s) from the result
13491355
@@ -1442,7 +1448,7 @@ def func(cond, values, other):
14421448
if try_cast:
14431449
result = self._try_cast_result(result)
14441450

1445-
return self.make_block(result)
1451+
return [self.make_block(result)]
14461452

14471453
# might need to separate out blocks
14481454
axis = cond.ndim - 1
@@ -1474,9 +1480,9 @@ def _unstack(self, unstacker_func, new_columns, n_rows, fill_value):
14741480
new_columns : Index
14751481
All columns of the unstacked BlockManager.
14761482
n_rows : int
1477-
Only used in ExtensionBlock.unstack
1483+
Only used in ExtensionBlock._unstack
14781484
fill_value : int
1479-
Only used in ExtensionBlock.unstack
1485+
Only used in ExtensionBlock._unstack
14801486
14811487
Returns
14821488
-------
@@ -1550,7 +1556,7 @@ def quantile(self, qs, interpolation="linear", axis=0):
15501556
result = result[..., 0]
15511557
result = lib.item_from_zerodim(result)
15521558

1553-
ndim = getattr(result, "ndim", None) or 0
1559+
ndim = np.ndim(result)
15541560
return make_block(result, placement=np.arange(len(result)), ndim=ndim)
15551561

15561562
def _replace_coerce(
@@ -1923,7 +1929,15 @@ def shift(
19231929
)
19241930
]
19251931

1926-
def where(self, other, cond, align=True, errors="raise", try_cast=False, axis=0):
1932+
def where(
1933+
self,
1934+
other,
1935+
cond,
1936+
align=True,
1937+
errors="raise",
1938+
try_cast: bool = False,
1939+
axis: int = 0,
1940+
) -> List["Block"]:
19271941
if isinstance(other, ABCDataFrame):
19281942
# ExtensionArrays are 1-D, so if we get here then
19291943
# `other` should be a DataFrame with a single column.
@@ -1968,7 +1982,7 @@ def where(self, other, cond, align=True, errors="raise", try_cast=False, axis=0)
19681982
np.where(cond, self.values, other), dtype=dtype
19691983
)
19701984

1971-
return self.make_block_same_class(result, placement=self.mgr_locs)
1985+
return [self.make_block_same_class(result, placement=self.mgr_locs)]
19721986

19731987
@property
19741988
def _ftype(self):
@@ -2706,7 +2720,7 @@ def f(m, v, i):
27062720

27072721
return blocks
27082722

2709-
def _maybe_downcast(self, blocks, downcast=None):
2723+
def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"]:
27102724

27112725
if downcast is not None:
27122726
return blocks
@@ -3031,7 +3045,15 @@ def concat_same_type(self, to_concat, placement=None):
30313045
values, placement=placement or slice(0, len(values), 1), ndim=self.ndim
30323046
)
30333047

3034-
def where(self, other, cond, align=True, errors="raise", try_cast=False, axis=0):
3048+
def where(
3049+
self,
3050+
other,
3051+
cond,
3052+
align=True,
3053+
errors="raise",
3054+
try_cast: bool = False,
3055+
axis: int = 0,
3056+
) -> List["Block"]:
30353057
# TODO(CategoricalBlock.where):
30363058
# This can all be deleted in favor of ExtensionBlock.where once
30373059
# we enforce the deprecation.

pandas/core/internals/managers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,7 @@ def _simple_blockify(tuples, dtype):
18231823
"""
18241824
values, placement = _stack_arrays(tuples, dtype)
18251825

1826-
# CHECK DTYPE?
1826+
# TODO: CHECK DTYPE?
18271827
if dtype is not None and values.dtype != dtype: # pragma: no cover
18281828
values = values.astype(dtype)
18291829

pandas/core/resample.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1630,15 +1630,14 @@ def _get_period_bins(self, ax):
16301630

16311631

16321632
def _take_new_index(obj, indexer, new_index, axis=0):
1633-
from pandas.core.api import Series, DataFrame
16341633

1635-
if isinstance(obj, Series):
1634+
if isinstance(obj, ABCSeries):
16361635
new_values = algos.take_1d(obj.values, indexer)
1637-
return Series(new_values, index=new_index, name=obj.name)
1638-
elif isinstance(obj, DataFrame):
1636+
return obj._constructor(new_values, index=new_index, name=obj.name)
1637+
elif isinstance(obj, ABCDataFrame):
16391638
if axis == 1:
16401639
raise NotImplementedError("axis 1 is not supported")
1641-
return DataFrame(
1640+
return obj._constructor(
16421641
obj._data.reindex_indexer(new_axis=new_index, indexer=indexer, axis=1)
16431642
)
16441643
else:

pandas/core/window.py

+2
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ def _wrap_result(self, result, block=None, obj=None):
265265
# coerce if necessary
266266
if block is not None:
267267
if is_timedelta64_dtype(block.values.dtype):
268+
# TODO: do we know what result.dtype is at this point?
269+
# i.e. can we just do an astype?
268270
from pandas import to_timedelta
269271

270272
result = to_timedelta(result.ravel(), unit="ns").values.reshape(

pandas/tests/groupby/test_categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ def test_datetime():
506506
desc_result = grouped.describe()
507507

508508
idx = cats.codes.argsort()
509-
ord_labels = cats.take_nd(idx)
509+
ord_labels = cats.take(idx)
510510
ord_data = data.take(idx)
511511
expected = ord_data.groupby(ord_labels, observed=False).describe()
512512
assert_frame_equal(desc_result, expected)

0 commit comments

Comments
 (0)