Skip to content

Commit 3e10647

Browse files
authored
CLN: assorted (#55618)
1 parent 5c7d89d commit 3e10647

File tree

18 files changed

+67
-41
lines changed

18 files changed

+67
-41
lines changed

pandas/_libs/tslibs/np_datetime.pyx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ import operator
484484

485485

486486
cdef int op_to_op_code(op):
487-
# TODO: should exist somewhere?
488487
if op is operator.eq:
489488
return Py_EQ
490489
if op is operator.ne:

pandas/core/arrays/categorical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2165,7 +2165,7 @@ def __contains__(self, key) -> bool:
21652165
# Rendering Methods
21662166

21672167
def _formatter(self, boxed: bool = False):
2168-
# Defer to CategoricalFormatter's formatter.
2168+
# Returning None here will cause format_array to do inference.
21692169
return None
21702170

21712171
def _repr_categories(self) -> list[str]:

pandas/core/arrays/datetimelike.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2013,8 +2013,9 @@ def freq(self, value) -> None:
20132013

20142014
self._freq = value
20152015

2016+
@final
20162017
@classmethod
2017-
def _validate_frequency(cls, index, freq, **kwargs):
2018+
def _validate_frequency(cls, index, freq: BaseOffset, **kwargs):
20182019
"""
20192020
Validate that a frequency is compatible with the values of a given
20202021
Datetime Array/Index or Timedelta Array/Index

pandas/core/arrays/datetimes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2266,7 +2266,8 @@ def _sequence_to_dt64ns(
22662266

22672267
elif lib.is_np_dtype(data_dtype, "M"):
22682268
# tz-naive DatetimeArray or ndarray[datetime64]
2269-
data = getattr(data, "_ndarray", data)
2269+
if isinstance(data, DatetimeArray):
2270+
data = data._ndarray
22702271
new_dtype = data.dtype
22712272
data_unit = get_unit_from_dtype(new_dtype)
22722273
if not is_supported_unit(data_unit):

pandas/core/dtypes/dtypes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from pandas.core.dtypes.generic import (
5656
ABCCategoricalIndex,
5757
ABCIndex,
58+
ABCRangeIndex,
5859
)
5960
from pandas.core.dtypes.inference import (
6061
is_bool,
@@ -464,8 +465,7 @@ def __repr__(self) -> str_type:
464465
dtype = "None"
465466
else:
466467
data = self.categories._format_data(name=type(self).__name__)
467-
if data is None:
468-
# self.categories is RangeIndex
468+
if isinstance(self.categories, ABCRangeIndex):
469469
data = str(self.categories._range)
470470
data = data.rstrip(", ")
471471
dtype = self.categories.dtype

pandas/core/indexes/base.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
ABCIntervalIndex,
128128
ABCMultiIndex,
129129
ABCPeriodIndex,
130+
ABCRangeIndex,
130131
ABCSeries,
131132
ABCTimedeltaIndex,
132133
)
@@ -1292,11 +1293,6 @@ def __repr__(self) -> str_t:
12921293
attrs_str = [f"{k}={v}" for k, v in attrs]
12931294
prepr = ", ".join(attrs_str)
12941295

1295-
# no data provided, just attributes
1296-
if data is None:
1297-
# i.e. RangeIndex
1298-
data = ""
1299-
13001296
return f"{klass_name}({data}{prepr})"
13011297

13021298
@property
@@ -1306,6 +1302,7 @@ def _formatter_func(self):
13061302
"""
13071303
return default_pprint
13081304

1305+
@final
13091306
def _format_data(self, name=None) -> str_t:
13101307
"""
13111308
Return the formatted data as a unicode string.
@@ -1319,6 +1316,9 @@ def _format_data(self, name=None) -> str_t:
13191316
self = cast("CategoricalIndex", self)
13201317
if is_object_dtype(self.categories.dtype):
13211318
is_justify = False
1319+
elif isinstance(self, ABCRangeIndex):
1320+
# We will do the relevant formatting via attrs
1321+
return ""
13221322

13231323
return format_object_summary(
13241324
self,
@@ -6961,6 +6961,7 @@ def drop(
69616961
indexer = indexer[~mask]
69626962
return self.delete(indexer)
69636963

6964+
@final
69646965
def infer_objects(self, copy: bool = True) -> Index:
69656966
"""
69666967
If we have an object dtype, try to infer a non-object dtype.
@@ -6992,6 +6993,7 @@ def infer_objects(self, copy: bool = True) -> Index:
69926993
result._references.add_index_reference(result)
69936994
return result
69946995

6996+
@final
69956997
def diff(self, periods: int = 1) -> Self:
69966998
"""
69976999
Computes the difference between consecutive values in the Index object.
@@ -7020,6 +7022,7 @@ def diff(self, periods: int = 1) -> Self:
70207022
"""
70217023
return self._constructor(self.to_series().diff(periods))
70227024

7025+
@final
70237026
def round(self, decimals: int = 0) -> Self:
70247027
"""
70257028
Round each value in the Index to the given number of decimals.

pandas/core/indexes/datetimes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,10 @@ def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
417417
# --------------------------------------------------------------------
418418
# Rendering Methods
419419

420-
@property
420+
@cache_readonly
421421
def _formatter_func(self):
422+
# Note this is equivalent to the DatetimeIndexOpsMixin method but
423+
# uses the maybe-cached self._is_dates_only instead of re-computing it.
422424
from pandas.io.formats.format import get_format_datetime64
423425

424426
formatter = get_format_datetime64(is_dates_only=self._is_dates_only)

pandas/core/indexes/range.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def _data(self) -> np.ndarray: # type: ignore[override]
240240
"""
241241
return np.arange(self.start, self.stop, self.step, dtype=np.int64)
242242

243-
def _get_data_as_items(self):
243+
def _get_data_as_items(self) -> list[tuple[str, int]]:
244244
"""return a list of tuples of start, stop, step"""
245245
rng = self._range
246246
return [("start", rng.start), ("stop", rng.stop), ("step", rng.step)]
@@ -257,15 +257,11 @@ def _format_attrs(self):
257257
"""
258258
Return a list of tuples of the (attr, formatted_value)
259259
"""
260-
attrs = self._get_data_as_items()
260+
attrs = cast("list[tuple[str, str | int]]", self._get_data_as_items())
261261
if self._name is not None:
262262
attrs.append(("name", ibase.default_pprint(self._name)))
263263
return attrs
264264

265-
def _format_data(self, name=None):
266-
# we are formatting thru the attributes
267-
return None
268-
269265
def _format_with_header(self, *, header: list[str], na_rep: str) -> list[str]:
270266
# Equivalent to Index implementation, but faster
271267
if not len(self._range):

pandas/core/resample.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ def __init__(
189189
else:
190190
self.exclusions = frozenset()
191191

192+
@final
192193
def __str__(self) -> str:
193194
"""
194195
Provide a nice str repr of our rolling object.
@@ -200,6 +201,7 @@ def __str__(self) -> str:
200201
)
201202
return f"{type(self).__name__} [{', '.join(attrs)}]"
202203

204+
@final
203205
def __getattr__(self, attr: str):
204206
if attr in self._internal_names_set:
205207
return object.__getattribute__(self, attr)
@@ -210,6 +212,7 @@ def __getattr__(self, attr: str):
210212

211213
return object.__getattribute__(self, attr)
212214

215+
@final
213216
@property
214217
def _from_selection(self) -> bool:
215218
"""
@@ -249,6 +252,7 @@ def _get_binner(self):
249252
bin_grouper = BinGrouper(bins, binlabels, indexer=self._indexer)
250253
return binner, bin_grouper
251254

255+
@final
252256
@Substitution(
253257
klass="Resampler",
254258
examples="""
@@ -334,6 +338,7 @@ def pipe(
334338
"""
335339
)
336340

341+
@final
337342
@doc(
338343
_shared_docs["aggregate"],
339344
see_also=_agg_see_also_doc,
@@ -352,6 +357,7 @@ def aggregate(self, func=None, *args, **kwargs):
352357
agg = aggregate
353358
apply = aggregate
354359

360+
@final
355361
def transform(self, arg, *args, **kwargs):
356362
"""
357363
Call function producing a like-indexed Series on each group.
@@ -471,6 +477,7 @@ def _groupby_and_aggregate(self, how, *args, **kwargs):
471477

472478
return self._wrap_result(result)
473479

480+
@final
474481
def _get_resampler_for_grouping(
475482
self, groupby: GroupBy, key, include_groups: bool = True
476483
):
@@ -506,6 +513,7 @@ def _wrap_result(self, result):
506513

507514
return result
508515

516+
@final
509517
def ffill(self, limit: int | None = None):
510518
"""
511519
Forward fill the values.
@@ -574,6 +582,7 @@ def ffill(self, limit: int | None = None):
574582
"""
575583
return self._upsample("ffill", limit=limit)
576584

585+
@final
577586
def nearest(self, limit: int | None = None):
578587
"""
579588
Resample by using the nearest value.
@@ -634,6 +643,7 @@ def nearest(self, limit: int | None = None):
634643
"""
635644
return self._upsample("nearest", limit=limit)
636645

646+
@final
637647
def bfill(self, limit: int | None = None):
638648
"""
639649
Backward fill the new missing values in the resampled data.
@@ -736,6 +746,7 @@ def bfill(self, limit: int | None = None):
736746
"""
737747
return self._upsample("bfill", limit=limit)
738748

749+
@final
739750
def fillna(self, method, limit: int | None = None):
740751
"""
741752
Fill missing values introduced by upsampling.
@@ -903,6 +914,7 @@ def fillna(self, method, limit: int | None = None):
903914
)
904915
return self._upsample(method, limit=limit)
905916

917+
@final
906918
def interpolate(
907919
self,
908920
method: InterpolateOptions = "linear",
@@ -1084,6 +1096,7 @@ def interpolate(
10841096
**kwargs,
10851097
)
10861098

1099+
@final
10871100
def asfreq(self, fill_value=None):
10881101
"""
10891102
Return the values at the new freq, essentially a reindex.
@@ -1122,6 +1135,7 @@ def asfreq(self, fill_value=None):
11221135
"""
11231136
return self._upsample("asfreq", fill_value=fill_value)
11241137

1138+
@final
11251139
def sum(
11261140
self,
11271141
numeric_only: bool = False,
@@ -1169,6 +1183,7 @@ def sum(
11691183
nv.validate_resampler_func("sum", args, kwargs)
11701184
return self._downsample("sum", numeric_only=numeric_only, min_count=min_count)
11711185

1186+
@final
11721187
def prod(
11731188
self,
11741189
numeric_only: bool = False,
@@ -1216,6 +1231,7 @@ def prod(
12161231
nv.validate_resampler_func("prod", args, kwargs)
12171232
return self._downsample("prod", numeric_only=numeric_only, min_count=min_count)
12181233

1234+
@final
12191235
def min(
12201236
self,
12211237
numeric_only: bool = False,
@@ -1250,6 +1266,7 @@ def min(
12501266
nv.validate_resampler_func("min", args, kwargs)
12511267
return self._downsample("min", numeric_only=numeric_only, min_count=min_count)
12521268

1269+
@final
12531270
def max(
12541271
self,
12551272
numeric_only: bool = False,
@@ -1283,6 +1300,7 @@ def max(
12831300
nv.validate_resampler_func("max", args, kwargs)
12841301
return self._downsample("max", numeric_only=numeric_only, min_count=min_count)
12851302

1303+
@final
12861304
@doc(GroupBy.first)
12871305
def first(
12881306
self,
@@ -1295,6 +1313,7 @@ def first(
12951313
nv.validate_resampler_func("first", args, kwargs)
12961314
return self._downsample("first", numeric_only=numeric_only, min_count=min_count)
12971315

1316+
@final
12981317
@doc(GroupBy.last)
12991318
def last(
13001319
self,
@@ -1307,12 +1326,14 @@ def last(
13071326
nv.validate_resampler_func("last", args, kwargs)
13081327
return self._downsample("last", numeric_only=numeric_only, min_count=min_count)
13091328

1329+
@final
13101330
@doc(GroupBy.median)
13111331
def median(self, numeric_only: bool = False, *args, **kwargs):
13121332
maybe_warn_args_and_kwargs(type(self), "median", args, kwargs)
13131333
nv.validate_resampler_func("median", args, kwargs)
13141334
return self._downsample("median", numeric_only=numeric_only)
13151335

1336+
@final
13161337
def mean(
13171338
self,
13181339
numeric_only: bool = False,
@@ -1356,6 +1377,7 @@ def mean(
13561377
nv.validate_resampler_func("mean", args, kwargs)
13571378
return self._downsample("mean", numeric_only=numeric_only)
13581379

1380+
@final
13591381
def std(
13601382
self,
13611383
ddof: int = 1,
@@ -1403,6 +1425,7 @@ def std(
14031425
nv.validate_resampler_func("std", args, kwargs)
14041426
return self._downsample("std", ddof=ddof, numeric_only=numeric_only)
14051427

1428+
@final
14061429
def var(
14071430
self,
14081431
ddof: int = 1,
@@ -1456,6 +1479,7 @@ def var(
14561479
nv.validate_resampler_func("var", args, kwargs)
14571480
return self._downsample("var", ddof=ddof, numeric_only=numeric_only)
14581481

1482+
@final
14591483
@doc(GroupBy.sem)
14601484
def sem(
14611485
self,
@@ -1468,6 +1492,7 @@ def sem(
14681492
nv.validate_resampler_func("sem", args, kwargs)
14691493
return self._downsample("sem", ddof=ddof, numeric_only=numeric_only)
14701494

1495+
@final
14711496
@doc(GroupBy.ohlc)
14721497
def ohlc(
14731498
self,
@@ -1495,6 +1520,7 @@ def ohlc(
14951520

14961521
return self._downsample("ohlc")
14971522

1523+
@final
14981524
@doc(SeriesGroupBy.nunique)
14991525
def nunique(
15001526
self,
@@ -1505,6 +1531,7 @@ def nunique(
15051531
nv.validate_resampler_func("nunique", args, kwargs)
15061532
return self._downsample("nunique")
15071533

1534+
@final
15081535
@doc(GroupBy.size)
15091536
def size(self):
15101537
result = self._downsample("size")
@@ -1524,6 +1551,7 @@ def size(self):
15241551
result = Series([], index=result.index, dtype="int64", name=name)
15251552
return result
15261553

1554+
@final
15271555
@doc(GroupBy.count)
15281556
def count(self):
15291557
result = self._downsample("count")
@@ -1541,6 +1569,7 @@ def count(self):
15411569

15421570
return result
15431571

1572+
@final
15441573
def quantile(self, q: float | list[float] | AnyArrayLike = 0.5, **kwargs):
15451574
"""
15461575
Return value at the given quantile.

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ def _ixs(self, i: int, axis: AxisInt = 0) -> Any:
10471047
10481048
Returns
10491049
-------
1050-
scalar (int) or Series (slice, sequence)
1050+
scalar
10511051
"""
10521052
return self._values[i]
10531053

0 commit comments

Comments
 (0)