From a4c2b9a7834b1f3f40d3b7c30b196dc9cce14e79 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 4 Apr 2020 16:59:09 -0700 Subject: [PATCH 1/2] CLN: assorted cleanups --- pandas/_libs/tslibs/timedeltas.pyx | 30 ++++++++++++++++++++++-------- pandas/core/generic.py | 3 ++- pandas/core/indexes/base.py | 16 ++++++++-------- pandas/core/indexing.py | 8 +++----- pandas/core/series.py | 2 +- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index f2b77f3517a25..fdda21eea0b95 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -257,10 +257,15 @@ def array_to_timedelta64(object[:] values, unit='ns', errors='raise'): return iresult.base # .base to access underlying np.ndarray -cpdef inline object precision_from_unit(object unit): +cpdef inline object precision_from_unit(str unit): """ Return a casting of the unit represented to nanoseconds + the precision to round the fractional part. + + Notes + ----- + The caller is responsible for ensuring that the default value of "ns" + takes the place of None. """ cdef: int64_t m @@ -308,7 +313,7 @@ cdef inline int64_t cast_from_unit(object ts, object unit) except? -1: int64_t m int p - m, p = precision_from_unit(unit) + m, p = precision_from_unit(unit or "ns") # just give me the unit back if ts is None: @@ -525,15 +530,24 @@ cdef inline timedelta_from_spec(object number, object frac, object unit): return cast_from_unit(float(n), unit) -cpdef inline object parse_timedelta_unit(object unit): +cpdef inline str parse_timedelta_unit(object unit): """ Parameters ---------- - unit : an unit string + unit : str or None + + Returns + ------- + str + Canonical unit string. + + Raises + ------ + ValueError : on non-parseable input """ if unit is None: - return 'ns' - elif unit == 'M': + return "ns" + elif unit == "M": return unit try: return timedelta_abbrevs[unit.lower()] @@ -622,14 +636,14 @@ def _binary_op_method_timedeltalike(op, name): # ---------------------------------------------------------------------- # Timedelta Construction -cdef inline int64_t parse_iso_format_string(object ts) except? -1: +cdef inline int64_t parse_iso_format_string(str ts) except? -1: """ Extracts and cleanses the appropriate values from a match object with groups for each component of an ISO 8601 duration Parameters ---------- - ts: + ts: str ISO 8601 Duration formatted string Returns diff --git a/pandas/core/generic.py b/pandas/core/generic.py index c202bf846047f..2ada19c4197ad 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -4502,6 +4502,8 @@ def _reindex_with_indexers( allow_dups=allow_dups, copy=copy, ) + # If we've made a copy once, no need to make another one + copy = False if copy and new_data is self._data: new_data = new_data.copy() @@ -6463,7 +6465,6 @@ def replace( ): if not ( is_scalar(to_replace) - or isinstance(to_replace, pd.Series) or is_re_compilable(to_replace) or is_list_like(to_replace) ): diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 5fec68d257167..97534a420fb5e 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5262,7 +5262,7 @@ def _add_numeric_methods_unary(cls): Add in numeric unary methods. """ - def _make_evaluate_unary(op, opstr): + def _make_evaluate_unary(op, opstr: str_t): def _evaluate_numeric_unary(self): attrs = self._get_attributes_dict() @@ -5368,7 +5368,7 @@ def _add_logical_methods(cls): """ ) - def _make_logical_function(name, desc, f): + def _make_logical_function(name: str_t, desc: str_t, f): @Substitution(outname=name, desc=desc) @Appender(_index_shared_docs["index_" + name]) @Appender(_doc) @@ -5457,15 +5457,15 @@ def ensure_index_from_sequences(sequences, names=None): return MultiIndex.from_arrays(sequences, names=names) -def ensure_index(index_like, copy=False): +def ensure_index(index_like, copy: bool = False): """ Ensure that we have an index from some index-like object. Parameters ---------- - index : sequence + index_like : sequence An Index or other sequence - copy : bool + copy : bool, default False Returns ------- @@ -5516,9 +5516,9 @@ def ensure_index(index_like, copy=False): # clean_index_list does the equivalent of copying # so only need to do this if not list instance if copy: - from copy import copy + from copy import copy as copy_func - index_like = copy(index_like) + index_like = copy_func(index_like) return Index(index_like) @@ -5545,7 +5545,7 @@ def _trim_front(strings): return trimmed -def _validate_join_method(method): +def _validate_join_method(method: str): if method not in ["left", "right", "inner", "outer"]: raise ValueError(f"do not recognize join method {method}") diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 14c5c8ea011c3..bf04cdde5833d 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -625,7 +625,7 @@ def _ensure_listlike_indexer(self, key, axis=None): Parameters ---------- - key : _LocIndexer key or list-like of column labels + key : list-like of column labels Target labels. axis : key axis if known """ @@ -636,7 +636,7 @@ def _ensure_listlike_indexer(self, key, axis=None): return if isinstance(key, tuple): - # key may be a tuple if key is a _LocIndexer key + # key may be a tuple if we are .loc # in that case, set key to the column part of key key = key[column_axis] axis = column_axis @@ -649,9 +649,7 @@ def _ensure_listlike_indexer(self, key, axis=None): and all(is_hashable(k) for k in key) ): for k in key: - try: - self.obj[k] - except KeyError: + if k not in self.obj: self.obj[k] = np.nan def __setitem__(self, key, value): diff --git a/pandas/core/series.py b/pandas/core/series.py index 03b82365358ac..1bf54a9491dac 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -415,7 +415,7 @@ def _set_axis(self, axis: int, labels, fastpath: bool = False) -> None: object.__setattr__(self, "_index", labels) if not fastpath: - # The ensure_index call aabove ensures we have an Index object + # The ensure_index call above ensures we have an Index object self._data.set_axis(axis, labels) # ndarray compatibility From 2fa1aa60fe9410cb7cf0a4e24e239d8e54abe55e Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 5 Apr 2020 13:27:28 -0700 Subject: [PATCH 2/2] update per comments --- pandas/_libs/tslibs/timedeltas.pxd | 2 +- pandas/_libs/tslibs/timedeltas.pyx | 4 ++-- pandas/core/indexes/base.py | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pxd b/pandas/_libs/tslibs/timedeltas.pxd index b08592755f2ee..d7af7636df753 100644 --- a/pandas/_libs/tslibs/timedeltas.pxd +++ b/pandas/_libs/tslibs/timedeltas.pxd @@ -1,6 +1,6 @@ from numpy cimport int64_t # Exposed for tslib, not intended for outside use. -cdef int64_t cast_from_unit(object ts, object unit) except? -1 +cdef int64_t cast_from_unit(object ts, str unit) except? -1 cpdef int64_t delta_to_nanoseconds(delta) except? -1 cdef convert_to_timedelta64(object ts, object unit) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index fdda21eea0b95..3af2279e2440f 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -306,14 +306,14 @@ cpdef inline object precision_from_unit(str unit): return m, p -cdef inline int64_t cast_from_unit(object ts, object unit) except? -1: +cdef inline int64_t cast_from_unit(object ts, str unit) except? -1: """ return a casting of the unit represented to nanoseconds round the fractional part of a float to our precision, p """ cdef: int64_t m int p - m, p = precision_from_unit(unit or "ns") + m, p = precision_from_unit(unit) # just give me the unit back if ts is None: diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 66bfd23c945b4..d8dbe3d8861b5 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1,3 +1,4 @@ +from copy import copy as copy_func from datetime import datetime import operator from textwrap import dedent @@ -5512,8 +5513,6 @@ def ensure_index(index_like, copy: bool = False): # clean_index_list does the equivalent of copying # so only need to do this if not list instance if copy: - from copy import copy as copy_func - index_like = copy_func(index_like) return Index(index_like)