Skip to content

Commit 45c13a9

Browse files
authored
CLN: assorted cleanups (#33297)
1 parent e0dd81a commit 45c13a9

File tree

6 files changed

+37
-25
lines changed

6 files changed

+37
-25
lines changed

pandas/_libs/tslibs/timedeltas.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from numpy cimport int64_t
22

33
# Exposed for tslib, not intended for outside use.
4-
cdef int64_t cast_from_unit(object ts, object unit) except? -1
4+
cdef int64_t cast_from_unit(object ts, str unit) except? -1
55
cpdef int64_t delta_to_nanoseconds(delta) except? -1
66
cdef convert_to_timedelta64(object ts, object unit)

pandas/_libs/tslibs/timedeltas.pyx

+22-8
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,15 @@ def array_to_timedelta64(object[:] values, unit='ns', errors='raise'):
257257
return iresult.base # .base to access underlying np.ndarray
258258

259259

260-
cpdef inline object precision_from_unit(object unit):
260+
cpdef inline object precision_from_unit(str unit):
261261
"""
262262
Return a casting of the unit represented to nanoseconds + the precision
263263
to round the fractional part.
264+
265+
Notes
266+
-----
267+
The caller is responsible for ensuring that the default value of "ns"
268+
takes the place of None.
264269
"""
265270
cdef:
266271
int64_t m
@@ -301,7 +306,7 @@ cpdef inline object precision_from_unit(object unit):
301306
return m, p
302307

303308

304-
cdef inline int64_t cast_from_unit(object ts, object unit) except? -1:
309+
cdef inline int64_t cast_from_unit(object ts, str unit) except? -1:
305310
""" return a casting of the unit represented to nanoseconds
306311
round the fractional part of a float to our precision, p """
307312
cdef:
@@ -525,15 +530,24 @@ cdef inline timedelta_from_spec(object number, object frac, object unit):
525530
return cast_from_unit(float(n), unit)
526531

527532

528-
cpdef inline object parse_timedelta_unit(object unit):
533+
cpdef inline str parse_timedelta_unit(object unit):
529534
"""
530535
Parameters
531536
----------
532-
unit : an unit string
537+
unit : str or None
538+
539+
Returns
540+
-------
541+
str
542+
Canonical unit string.
543+
544+
Raises
545+
------
546+
ValueError : on non-parseable input
533547
"""
534548
if unit is None:
535-
return 'ns'
536-
elif unit == 'M':
549+
return "ns"
550+
elif unit == "M":
537551
return unit
538552
try:
539553
return timedelta_abbrevs[unit.lower()]
@@ -622,14 +636,14 @@ def _binary_op_method_timedeltalike(op, name):
622636
# ----------------------------------------------------------------------
623637
# Timedelta Construction
624638

625-
cdef inline int64_t parse_iso_format_string(object ts) except? -1:
639+
cdef inline int64_t parse_iso_format_string(str ts) except? -1:
626640
"""
627641
Extracts and cleanses the appropriate values from a match object with
628642
groups for each component of an ISO 8601 duration
629643
630644
Parameters
631645
----------
632-
ts:
646+
ts: str
633647
ISO 8601 Duration formatted string
634648
635649
Returns

pandas/core/generic.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4504,6 +4504,8 @@ def _reindex_with_indexers(
45044504
allow_dups=allow_dups,
45054505
copy=copy,
45064506
)
4507+
# If we've made a copy once, no need to make another one
4508+
copy = False
45074509

45084510
if copy and new_data is self._mgr:
45094511
new_data = new_data.copy()
@@ -6468,7 +6470,6 @@ def replace(
64686470
):
64696471
if not (
64706472
is_scalar(to_replace)
6471-
or isinstance(to_replace, pd.Series)
64726473
or is_re_compilable(to_replace)
64736474
or is_list_like(to_replace)
64746475
):

pandas/core/indexes/base.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from copy import copy as copy_func
12
from datetime import datetime
23
import operator
34
from textwrap import dedent
@@ -5313,7 +5314,7 @@ def _add_numeric_methods_unary(cls):
53135314
Add in numeric unary methods.
53145315
"""
53155316

5316-
def _make_evaluate_unary(op, opstr):
5317+
def _make_evaluate_unary(op, opstr: str_t):
53175318
def _evaluate_numeric_unary(self):
53185319

53195320
attrs = self._get_attributes_dict()
@@ -5419,7 +5420,7 @@ def _add_logical_methods(cls):
54195420
"""
54205421
)
54215422

5422-
def _make_logical_function(name, desc, f):
5423+
def _make_logical_function(name: str_t, desc: str_t, f):
54235424
@Substitution(outname=name, desc=desc)
54245425
@Appender(_index_shared_docs["index_" + name])
54255426
@Appender(_doc)
@@ -5508,15 +5509,15 @@ def ensure_index_from_sequences(sequences, names=None):
55085509
return MultiIndex.from_arrays(sequences, names=names)
55095510

55105511

5511-
def ensure_index(index_like, copy=False):
5512+
def ensure_index(index_like, copy: bool = False):
55125513
"""
55135514
Ensure that we have an index from some index-like object.
55145515
55155516
Parameters
55165517
----------
5517-
index : sequence
5518+
index_like : sequence
55185519
An Index or other sequence
5519-
copy : bool
5520+
copy : bool, default False
55205521
55215522
Returns
55225523
-------
@@ -5567,9 +5568,7 @@ def ensure_index(index_like, copy=False):
55675568
# clean_index_list does the equivalent of copying
55685569
# so only need to do this if not list instance
55695570
if copy:
5570-
from copy import copy
5571-
5572-
index_like = copy(index_like)
5571+
index_like = copy_func(index_like)
55735572

55745573
return Index(index_like)
55755574

@@ -5596,7 +5595,7 @@ def _trim_front(strings):
55965595
return trimmed
55975596

55985597

5599-
def _validate_join_method(method):
5598+
def _validate_join_method(method: str):
56005599
if method not in ["left", "right", "inner", "outer"]:
56015600
raise ValueError(f"do not recognize join method {method}")
56025601

pandas/core/indexing.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ def _ensure_listlike_indexer(self, key, axis=None):
625625
626626
Parameters
627627
----------
628-
key : _LocIndexer key or list-like of column labels
628+
key : list-like of column labels
629629
Target labels.
630630
axis : key axis if known
631631
"""
@@ -636,7 +636,7 @@ def _ensure_listlike_indexer(self, key, axis=None):
636636
return
637637

638638
if isinstance(key, tuple):
639-
# key may be a tuple if key is a _LocIndexer key
639+
# key may be a tuple if we are .loc
640640
# in that case, set key to the column part of key
641641
key = key[column_axis]
642642
axis = column_axis
@@ -649,9 +649,7 @@ def _ensure_listlike_indexer(self, key, axis=None):
649649
and all(is_hashable(k) for k in key)
650650
):
651651
for k in key:
652-
try:
653-
self.obj[k]
654-
except KeyError:
652+
if k not in self.obj:
655653
self.obj[k] = np.nan
656654

657655
def __setitem__(self, key, value):

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ def _set_axis(self, axis: int, labels, fastpath: bool = False) -> None:
415415

416416
object.__setattr__(self, "_index", labels)
417417
if not fastpath:
418-
# The ensure_index call aabove ensures we have an Index object
418+
# The ensure_index call above ensures we have an Index object
419419
self._mgr.set_axis(axis, labels)
420420

421421
# ndarray compatibility

0 commit comments

Comments
 (0)