Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLN: assorted #55618

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pandas/_libs/tslibs/np_datetime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ import operator


cdef int op_to_op_code(op):
# TODO: should exist somewhere?
if op is operator.eq:
return Py_EQ
if op is operator.ne:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2165,7 +2165,7 @@ def __contains__(self, key) -> bool:
# Rendering Methods

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

def _repr_categories(self) -> list[str]:
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -2013,8 +2013,9 @@ def freq(self, value) -> None:

self._freq = value

@final
@classmethod
def _validate_frequency(cls, index, freq, **kwargs):
def _validate_frequency(cls, index, freq: BaseOffset, **kwargs):
"""
Validate that a frequency is compatible with the values of a given
Datetime Array/Index or Timedelta Array/Index
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2266,7 +2266,8 @@ def _sequence_to_dt64ns(

elif lib.is_np_dtype(data_dtype, "M"):
# tz-naive DatetimeArray or ndarray[datetime64]
data = getattr(data, "_ndarray", data)
if isinstance(data, DatetimeArray):
data = data._ndarray
new_dtype = data.dtype
data_unit = get_unit_from_dtype(new_dtype)
if not is_supported_unit(data_unit):
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
from pandas.core.dtypes.generic import (
ABCCategoricalIndex,
ABCIndex,
ABCRangeIndex,
)
from pandas.core.dtypes.inference import (
is_bool,
Expand Down Expand Up @@ -464,8 +465,7 @@ def __repr__(self) -> str_type:
dtype = "None"
else:
data = self.categories._format_data(name=type(self).__name__)
if data is None:
# self.categories is RangeIndex
if isinstance(self.categories, ABCRangeIndex):
data = str(self.categories._range)
data = data.rstrip(", ")
dtype = self.categories.dtype
Expand Down
13 changes: 8 additions & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
ABCIntervalIndex,
ABCMultiIndex,
ABCPeriodIndex,
ABCRangeIndex,
ABCSeries,
ABCTimedeltaIndex,
)
Expand Down Expand Up @@ -1292,11 +1293,6 @@ def __repr__(self) -> str_t:
attrs_str = [f"{k}={v}" for k, v in attrs]
prepr = ", ".join(attrs_str)

# no data provided, just attributes
if data is None:
# i.e. RangeIndex
data = ""

return f"{klass_name}({data}{prepr})"

@property
Expand All @@ -1306,6 +1302,7 @@ def _formatter_func(self):
"""
return default_pprint

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

return format_object_summary(
self,
Expand Down Expand Up @@ -6961,6 +6961,7 @@ def drop(
indexer = indexer[~mask]
return self.delete(indexer)

@final
def infer_objects(self, copy: bool = True) -> Index:
"""
If we have an object dtype, try to infer a non-object dtype.
Expand Down Expand Up @@ -6992,6 +6993,7 @@ def infer_objects(self, copy: bool = True) -> Index:
result._references.add_index_reference(result)
return result

@final
def diff(self, periods: int = 1) -> Self:
"""
Computes the difference between consecutive values in the Index object.
Expand Down Expand Up @@ -7020,6 +7022,7 @@ def diff(self, periods: int = 1) -> Self:
"""
return self._constructor(self.to_series().diff(periods))

@final
def round(self, decimals: int = 0) -> Self:
"""
Round each value in the Index to the given number of decimals.
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,10 @@ def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
# --------------------------------------------------------------------
# Rendering Methods

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

formatter = get_format_datetime64(is_dates_only=self._is_dates_only)
Expand Down
8 changes: 2 additions & 6 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def _data(self) -> np.ndarray: # type: ignore[override]
"""
return np.arange(self.start, self.stop, self.step, dtype=np.int64)

def _get_data_as_items(self):
def _get_data_as_items(self) -> list[tuple[str, int]]:
"""return a list of tuples of start, stop, step"""
rng = self._range
return [("start", rng.start), ("stop", rng.stop), ("step", rng.step)]
Expand All @@ -257,15 +257,11 @@ def _format_attrs(self):
"""
Return a list of tuples of the (attr, formatted_value)
"""
attrs = self._get_data_as_items()
attrs = cast("list[tuple[str, str | int]]", self._get_data_as_items())
if self._name is not None:
attrs.append(("name", ibase.default_pprint(self._name)))
return attrs

def _format_data(self, name=None):
# we are formatting thru the attributes
return None

def _format_with_header(self, *, header: list[str], na_rep: str) -> list[str]:
# Equivalent to Index implementation, but faster
if not len(self._range):
Expand Down
29 changes: 29 additions & 0 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def __init__(
else:
self.exclusions = frozenset()

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

@final
def __getattr__(self, attr: str):
if attr in self._internal_names_set:
return object.__getattribute__(self, attr)
Expand All @@ -210,6 +212,7 @@ def __getattr__(self, attr: str):

return object.__getattribute__(self, attr)

@final
@property
def _from_selection(self) -> bool:
"""
Expand Down Expand Up @@ -249,6 +252,7 @@ def _get_binner(self):
bin_grouper = BinGrouper(bins, binlabels, indexer=self._indexer)
return binner, bin_grouper

@final
@Substitution(
klass="Resampler",
examples="""
Expand Down Expand Up @@ -334,6 +338,7 @@ def pipe(
"""
)

@final
@doc(
_shared_docs["aggregate"],
see_also=_agg_see_also_doc,
Expand All @@ -352,6 +357,7 @@ def aggregate(self, func=None, *args, **kwargs):
agg = aggregate
apply = aggregate

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

return self._wrap_result(result)

@final
def _get_resampler_for_grouping(
self, groupby: GroupBy, key, include_groups: bool = True
):
Expand Down Expand Up @@ -506,6 +513,7 @@ def _wrap_result(self, result):

return result

@final
def ffill(self, limit: int | None = None):
"""
Forward fill the values.
Expand Down Expand Up @@ -574,6 +582,7 @@ def ffill(self, limit: int | None = None):
"""
return self._upsample("ffill", limit=limit)

@final
def nearest(self, limit: int | None = None):
"""
Resample by using the nearest value.
Expand Down Expand Up @@ -634,6 +643,7 @@ def nearest(self, limit: int | None = None):
"""
return self._upsample("nearest", limit=limit)

@final
def bfill(self, limit: int | None = None):
"""
Backward fill the new missing values in the resampled data.
Expand Down Expand Up @@ -736,6 +746,7 @@ def bfill(self, limit: int | None = None):
"""
return self._upsample("bfill", limit=limit)

@final
def fillna(self, method, limit: int | None = None):
"""
Fill missing values introduced by upsampling.
Expand Down Expand Up @@ -903,6 +914,7 @@ def fillna(self, method, limit: int | None = None):
)
return self._upsample(method, limit=limit)

@final
def interpolate(
self,
method: InterpolateOptions = "linear",
Expand Down Expand Up @@ -1084,6 +1096,7 @@ def interpolate(
**kwargs,
)

@final
def asfreq(self, fill_value=None):
"""
Return the values at the new freq, essentially a reindex.
Expand Down Expand Up @@ -1122,6 +1135,7 @@ def asfreq(self, fill_value=None):
"""
return self._upsample("asfreq", fill_value=fill_value)

@final
def sum(
self,
numeric_only: bool = False,
Expand Down Expand Up @@ -1169,6 +1183,7 @@ def sum(
nv.validate_resampler_func("sum", args, kwargs)
return self._downsample("sum", numeric_only=numeric_only, min_count=min_count)

@final
def prod(
self,
numeric_only: bool = False,
Expand Down Expand Up @@ -1216,6 +1231,7 @@ def prod(
nv.validate_resampler_func("prod", args, kwargs)
return self._downsample("prod", numeric_only=numeric_only, min_count=min_count)

@final
def min(
self,
numeric_only: bool = False,
Expand Down Expand Up @@ -1250,6 +1266,7 @@ def min(
nv.validate_resampler_func("min", args, kwargs)
return self._downsample("min", numeric_only=numeric_only, min_count=min_count)

@final
def max(
self,
numeric_only: bool = False,
Expand Down Expand Up @@ -1283,6 +1300,7 @@ def max(
nv.validate_resampler_func("max", args, kwargs)
return self._downsample("max", numeric_only=numeric_only, min_count=min_count)

@final
@doc(GroupBy.first)
def first(
self,
Expand All @@ -1295,6 +1313,7 @@ def first(
nv.validate_resampler_func("first", args, kwargs)
return self._downsample("first", numeric_only=numeric_only, min_count=min_count)

@final
@doc(GroupBy.last)
def last(
self,
Expand All @@ -1307,12 +1326,14 @@ def last(
nv.validate_resampler_func("last", args, kwargs)
return self._downsample("last", numeric_only=numeric_only, min_count=min_count)

@final
@doc(GroupBy.median)
def median(self, numeric_only: bool = False, *args, **kwargs):
maybe_warn_args_and_kwargs(type(self), "median", args, kwargs)
nv.validate_resampler_func("median", args, kwargs)
return self._downsample("median", numeric_only=numeric_only)

@final
def mean(
self,
numeric_only: bool = False,
Expand Down Expand Up @@ -1356,6 +1377,7 @@ def mean(
nv.validate_resampler_func("mean", args, kwargs)
return self._downsample("mean", numeric_only=numeric_only)

@final
def std(
self,
ddof: int = 1,
Expand Down Expand Up @@ -1403,6 +1425,7 @@ def std(
nv.validate_resampler_func("std", args, kwargs)
return self._downsample("std", ddof=ddof, numeric_only=numeric_only)

@final
def var(
self,
ddof: int = 1,
Expand Down Expand Up @@ -1456,6 +1479,7 @@ def var(
nv.validate_resampler_func("var", args, kwargs)
return self._downsample("var", ddof=ddof, numeric_only=numeric_only)

@final
@doc(GroupBy.sem)
def sem(
self,
Expand All @@ -1468,6 +1492,7 @@ def sem(
nv.validate_resampler_func("sem", args, kwargs)
return self._downsample("sem", ddof=ddof, numeric_only=numeric_only)

@final
@doc(GroupBy.ohlc)
def ohlc(
self,
Expand Down Expand Up @@ -1495,6 +1520,7 @@ def ohlc(

return self._downsample("ohlc")

@final
@doc(SeriesGroupBy.nunique)
def nunique(
self,
Expand All @@ -1505,6 +1531,7 @@ def nunique(
nv.validate_resampler_func("nunique", args, kwargs)
return self._downsample("nunique")

@final
@doc(GroupBy.size)
def size(self):
result = self._downsample("size")
Expand All @@ -1524,6 +1551,7 @@ def size(self):
result = Series([], index=result.index, dtype="int64", name=name)
return result

@final
@doc(GroupBy.count)
def count(self):
result = self._downsample("count")
Expand All @@ -1541,6 +1569,7 @@ def count(self):

return result

@final
def quantile(self, q: float | list[float] | AnyArrayLike = 0.5, **kwargs):
"""
Return value at the given quantile.
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ def _ixs(self, i: int, axis: AxisInt = 0) -> Any:

Returns
-------
scalar (int) or Series (slice, sequence)
scalar
"""
return self._values[i]

Expand Down
Loading
Loading