From e634d60c148345258c7f6cedd911305963a41699 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 16 May 2021 21:56:15 -0700 Subject: [PATCH 1/3] simplify to_datetime --- pandas/_libs/tslib.pyx | 5 ++++- pandas/core/tools/datetimes.py | 18 ++++++------------ pandas/tests/tools/test_to_datetime.py | 4 ++-- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 02c64fac0c009..6b1c0f851f8e7 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -235,7 +235,10 @@ def array_with_unit_to_datetime( if issubclass(values.dtype.type, (np.integer, np.float_)): result = values.astype("M8[ns]", copy=False) else: - result, tz = array_to_datetime(values.astype(object), errors=errors) + result, tz = array_to_datetime( + values.astype(object, copy=False), + errors=errors, + ) return result, tz m, p = precision_from_unit(unit) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 4eb469f52fb19..014a702618bda 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -497,14 +497,12 @@ def _to_datetime_with_format( return _box_as_indexlike(result, utc=utc, name=name) # fallback - if result is None: - res = _array_strptime_with_fallback( - arg, name, tz, fmt, exact, errors, infer_datetime_format - ) - if res is not None: - return res + res = _array_strptime_with_fallback( + arg, name, tz, fmt, exact, errors, infer_datetime_format + ) + return res - except ValueError as e: + except ValueError as err: # Fallback to try to convert datetime objects if timezone-aware # datetime objects are found without passing `utc=True` try: @@ -512,11 +510,7 @@ def _to_datetime_with_format( dta = DatetimeArray(values, dtype=tz_to_dtype(tz)) return DatetimeIndex._simple_new(dta, name=name) except (ValueError, TypeError): - raise e - - # error: Incompatible return value type (got "Optional[ndarray]", expected - # "Optional[Index]") - return result # type: ignore[return-value] + raise err def _to_datetime_with_unit(arg, unit, name, tz, errors: str) -> Index: diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index eb38f2f95d2d5..121ca99785831 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -1079,7 +1079,7 @@ def test_iso8601_strings_mixed_offsets_with_naive(self): def test_mixed_offsets_with_native_datetime_raises(self): # GH 25978 - s = Series( + ser = Series( [ "nan", Timestamp("1990-01-01"), @@ -1089,7 +1089,7 @@ def test_mixed_offsets_with_native_datetime_raises(self): ] ) with pytest.raises(ValueError, match="Tz-aware datetime.datetime"): - to_datetime(s) + to_datetime(ser) def test_non_iso_strings_with_tz_offset(self): result = to_datetime(["March 1, 2018 12:00:00+0400"] * 2) From f0544bdead43c8b29bfab6790f60f5e81f070586 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 17 May 2021 18:59:43 -0700 Subject: [PATCH 2/3] CLN/TYP: assorted --- pandas/core/base.py | 1 + pandas/core/groupby/generic.py | 4 ++-- pandas/core/groupby/groupby.py | 7 ++++--- pandas/core/groupby/grouper.py | 2 +- pandas/core/internals/construction.py | 6 +++--- pandas/core/window/rolling.py | 5 +++-- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index e2720fcbc7ec4..55e776d2e6b73 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -196,6 +196,7 @@ def _selected_obj(self): else: return self.obj[self._selection] + @final @cache_readonly def ndim(self) -> int: return self._selected_obj.ndim diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 123b9e3350fda..c38c51d46f83e 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -1105,8 +1105,8 @@ def _aggregate_frame(self, func, *args, **kwargs) -> DataFrame: else: # we get here in a number of test_multilevel tests for name in self.indices: - data = self.get_group(name, obj=obj) - fres = func(data, *args, **kwargs) + grp_df = self.get_group(name, obj=obj) + fres = func(grp_df, *args, **kwargs) result[name] = fres result_index = self.grouper.result_index diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 0b07668a9fea2..29a161676b2db 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -525,7 +525,7 @@ class GroupByPlot(PandasObject): Class implementing the .plot attribute for groupby objects. """ - def __init__(self, groupby): + def __init__(self, groupby: GroupBy): self._groupby = groupby def __call__(self, *args, **kwargs): @@ -727,7 +727,7 @@ def pipe( plot = property(GroupByPlot) @final - def get_group(self, name, obj=None): + def get_group(self, name, obj=None) -> FrameOrSeriesUnion: """ Construct DataFrame from group with provided name. @@ -960,10 +960,11 @@ def _set_group_selection(self) -> None: NOTE: this should be paired with a call to _reset_group_selection """ + # This is a no-op for SeriesGroupBy grp = self.grouper if not ( self.as_index - and getattr(grp, "groupings", None) is not None + and grp.groupings is not None and self.obj.ndim > 1 and self._group_selection is None ): diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 4aac2630feb2c..87aba1897c1bc 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -287,7 +287,7 @@ def __init__( @final @property - def ax(self): + def ax(self) -> Index | None: return self.grouper def _get_grouper(self, obj: FrameOrSeries, validate: bool = True): diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 00efc695ff04a..f33cb104cef44 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -117,7 +117,7 @@ def arrays_to_mgr( if verify_integrity: # figure out the index, if necessary if index is None: - index = extract_index(arrays) + index = _extract_index(arrays) else: index = ensure_index(index) @@ -424,7 +424,7 @@ def dict_to_mgr( if index is None: # GH10856 # raise ValueError if only scalars in dict - index = extract_index(arrays[~missing]) + index = _extract_index(arrays[~missing]) else: index = ensure_index(index) @@ -603,7 +603,7 @@ def _homogenize(data, index: Index, dtype: DtypeObj | None): return homogenized -def extract_index(data) -> Index: +def _extract_index(data) -> Index: """ Try to infer an Index from the passed data, raise ValueError on failure. """ diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 5e038635305f1..0ef0896df8d44 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -111,6 +111,7 @@ class BaseWindow(SelectionMixin): _attributes: list[str] = [] exclusions: frozenset[Hashable] = frozenset() + _on: Index def __init__( self, @@ -169,7 +170,7 @@ def win_type(self): return self._win_type @property - def is_datetimelike(self): + def is_datetimelike(self) -> bool: warnings.warn( "is_datetimelike is deprecated and will be removed in a future version.", FutureWarning, @@ -329,7 +330,7 @@ def _prep_values(self, values: ArrayLike) -> np.ndarray: # expected "ndarray") return values # type: ignore[return-value] - def _insert_on_column(self, result: DataFrame, obj: DataFrame): + def _insert_on_column(self, result: DataFrame, obj: DataFrame) -> None: # if we have an 'on' column we want to put it back into # the results in the same location from pandas import Series From ddc0068b718fb01c1c36f6b950ded024eea6cd1c Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 17 May 2021 19:01:35 -0700 Subject: [PATCH 3/3] revert grouper --- pandas/core/groupby/grouper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 87aba1897c1bc..4aac2630feb2c 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -287,7 +287,7 @@ def __init__( @final @property - def ax(self) -> Index | None: + def ax(self): return self.grouper def _get_grouper(self, obj: FrameOrSeries, validate: bool = True):