Skip to content

Commit ef99443

Browse files
authored
CLN/TYP: assorted cleanup and annotations (#41533)
1 parent d220989 commit ef99443

File tree

8 files changed

+25
-25
lines changed

8 files changed

+25
-25
lines changed

pandas/_libs/tslib.pyx

+4-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ def array_with_unit_to_datetime(
235235
if issubclass(values.dtype.type, (np.integer, np.float_)):
236236
result = values.astype("M8[ns]", copy=False)
237237
else:
238-
result, tz = array_to_datetime(values.astype(object), errors=errors)
238+
result, tz = array_to_datetime(
239+
values.astype(object, copy=False),
240+
errors=errors,
241+
)
239242
return result, tz
240243

241244
m, p = precision_from_unit(unit)

pandas/core/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ def _selected_obj(self):
196196
else:
197197
return self.obj[self._selection]
198198

199+
@final
199200
@cache_readonly
200201
def ndim(self) -> int:
201202
return self._selected_obj.ndim

pandas/core/groupby/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1105,8 +1105,8 @@ def _aggregate_frame(self, func, *args, **kwargs) -> DataFrame:
11051105
else:
11061106
# we get here in a number of test_multilevel tests
11071107
for name in self.indices:
1108-
data = self.get_group(name, obj=obj)
1109-
fres = func(data, *args, **kwargs)
1108+
grp_df = self.get_group(name, obj=obj)
1109+
fres = func(grp_df, *args, **kwargs)
11101110
result[name] = fres
11111111

11121112
result_index = self.grouper.result_index

pandas/core/groupby/groupby.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ class GroupByPlot(PandasObject):
525525
Class implementing the .plot attribute for groupby objects.
526526
"""
527527

528-
def __init__(self, groupby):
528+
def __init__(self, groupby: GroupBy):
529529
self._groupby = groupby
530530

531531
def __call__(self, *args, **kwargs):
@@ -727,7 +727,7 @@ def pipe(
727727
plot = property(GroupByPlot)
728728

729729
@final
730-
def get_group(self, name, obj=None):
730+
def get_group(self, name, obj=None) -> FrameOrSeriesUnion:
731731
"""
732732
Construct DataFrame from group with provided name.
733733
@@ -960,10 +960,11 @@ def _set_group_selection(self) -> None:
960960
961961
NOTE: this should be paired with a call to _reset_group_selection
962962
"""
963+
# This is a no-op for SeriesGroupBy
963964
grp = self.grouper
964965
if not (
965966
self.as_index
966-
and getattr(grp, "groupings", None) is not None
967+
and grp.groupings is not None
967968
and self.obj.ndim > 1
968969
and self._group_selection is None
969970
):

pandas/core/internals/construction.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def arrays_to_mgr(
117117
if verify_integrity:
118118
# figure out the index, if necessary
119119
if index is None:
120-
index = extract_index(arrays)
120+
index = _extract_index(arrays)
121121
else:
122122
index = ensure_index(index)
123123

@@ -424,7 +424,7 @@ def dict_to_mgr(
424424
if index is None:
425425
# GH10856
426426
# raise ValueError if only scalars in dict
427-
index = extract_index(arrays[~missing])
427+
index = _extract_index(arrays[~missing])
428428
else:
429429
index = ensure_index(index)
430430

@@ -603,7 +603,7 @@ def _homogenize(data, index: Index, dtype: DtypeObj | None):
603603
return homogenized
604604

605605

606-
def extract_index(data) -> Index:
606+
def _extract_index(data) -> Index:
607607
"""
608608
Try to infer an Index from the passed data, raise ValueError on failure.
609609
"""

pandas/core/tools/datetimes.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -497,26 +497,20 @@ def _to_datetime_with_format(
497497
return _box_as_indexlike(result, utc=utc, name=name)
498498

499499
# fallback
500-
if result is None:
501-
res = _array_strptime_with_fallback(
502-
arg, name, tz, fmt, exact, errors, infer_datetime_format
503-
)
504-
if res is not None:
505-
return res
500+
res = _array_strptime_with_fallback(
501+
arg, name, tz, fmt, exact, errors, infer_datetime_format
502+
)
503+
return res
506504

507-
except ValueError as e:
505+
except ValueError as err:
508506
# Fallback to try to convert datetime objects if timezone-aware
509507
# datetime objects are found without passing `utc=True`
510508
try:
511509
values, tz = conversion.datetime_to_datetime64(arg)
512510
dta = DatetimeArray(values, dtype=tz_to_dtype(tz))
513511
return DatetimeIndex._simple_new(dta, name=name)
514512
except (ValueError, TypeError):
515-
raise e
516-
517-
# error: Incompatible return value type (got "Optional[ndarray]", expected
518-
# "Optional[Index]")
519-
return result # type: ignore[return-value]
513+
raise err
520514

521515

522516
def _to_datetime_with_unit(arg, unit, name, tz, errors: str) -> Index:

pandas/core/window/rolling.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class BaseWindow(SelectionMixin):
111111

112112
_attributes: list[str] = []
113113
exclusions: frozenset[Hashable] = frozenset()
114+
_on: Index
114115

115116
def __init__(
116117
self,
@@ -169,7 +170,7 @@ def win_type(self):
169170
return self._win_type
170171

171172
@property
172-
def is_datetimelike(self):
173+
def is_datetimelike(self) -> bool:
173174
warnings.warn(
174175
"is_datetimelike is deprecated and will be removed in a future version.",
175176
FutureWarning,
@@ -329,7 +330,7 @@ def _prep_values(self, values: ArrayLike) -> np.ndarray:
329330
# expected "ndarray")
330331
return values # type: ignore[return-value]
331332

332-
def _insert_on_column(self, result: DataFrame, obj: DataFrame):
333+
def _insert_on_column(self, result: DataFrame, obj: DataFrame) -> None:
333334
# if we have an 'on' column we want to put it back into
334335
# the results in the same location
335336
from pandas import Series

pandas/tests/tools/test_to_datetime.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ def test_iso8601_strings_mixed_offsets_with_naive(self):
10791079

10801080
def test_mixed_offsets_with_native_datetime_raises(self):
10811081
# GH 25978
1082-
s = Series(
1082+
ser = Series(
10831083
[
10841084
"nan",
10851085
Timestamp("1990-01-01"),
@@ -1089,7 +1089,7 @@ def test_mixed_offsets_with_native_datetime_raises(self):
10891089
]
10901090
)
10911091
with pytest.raises(ValueError, match="Tz-aware datetime.datetime"):
1092-
to_datetime(s)
1092+
to_datetime(ser)
10931093

10941094
def test_non_iso_strings_with_tz_offset(self):
10951095
result = to_datetime(["March 1, 2018 12:00:00+0400"] * 2)

0 commit comments

Comments
 (0)