Skip to content

Commit ae12817

Browse files
authored
CLN: docstrings, annotations, raising corner cases (#40409)
1 parent 940695b commit ae12817

File tree

13 files changed

+55
-33
lines changed

13 files changed

+55
-33
lines changed

pandas/_libs/index.pyx

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import warnings
22

3+
cimport cython
4+
35
import numpy as np
46

57
cimport numpy as cnp
@@ -47,6 +49,7 @@ cdef inline bint is_definitely_invalid_key(object val):
4749
_SIZE_CUTOFF = 1_000_000
4850

4951

52+
@cython.freelist(32)
5053
cdef class IndexEngine:
5154

5255
cdef readonly:

pandas/_libs/missing.pyx

+10
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ cpdef bint checknull(object val):
104104
- np.datetime64 representation of NaT
105105
- np.timedelta64 representation of NaT
106106
- NA
107+
- Decimal("NaN")
107108
108109
Parameters
109110
----------
@@ -143,6 +144,8 @@ cpdef bint checknull_old(object val):
143144
- NaT
144145
- np.datetime64 representation of NaT
145146
- np.timedelta64 representation of NaT
147+
- NA
148+
- Decimal("NaN")
146149
147150
Parameters
148151
----------
@@ -175,6 +178,8 @@ cpdef ndarray[uint8_t] isnaobj(ndarray arr):
175178
- NaT
176179
- np.datetime64 representation of NaT
177180
- np.timedelta64 representation of NaT
181+
- NA
182+
- Decimal("NaN")
178183
179184
Parameters
180185
----------
@@ -211,6 +216,7 @@ def isnaobj_old(arr: ndarray) -> ndarray:
211216
- NEGINF
212217
- NaT
213218
- NA
219+
- Decimal("NaN")
214220

215221
Parameters
216222
----------
@@ -249,6 +255,8 @@ def isnaobj2d(arr: ndarray) -> ndarray:
249255
- NaT
250256
- np.datetime64 representation of NaT
251257
- np.timedelta64 representation of NaT
258+
- NA
259+
- Decimal("NaN")
252260

253261
Parameters
254262
----------
@@ -293,6 +301,8 @@ def isnaobj2d_old(arr: ndarray) -> ndarray:
293301
- NaT
294302
- np.datetime64 representation of NaT
295303
- np.timedelta64 representation of NaT
304+
- NA
305+
- Decimal("NaN")
296306

297307
Parameters
298308
----------

pandas/compat/pickle_compat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def load_reduce(self):
4949
return
5050
except TypeError:
5151
pass
52-
elif args and issubclass(args[0], BaseOffset):
52+
elif args and isinstance(args[0], type) and issubclass(args[0], BaseOffset):
5353
# TypeError: object.__new__(Day) is not safe, use Day.__new__()
5454
cls = args[0]
5555
stack[-1] = cls.__new__(*args)

pandas/core/array_algos/take.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ def take_2d_multi(
271271

272272

273273
@functools.lru_cache(maxsize=128)
274-
def _get_take_nd_function_cached(ndim, arr_dtype, out_dtype, axis):
274+
def _get_take_nd_function_cached(
275+
ndim: int, arr_dtype: np.dtype, out_dtype: np.dtype, axis: int
276+
):
275277
"""
276278
Part of _get_take_nd_function below that doesn't need `mask_info` and thus
277279
can be cached (mask_info potentially contains a numpy ndarray which is not
@@ -304,7 +306,7 @@ def _get_take_nd_function_cached(ndim, arr_dtype, out_dtype, axis):
304306

305307

306308
def _get_take_nd_function(
307-
ndim: int, arr_dtype, out_dtype, axis: int = 0, mask_info=None
309+
ndim: int, arr_dtype: np.dtype, out_dtype: np.dtype, axis: int = 0, mask_info=None
308310
):
309311
"""
310312
Get the appropriate "take" implementation for the given dimension, axis

pandas/core/indexes/base.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -657,15 +657,15 @@ def _shallow_copy(self: _IndexT, values, name: Hashable = no_default) -> _IndexT
657657
values : the values to create the new Index, optional
658658
name : Label, defaults to self.name
659659
"""
660-
name = self.name if name is no_default else name
660+
name = self._name if name is no_default else name
661661

662662
return self._simple_new(values, name=name)
663663

664664
def _view(self: _IndexT) -> _IndexT:
665665
"""
666666
fastpath to make a shallow copy, i.e. new object with same data.
667667
"""
668-
result = self._simple_new(self._values, name=self.name)
668+
result = self._simple_new(self._values, name=self._name)
669669

670670
result._cache = self._cache
671671
return result
@@ -4569,7 +4569,7 @@ def __getitem__(self, key):
45694569
# pessimization of basic indexing.
45704570
result = getitem(key)
45714571
# Going through simple_new for performance.
4572-
return type(self)._simple_new(result, name=self.name)
4572+
return type(self)._simple_new(result, name=self._name)
45734573

45744574
if com.is_bool_indexer(key):
45754575
key = np.asarray(key, dtype=bool)
@@ -4585,7 +4585,7 @@ def __getitem__(self, key):
45854585
return result
45864586
# NB: Using _constructor._simple_new would break if MultiIndex
45874587
# didn't override __getitem__
4588-
return self._constructor._simple_new(result, name=self.name)
4588+
return self._constructor._simple_new(result, name=self._name)
45894589
else:
45904590
return result
45914591

pandas/core/indexes/category.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def _shallow_copy(
240240
values: Categorical,
241241
name: Hashable = no_default,
242242
):
243-
name = self.name if name is no_default else name
243+
name = self._name if name is no_default else name
244244

245245
if values is not None:
246246
# In tests we only get here with Categorical objects that

pandas/core/indexes/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin):
645645

646646
def _with_freq(self, freq):
647647
arr = self._data._with_freq(freq)
648-
return type(self)._simple_new(arr, name=self.name)
648+
return type(self)._simple_new(arr, name=self._name)
649649

650650
@property
651651
def _has_complex_internals(self) -> bool:

pandas/core/indexes/extension.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def __getitem__(self, key):
250250
result = self._data[key]
251251
if isinstance(result, type(self._data)):
252252
if result.ndim == 1:
253-
return type(self)(result, name=self.name)
253+
return type(self)(result, name=self._name)
254254
# Unpack to ndarray for MPL compat
255255

256256
result = result._ndarray

pandas/core/indexes/multi.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -1718,7 +1718,7 @@ def unique(self, level=None):
17181718
level = self._get_level_number(level)
17191719
return self._get_level_values(level=level, unique=True)
17201720

1721-
def to_frame(self, index=True, name=None) -> DataFrame:
1721+
def to_frame(self, index: bool = True, name=None) -> DataFrame:
17221722
"""
17231723
Create a DataFrame with the levels of the MultiIndex as columns.
17241724
@@ -2123,7 +2123,12 @@ def _getitem_slice(self: MultiIndex, slobj: slice) -> MultiIndex:
21232123

21242124
@Appender(_index_shared_docs["take"] % _index_doc_kwargs)
21252125
def take(
2126-
self: MultiIndex, indices, axis=0, allow_fill=True, fill_value=None, **kwargs
2126+
self: MultiIndex,
2127+
indices,
2128+
axis: int = 0,
2129+
allow_fill: bool = True,
2130+
fill_value=None,
2131+
**kwargs,
21272132
) -> MultiIndex:
21282133
nv.validate_take((), kwargs)
21292134
indices = ensure_platform_int(indices)
@@ -3647,7 +3652,7 @@ def _intersection(self, other, sort=False) -> MultiIndex:
36473652
zip(*uniq_tuples), sortorder=0, names=result_names
36483653
)
36493654

3650-
def _difference(self, other, sort):
3655+
def _difference(self, other, sort) -> MultiIndex:
36513656
other, result_names = self._convert_can_do_setop(other)
36523657

36533658
this = self._get_unique_index()
@@ -3705,7 +3710,7 @@ def symmetric_difference(self, other, result_name=None, sort=None):
37053710
# --------------------------------------------------------------------
37063711

37073712
@doc(Index.astype)
3708-
def astype(self, dtype, copy=True):
3713+
def astype(self, dtype, copy: bool = True):
37093714
dtype = pandas_dtype(dtype)
37103715
if is_categorical_dtype(dtype):
37113716
msg = "> 1 ndim Categorical are not supported at this time"

pandas/core/indexes/numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def _maybe_cast_slice_bound(self, label, side: str, kind):
125125
@doc(Index._shallow_copy)
126126
def _shallow_copy(self, values, name: Hashable = lib.no_default):
127127
if not self._can_hold_na and values.dtype.kind == "f":
128-
name = self.name if name is lib.no_default else name
128+
name = self._name if name is lib.no_default else name
129129
# Ensure we are not returning an Int64Index with float data:
130130
return Float64Index._simple_new(values, name=name)
131131
return super()._shallow_copy(values=values, name=name)

pandas/core/indexes/range.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ def _shallow_copy(self, values, name: Hashable = no_default):
460460
return Int64Index._simple_new(values, name=name)
461461

462462
def _view(self: RangeIndex) -> RangeIndex:
463-
result = type(self)._simple_new(self._range, name=self.name)
463+
result = type(self)._simple_new(self._range, name=self._name)
464464
result._cache = self._cache
465465
return result
466466

@@ -811,7 +811,7 @@ def __getitem__(self, key):
811811
"""
812812
if isinstance(key, slice):
813813
new_range = self._range[key]
814-
return self._simple_new(new_range, name=self.name)
814+
return self._simple_new(new_range, name=self._name)
815815
elif is_integer(key):
816816
new_key = int(key)
817817
try:

pandas/core/internals/blocks.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def make_block(self, values, placement=None) -> Block:
260260
not specified
261261
"""
262262
if placement is None:
263-
placement = self.mgr_locs
263+
placement = self._mgr_locs
264264
if self.is_extension:
265265
values = ensure_block_shape(values, ndim=self.ndim)
266266

@@ -272,8 +272,7 @@ def make_block(self, values, placement=None) -> Block:
272272
def make_block_same_class(self, values, placement=None) -> Block:
273273
""" Wrap given values in a block of same type as self. """
274274
if placement is None:
275-
placement = self.mgr_locs
276-
# TODO: perf by not going through new_block
275+
placement = self._mgr_locs
277276
# We assume maybe_coerce_values has already been called
278277
return type(self)(values, placement=placement, ndim=self.ndim)
279278

@@ -318,7 +317,7 @@ def getitem_block(self, slicer, new_mgr_locs=None) -> Block:
318317
"""
319318
if new_mgr_locs is None:
320319
axis0_slicer = slicer[0] if isinstance(slicer, tuple) else slicer
321-
new_mgr_locs = self.mgr_locs[axis0_slicer]
320+
new_mgr_locs = self._mgr_locs[axis0_slicer]
322321
elif not isinstance(new_mgr_locs, BlockPlacement):
323322
new_mgr_locs = BlockPlacement(new_mgr_locs)
324323

@@ -358,7 +357,7 @@ def delete(self, loc) -> None:
358357
Delete given loc(-s) from block in-place.
359358
"""
360359
self.values = np.delete(self.values, loc, 0)
361-
self.mgr_locs = self.mgr_locs.delete(loc)
360+
self.mgr_locs = self._mgr_locs.delete(loc)
362361

363362
@final
364363
def apply(self, func, **kwargs) -> List[Block]:
@@ -399,7 +398,7 @@ def _split_op_result(self, result) -> List[Block]:
399398
# TODO(EA2D): unnecessary with 2D EAs
400399
# if we get a 2D ExtensionArray, we need to split it into 1D pieces
401400
nbs = []
402-
for i, loc in enumerate(self.mgr_locs):
401+
for i, loc in enumerate(self._mgr_locs):
403402
vals = result[i]
404403
block = self.make_block(values=vals, placement=loc)
405404
nbs.append(block)
@@ -462,7 +461,7 @@ def _split(self) -> List[Block]:
462461
assert self.ndim == 2
463462

464463
new_blocks = []
465-
for i, ref_loc in enumerate(self.mgr_locs):
464+
for i, ref_loc in enumerate(self._mgr_locs):
466465
vals = self.values[slice(i, i + 1)]
467466

468467
nb = self.make_block(vals, BlockPlacement(ref_loc))
@@ -512,12 +511,12 @@ def make_a_block(nv, ref_loc):
512511
nv = f(mask, new_values, None)
513512
else:
514513
nv = new_values if inplace else new_values.copy()
515-
block = make_a_block(nv, self.mgr_locs)
514+
block = make_a_block(nv, self._mgr_locs)
516515
return [block]
517516

518517
# ndim > 1
519518
new_blocks = []
520-
for i, ref_loc in enumerate(self.mgr_locs):
519+
for i, ref_loc in enumerate(self._mgr_locs):
521520
m = mask[i]
522521
v = new_values[i]
523522

@@ -1254,7 +1253,7 @@ def take_nd(
12541253
# this assertion
12551254
assert not (axis == 0 and new_mgr_locs is None)
12561255
if new_mgr_locs is None:
1257-
new_mgr_locs = self.mgr_locs
1256+
new_mgr_locs = self._mgr_locs
12581257

12591258
if not is_dtype_equal(new_values.dtype, self.dtype):
12601259
return self.make_block(new_values, new_mgr_locs)
@@ -1362,7 +1361,7 @@ def where(self, other, cond, errors="raise", axis: int = 0) -> List[Block]:
13621361
result = cast(np.ndarray, result) # EABlock overrides where
13631362
taken = result.take(m.nonzero()[0], axis=axis)
13641363
r = maybe_downcast_numeric(taken, self.dtype)
1365-
nb = self.make_block(r.T, placement=self.mgr_locs[m])
1364+
nb = self.make_block(r.T, placement=self._mgr_locs[m])
13661365
result_blocks.append(nb)
13671366

13681367
return result_blocks
@@ -1423,7 +1422,7 @@ def quantile(
14231422

14241423
result = quantile_compat(self.values, qs, interpolation, axis)
14251424

1426-
return new_block(result, placement=self.mgr_locs, ndim=2)
1425+
return new_block(result, placement=self._mgr_locs, ndim=2)
14271426

14281427

14291428
class ExtensionBlock(Block):
@@ -1449,7 +1448,7 @@ def shape(self) -> Shape:
14491448
# TODO(EA2D): override unnecessary with 2D EAs
14501449
if self.ndim == 1:
14511450
return (len(self.values),)
1452-
return len(self.mgr_locs), len(self.values)
1451+
return len(self._mgr_locs), len(self.values)
14531452

14541453
def iget(self, col):
14551454

@@ -1594,7 +1593,7 @@ def take_nd(
15941593
# this assertion
15951594
assert not (self.ndim == 1 and new_mgr_locs is None)
15961595
if new_mgr_locs is None:
1597-
new_mgr_locs = self.mgr_locs
1596+
new_mgr_locs = self._mgr_locs
15981597

15991598
return self.make_block_same_class(new_values, new_mgr_locs)
16001599

@@ -1630,7 +1629,7 @@ def _slice(self, slicer):
16301629
)
16311630
# GH#32959 only full-slicers along fake-dim0 are valid
16321631
# TODO(EA2D): won't be necessary with 2D EAs
1633-
new_locs = self.mgr_locs[first]
1632+
new_locs = self._mgr_locs[first]
16341633
if len(new_locs):
16351634
# effectively slice(None)
16361635
slicer = slicer[1]
@@ -1741,9 +1740,10 @@ def _unstack(self, unstacker, fill_value, new_placement):
17411740
# TODO: in all tests we have mask.all(); can we rely on that?
17421741

17431742
blocks = [
1743+
# TODO: could cast to object depending on fill_value?
17441744
self.make_block_same_class(
17451745
self.values.take(indices, allow_fill=True, fill_value=fill_value),
1746-
[place],
1746+
BlockPlacement(place),
17471747
)
17481748
for indices, place in zip(new_values.T, new_placement)
17491749
]

pandas/util/_exceptions.py

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ def rewrite_exception(old_name: str, new_name: str):
1111
try:
1212
yield
1313
except Exception as err:
14+
if not err.args:
15+
raise
1416
msg = str(err.args[0])
1517
msg = msg.replace(old_name, new_name)
1618
args: Tuple[str, ...] = (msg,)

0 commit comments

Comments
 (0)