Skip to content

Commit 4dd2e3f

Browse files
jbrockmendeljreback
authored andcommitted
Assorted cleanups (#27376)
1 parent 7b61952 commit 4dd2e3f

File tree

5 files changed

+34
-52
lines changed

5 files changed

+34
-52
lines changed

pandas/_libs/src/klib/khash_python.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ khint_t PANDAS_INLINE kh_put_str_starts_item(kh_str_starts_t* table, char* key,
106106
return result;
107107
}
108108

109-
khint_t PANDAS_INLINE kh_get_str_starts_item(kh_str_starts_t* table, char* key) {
109+
khint_t PANDAS_INLINE kh_get_str_starts_item(const kh_str_starts_t* table, const char* key) {
110110
unsigned char ch = *key;
111111
if (table->starts[ch]) {
112112
if (ch == '\0' || kh_get_str(table->table, key) != table->table->n_buckets) return 1;

pandas/core/indexing.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import textwrap
2+
from typing import Tuple
23
import warnings
34

45
import numpy as np
@@ -936,7 +937,7 @@ def _getitem_lowerdim(self, tup):
936937
new_key = b, a
937938

938939
if len(new_key) == 1:
939-
new_key, = new_key
940+
new_key = new_key[0]
940941

941942
# Slices should return views, but calling iloc/loc with a null
942943
# slice returns a new object.
@@ -1250,7 +1251,7 @@ def _convert_to_indexer(
12501251
# a positional
12511252
if obj >= self.obj.shape[axis] and not isinstance(labels, MultiIndex):
12521253
raise ValueError(
1253-
"cannot set by positional indexing with " "enlargement"
1254+
"cannot set by positional indexing with enlargement"
12541255
)
12551256

12561257
return obj
@@ -1408,7 +1409,7 @@ def __getitem__(self, key):
14081409
maybe_callable = com.apply_if_callable(key, self.obj)
14091410
return self._getitem_axis(maybe_callable, axis=axis)
14101411

1411-
def _is_scalar_access(self, key):
1412+
def _is_scalar_access(self, key: Tuple):
14121413
raise NotImplementedError()
14131414

14141415
def _getitem_scalar(self, key):
@@ -1709,14 +1710,11 @@ def _validate_key(self, key, axis: int):
17091710
if not is_list_like_indexer(key):
17101711
self._convert_scalar_indexer(key, axis)
17111712

1712-
def _is_scalar_access(self, key):
1713+
def _is_scalar_access(self, key: Tuple):
17131714
# this is a shortcut accessor to both .loc and .iloc
17141715
# that provide the equivalent access of .at and .iat
17151716
# a) avoid getting things via sections and (to minimize dtype changes)
17161717
# b) provide a performant path
1717-
if not hasattr(key, "__len__"):
1718-
return False
1719-
17201718
if len(key) != self.ndim:
17211719
return False
17221720

@@ -2000,7 +1998,7 @@ def _validate_key(self, key, axis: int):
20001998
# check that the key has a numeric dtype
20011999
if not is_numeric_dtype(arr.dtype):
20022000
raise IndexError(
2003-
".iloc requires numeric indexers, got " "{arr}".format(arr=arr)
2001+
".iloc requires numeric indexers, got {arr}".format(arr=arr)
20042002
)
20052003

20062004
# check that the key does not exceed the maximum size of the index
@@ -2015,14 +2013,11 @@ def _validate_key(self, key, axis: int):
20152013
def _has_valid_setitem_indexer(self, indexer):
20162014
self._has_valid_positional_setitem_indexer(indexer)
20172015

2018-
def _is_scalar_access(self, key):
2016+
def _is_scalar_access(self, key: Tuple):
20192017
# this is a shortcut accessor to both .loc and .iloc
20202018
# that provide the equivalent access of .at and .iat
20212019
# a) avoid getting things via sections and (to minimize dtype changes)
20222020
# b) provide a performant path
2023-
if not hasattr(key, "__len__"):
2024-
return False
2025-
20262021
if len(key) != self.ndim:
20272022
return False
20282023

@@ -2131,9 +2126,7 @@ def _getitem_axis(self, key, axis: int):
21312126
else:
21322127
key = item_from_zerodim(key)
21332128
if not is_integer(key):
2134-
raise TypeError(
2135-
"Cannot index by location index with a " "non-integer key"
2136-
)
2129+
raise TypeError("Cannot index by location index with a non-integer key")
21372130

21382131
# validate the location
21392132
self._validate_integer(key, axis)
@@ -2191,7 +2184,7 @@ def __setitem__(self, key, value):
21912184
if not isinstance(key, tuple):
21922185
key = self._tuplify(key)
21932186
if len(key) != self.obj.ndim:
2194-
raise ValueError("Not enough indexers for scalar access " "(setting)!")
2187+
raise ValueError("Not enough indexers for scalar access (setting)!")
21952188
key = list(self._convert_key(key, is_setter=True))
21962189
key.append(value)
21972190
self.obj._set_value(*key, takeable=self._takeable)
@@ -2327,7 +2320,7 @@ def _convert_key(self, key, is_setter: bool = False):
23272320
""" require integer args (and convert to label arguments) """
23282321
for a, i in zip(self.obj.axes, key):
23292322
if not is_integer(i):
2330-
raise ValueError("iAt based indexing can only have integer " "indexers")
2323+
raise ValueError("iAt based indexing can only have integer indexers")
23312324
return key
23322325

23332326

pandas/core/internals/blocks.py

+15-24
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
is_integer_dtype,
4343
is_interval_dtype,
4444
is_list_like,
45-
is_numeric_v_string_like,
4645
is_object_dtype,
4746
is_period_dtype,
4847
is_re,
@@ -1304,24 +1303,20 @@ def take_nd(self, indexer, axis, new_mgr_locs=None, fill_tuple=None):
13041303

13051304
if fill_tuple is None:
13061305
fill_value = self.fill_value
1307-
new_values = algos.take_nd(
1308-
values, indexer, axis=axis, allow_fill=False, fill_value=fill_value
1309-
)
1306+
allow_fill = False
13101307
else:
13111308
fill_value = fill_tuple[0]
1312-
new_values = algos.take_nd(
1313-
values, indexer, axis=axis, allow_fill=True, fill_value=fill_value
1314-
)
1309+
allow_fill = True
13151310

1311+
new_values = algos.take_nd(
1312+
values, indexer, axis=axis, allow_fill=allow_fill, fill_value=fill_value
1313+
)
1314+
1315+
# Called from three places in managers, all of which satisfy
1316+
# this assertion
1317+
assert not (axis == 0 and new_mgr_locs is None)
13161318
if new_mgr_locs is None:
1317-
if axis == 0:
1318-
slc = libinternals.indexer_as_slice(indexer)
1319-
if slc is not None:
1320-
new_mgr_locs = self.mgr_locs[slc]
1321-
else:
1322-
new_mgr_locs = self.mgr_locs[indexer]
1323-
else:
1324-
new_mgr_locs = self.mgr_locs
1319+
new_mgr_locs = self.mgr_locs
13251320

13261321
if not is_dtype_equal(new_values.dtype, self.dtype):
13271322
return self.make_block(new_values, new_mgr_locs)
@@ -1865,11 +1860,11 @@ def take_nd(self, indexer, axis=0, new_mgr_locs=None, fill_tuple=None):
18651860
# if its REALLY axis 0, then this will be a reindex and not a take
18661861
new_values = self.values.take(indexer, fill_value=fill_value, allow_fill=True)
18671862

1868-
if self.ndim == 1 and new_mgr_locs is None:
1869-
new_mgr_locs = [0]
1870-
else:
1871-
if new_mgr_locs is None:
1872-
new_mgr_locs = self.mgr_locs
1863+
# Called from three places in managers, all of which satisfy
1864+
# this assertion
1865+
assert not (self.ndim == 1 and new_mgr_locs is None)
1866+
if new_mgr_locs is None:
1867+
new_mgr_locs = self.mgr_locs
18731868

18741869
return self.make_block_same_class(new_values, new_mgr_locs)
18751870

@@ -3388,10 +3383,6 @@ def _putmask_smart(v, m, n):
33883383
# if we have nulls
33893384
if not _isna_compat(v, nn[0]):
33903385
pass
3391-
elif is_numeric_v_string_like(nn, v):
3392-
# avoid invalid dtype comparisons
3393-
# between numbers & strings
3394-
pass
33953386
elif not (is_float_dtype(nn.dtype) or is_integer_dtype(nn.dtype)):
33963387
# only compare integers/floats
33973388
pass

pandas/core/internals/construction.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def extract_index(data):
355355
raw_lengths.append(len(val))
356356

357357
if not indexes and not raw_lengths:
358-
raise ValueError("If using all scalar values, you must pass" " an index")
358+
raise ValueError("If using all scalar values, you must pass an index")
359359

360360
if have_series:
361361
index = _union_indexes(indexes)
@@ -369,7 +369,7 @@ def extract_index(data):
369369

370370
if have_dicts:
371371
raise ValueError(
372-
"Mixing dicts with non-Series may lead to " "ambiguous ordering."
372+
"Mixing dicts with non-Series may lead to ambiguous ordering."
373373
)
374374

375375
if have_series:

pandas/core/internals/managers.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ def value_getitem(placement):
10611061

10621062
if value.shape[1:] != self.shape[1:]:
10631063
raise AssertionError(
1064-
"Shape of new values must be compatible " "with manager shape"
1064+
"Shape of new values must be compatible with manager shape"
10651065
)
10661066

10671067
try:
@@ -1154,7 +1154,7 @@ def value_getitem(placement):
11541154
# Newly created block's dtype may already be present.
11551155
self._known_consolidated = False
11561156

1157-
def insert(self, loc, item, value, allow_duplicates=False):
1157+
def insert(self, loc: int, item, value, allow_duplicates: bool = False):
11581158
"""
11591159
Insert item at selected position.
11601160
@@ -1389,9 +1389,7 @@ def take(self, indexer, axis=1, verify=True, convert=True):
13891389

13901390
if verify:
13911391
if ((indexer == -1) | (indexer >= n)).any():
1392-
raise Exception(
1393-
"Indices must be nonzero and less than " "the axis length"
1394-
)
1392+
raise Exception("Indices must be nonzero and less than the axis length")
13951393

13961394
new_labels = self.axes[axis].take(indexer)
13971395
return self.reindex_indexer(
@@ -1478,7 +1476,7 @@ def __init__(
14781476
if isinstance(axis, list):
14791477
if len(axis) != 1:
14801478
raise ValueError(
1481-
"cannot create SingleBlockManager with more " "than 1 axis"
1479+
"cannot create SingleBlockManager with more than 1 axis"
14821480
)
14831481
axis = axis[0]
14841482

@@ -1492,7 +1490,7 @@ def __init__(
14921490
block = [np.array([])]
14931491
elif len(block) != 1:
14941492
raise ValueError(
1495-
"Cannot create SingleBlockManager with " "more than 1 block"
1493+
"Cannot create SingleBlockManager with more than 1 block"
14961494
)
14971495
block = block[0]
14981496
else:
@@ -1509,7 +1507,7 @@ def __init__(
15091507

15101508
if len(block) != 1:
15111509
raise ValueError(
1512-
"Cannot create SingleBlockManager with " "more than 1 block"
1510+
"Cannot create SingleBlockManager with more than 1 block"
15131511
)
15141512
block = block[0]
15151513

0 commit comments

Comments
 (0)