Skip to content

Commit f94bdc4

Browse files
authored
CLN: assorted (#52820)
* CLN: assorted * typo fixup
1 parent bb8067f commit f94bdc4

File tree

18 files changed

+108
-63
lines changed

18 files changed

+108
-63
lines changed

pandas/_libs/tslibs/conversion.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ from pandas._libs.tslibs.nattype cimport (
6161
c_nat_strings as nat_strings,
6262
)
6363
from pandas._libs.tslibs.parsing cimport parse_datetime_string
64-
from pandas._libs.tslibs.timestamps cimport _Timestamp
6564
from pandas._libs.tslibs.timezones cimport (
6665
get_utcoffset,
6766
is_utc,
@@ -761,7 +760,7 @@ cdef int64_t parse_pydatetime(
761760
_ts.ensure_reso(NPY_FR_ns)
762761
result = _ts.value
763762
else:
764-
if isinstance(val, _Timestamp):
763+
if isinstance(val, ABCTimestamp):
765764
result = val.as_unit("ns")._value
766765
else:
767766
result = pydatetime_to_dt64(val, dts)

pandas/_libs/tslibs/timestamps.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ from numpy cimport (
2222

2323
cnp.import_array()
2424

25-
from cpython.datetime cimport ( # alias bc `tzinfo` is a kwarg below
25+
from cpython.datetime cimport ( # alias tzinfo_type bc `tzinfo` is a kwarg below
2626
PyDate_Check,
2727
PyDateTime_Check,
2828
PyDelta_Check,

pandas/_libs/tslibs/util.pxd

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ cdef extern from "Python.h":
1010
bint PyComplex_Check(object obj) nogil
1111
bint PyObject_TypeCheck(object obj, PyTypeObject* type) nogil
1212

13+
# TODO(cython3): cimport this, xref GH#49670
1314
# Note that following functions can potentially raise an exception,
1415
# thus they cannot be declared 'nogil'. Also PyUnicode_AsUTF8AndSize() can
1516
# potentially allocate memory inside in unlikely case of when underlying

pandas/core/computation/align.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,8 @@ def _align_core(terms):
133133
w, category=PerformanceWarning, stacklevel=find_stack_level()
134134
)
135135

136-
f = partial(ti.reindex, reindexer, axis=axis, copy=False)
137-
138-
terms[i].update(f())
136+
obj = ti.reindex(reindexer, axis=axis, copy=False)
137+
terms[i].update(obj)
139138

140139
terms[i].update(terms[i].value.values)
141140

pandas/core/dtypes/cast.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
ensure_str,
5151
is_bool,
5252
is_complex,
53-
is_extension_array_dtype,
5453
is_float,
5554
is_integer,
5655
is_object_dtype,
@@ -903,7 +902,8 @@ def infer_dtype_from_array(
903902
if not is_list_like(arr):
904903
raise TypeError("'arr' must be list-like")
905904

906-
if pandas_dtype and is_extension_array_dtype(arr):
905+
arr_dtype = getattr(arr, "dtype", None)
906+
if pandas_dtype and isinstance(arr_dtype, ExtensionDtype):
907907
return arr.dtype, arr
908908

909909
elif isinstance(arr, ABCSeries):

pandas/core/dtypes/missing.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
TD64NS_DTYPE,
2727
ensure_object,
2828
is_dtype_equal,
29-
is_extension_array_dtype,
3029
is_scalar,
3130
is_string_or_object_np_dtype,
3231
)
@@ -56,6 +55,7 @@
5655
npt,
5756
)
5857

58+
from pandas import Series
5959
from pandas.core.indexes.base import Index
6060

6161

@@ -642,11 +642,11 @@ def na_value_for_dtype(dtype: DtypeObj, compat: bool = True):
642642
return np.nan
643643

644644

645-
def remove_na_arraylike(arr):
645+
def remove_na_arraylike(arr: Series | Index | np.ndarray):
646646
"""
647647
Return array-like containing only true/non-NaN values, possibly empty.
648648
"""
649-
if is_extension_array_dtype(arr):
649+
if isinstance(arr.dtype, ExtensionDtype):
650650
return arr[notna(arr)]
651651
else:
652652
return arr[notna(np.asarray(arr))]

pandas/core/generic.py

+26
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ def _get_cleaned_column_resolvers(self) -> dict[Hashable, Series]:
590590
clean_column_name(k): v for k, v in self.items() if not isinstance(k, int)
591591
}
592592

593+
@final
593594
@property
594595
def _info_axis(self) -> Index:
595596
return getattr(self, self._info_axis_name)
@@ -969,6 +970,7 @@ def squeeze(self, axis: Axis | None = None):
969970
# ----------------------------------------------------------------------
970971
# Rename
971972

973+
@final
972974
def _rename(
973975
self,
974976
mapper: Renamer | None = None,
@@ -3476,6 +3478,7 @@ def _wrap(x, alt_format_):
34763478
render_kwargs=render_kwargs_,
34773479
)
34783480

3481+
@final
34793482
def _to_latex_via_styler(
34803483
self,
34813484
buf=None,
@@ -4293,6 +4296,7 @@ def _check_setitem_copy(self, t: str = "setting", force: bool_t = False):
42934296
if value == "warn":
42944297
warnings.warn(t, SettingWithCopyWarning, stacklevel=find_stack_level())
42954298

4299+
@final
42964300
def __delitem__(self, key) -> None:
42974301
"""
42984302
Delete item
@@ -6069,6 +6073,7 @@ def __finalize__(self, other, method: str | None = None, **kwargs) -> Self:
60696073

60706074
return self
60716075

6076+
@final
60726077
def __getattr__(self, name: str):
60736078
"""
60746079
After regular attribute access, try looking up the name
@@ -6085,6 +6090,7 @@ def __getattr__(self, name: str):
60856090
return self[name]
60866091
return object.__getattribute__(self, name)
60876092

6093+
@final
60886094
def __setattr__(self, name: str, value) -> None:
60896095
"""
60906096
After regular attribute access, try setting the name
@@ -6255,6 +6261,7 @@ def dtypes(self):
62556261
data = self._mgr.get_dtypes()
62566262
return self._constructor_sliced(data, index=self._info_axis, dtype=np.object_)
62576263

6264+
@final
62586265
def astype(
62596266
self, dtype, copy: bool_t | None = None, errors: IgnoreRaise = "raise"
62606267
) -> Self:
@@ -7128,6 +7135,7 @@ def ffill(
71287135
) -> Self | None:
71297136
...
71307137

7138+
@final
71317139
@doc(klass=_shared_doc_kwargs["klass"])
71327140
def ffill(
71337141
self,
@@ -7149,6 +7157,7 @@ def ffill(
71497157
method="ffill", axis=axis, inplace=inplace, limit=limit, downcast=downcast
71507158
)
71517159

7160+
@final
71527161
@doc(klass=_shared_doc_kwargs["klass"])
71537162
def pad(
71547163
self,
@@ -7211,6 +7220,7 @@ def bfill(
72117220
) -> Self | None:
72127221
...
72137222

7223+
@final
72147224
@doc(klass=_shared_doc_kwargs["klass"])
72157225
def bfill(
72167226
self,
@@ -7232,6 +7242,7 @@ def bfill(
72327242
method="bfill", axis=axis, inplace=inplace, limit=limit, downcast=downcast
72337243
)
72347244

7245+
@final
72357246
@doc(klass=_shared_doc_kwargs["klass"])
72367247
def backfill(
72377248
self,
@@ -7501,6 +7512,7 @@ def replace(
75017512
else:
75027513
return result.__finalize__(self, method="replace")
75037514

7515+
@final
75047516
def interpolate(
75057517
self,
75067518
method: Literal[
@@ -8185,6 +8197,7 @@ def _clip_with_one_bound(self, threshold, method, axis, inplace):
81858197
# GH 40420
81868198
return self.where(subset, threshold, axis=axis, inplace=inplace)
81878199

8200+
@final
81888201
def clip(
81898202
self,
81908203
lower=None,
@@ -9996,6 +10009,7 @@ def where(
999610009
) -> Self | None:
999710010
...
999810011

10012+
@final
999910013
@doc(
1000010014
klass=_shared_doc_kwargs["klass"],
1000110015
cond="True",
@@ -10188,6 +10202,7 @@ def mask(
1018810202
) -> Self | None:
1018910203
...
1019010204

10205+
@final
1019110206
@doc(
1019210207
where,
1019310208
klass=_shared_doc_kwargs["klass"],
@@ -10368,6 +10383,7 @@ def shift(
1036810383
result = self.set_axis(new_ax, axis=axis)
1036910384
return result.__finalize__(self, method="shift")
1037010385

10386+
@final
1037110387
def truncate(
1037210388
self,
1037310389
before=None,
@@ -11693,46 +11709,56 @@ def _inplace_method(self, other, op) -> Self:
1169311709
)
1169411710
return self
1169511711

11712+
@final
1169611713
def __iadd__(self, other) -> Self:
1169711714
# error: Unsupported left operand type for + ("Type[NDFrame]")
1169811715
return self._inplace_method(other, type(self).__add__) # type: ignore[operator]
1169911716

11717+
@final
1170011718
def __isub__(self, other) -> Self:
1170111719
# error: Unsupported left operand type for - ("Type[NDFrame]")
1170211720
return self._inplace_method(other, type(self).__sub__) # type: ignore[operator]
1170311721

11722+
@final
1170411723
def __imul__(self, other) -> Self:
1170511724
# error: Unsupported left operand type for * ("Type[NDFrame]")
1170611725
return self._inplace_method(other, type(self).__mul__) # type: ignore[operator]
1170711726

11727+
@final
1170811728
def __itruediv__(self, other) -> Self:
1170911729
# error: Unsupported left operand type for / ("Type[NDFrame]")
1171011730
return self._inplace_method(
1171111731
other, type(self).__truediv__ # type: ignore[operator]
1171211732
)
1171311733

11734+
@final
1171411735
def __ifloordiv__(self, other) -> Self:
1171511736
# error: Unsupported left operand type for // ("Type[NDFrame]")
1171611737
return self._inplace_method(
1171711738
other, type(self).__floordiv__ # type: ignore[operator]
1171811739
)
1171911740

11741+
@final
1172011742
def __imod__(self, other) -> Self:
1172111743
# error: Unsupported left operand type for % ("Type[NDFrame]")
1172211744
return self._inplace_method(other, type(self).__mod__) # type: ignore[operator]
1172311745

11746+
@final
1172411747
def __ipow__(self, other) -> Self:
1172511748
# error: Unsupported left operand type for ** ("Type[NDFrame]")
1172611749
return self._inplace_method(other, type(self).__pow__) # type: ignore[operator]
1172711750

11751+
@final
1172811752
def __iand__(self, other) -> Self:
1172911753
# error: Unsupported left operand type for & ("Type[NDFrame]")
1173011754
return self._inplace_method(other, type(self).__and__) # type: ignore[operator]
1173111755

11756+
@final
1173211757
def __ior__(self, other) -> Self:
1173311758
# error: Unsupported left operand type for | ("Type[NDFrame]")
1173411759
return self._inplace_method(other, type(self).__or__) # type: ignore[operator]
1173511760

11761+
@final
1173611762
def __ixor__(self, other) -> Self:
1173711763
# error: Unsupported left operand type for ^ ("Type[NDFrame]")
1173811764
return self._inplace_method(other, type(self).__xor__) # type: ignore[operator]

pandas/core/indexes/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -922,18 +922,20 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str_t, *inputs, **kwargs):
922922

923923
return self.__array_wrap__(result)
924924

925+
@final
925926
def __array_wrap__(self, result, context=None):
926927
"""
927928
Gets called after a ufunc and other functions e.g. np.split.
928929
"""
929930
result = lib.item_from_zerodim(result)
930931
if (
931-
(not isinstance(result, Index) and is_bool_dtype(result))
932+
(not isinstance(result, Index) and is_bool_dtype(result.dtype))
932933
or lib.is_scalar(result)
933934
or np.ndim(result) > 1
934935
):
935936
# exclude Index to avoid warning from is_bool_dtype deprecation;
936937
# in the Index case it doesn't matter which path we go down.
938+
# reached in plotting tests with e.g. np.nonzero(index)
937939
return result
938940

939941
return Index(result, name=self.name)

pandas/core/indexes/category.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
)
1818

1919
from pandas.core.dtypes.common import is_scalar
20+
from pandas.core.dtypes.concat import concat_compat
2021
from pandas.core.dtypes.dtypes import CategoricalDtype
2122
from pandas.core.dtypes.missing import (
2223
is_valid_na_for_dtype,
@@ -478,7 +479,6 @@ def _concat(self, to_concat: list[Index], name: Hashable) -> Index:
478479
)
479480
except TypeError:
480481
# not all to_concat elements are among our categories (or NA)
481-
from pandas.core.dtypes.concat import concat_compat
482482

483483
res = concat_compat([x._values for x in to_concat])
484484
return Index(res, name=name)

pandas/core/indexes/interval.py

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113

114114

115115
def _get_next_label(label):
116+
# see test_slice_locs_with_ints_and_floats_succeeds
116117
dtype = getattr(label, "dtype", type(label))
117118
if isinstance(label, (Timestamp, Timedelta)):
118119
dtype = "datetime64[ns]"
@@ -129,6 +130,7 @@ def _get_next_label(label):
129130

130131

131132
def _get_prev_label(label):
133+
# see test_slice_locs_with_ints_and_floats_succeeds
132134
dtype = getattr(label, "dtype", type(label))
133135
if isinstance(label, (Timestamp, Timedelta)):
134136
dtype = "datetime64[ns]"

pandas/core/reshape/melt.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010

1111
from pandas.util._decorators import Appender
1212

13-
from pandas.core.dtypes.common import (
14-
is_extension_array_dtype,
15-
is_list_like,
16-
)
13+
from pandas.core.dtypes.common import is_list_like
1714
from pandas.core.dtypes.concat import concat_compat
1815
from pandas.core.dtypes.missing import notna
1916

@@ -126,7 +123,8 @@ def melt(
126123
mdata: dict[Hashable, AnyArrayLike] = {}
127124
for col in id_vars:
128125
id_data = frame.pop(col)
129-
if is_extension_array_dtype(id_data):
126+
if not isinstance(id_data.dtype, np.dtype):
127+
# i.e. ExtensionDtype
130128
if K > 0:
131129
mdata[col] = concat([id_data] * K, ignore_index=True)
132130
else:

pandas/core/reshape/pivot.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
from pandas.core.dtypes.cast import maybe_downcast_to_dtype
2020
from pandas.core.dtypes.common import (
21-
is_extension_array_dtype,
2221
is_integer_dtype,
2322
is_list_like,
2423
is_nested_list_like,
2524
is_scalar,
2625
)
26+
from pandas.core.dtypes.dtypes import ExtensionDtype
2727
from pandas.core.dtypes.generic import (
2828
ABCDataFrame,
2929
ABCSeries,
@@ -326,7 +326,7 @@ def _add_margins(
326326
row_names = result.index.names
327327
# check the result column and leave floats
328328
for dtype in set(result.dtypes):
329-
if is_extension_array_dtype(dtype):
329+
if isinstance(dtype, ExtensionDtype):
330330
# Can hold NA already
331331
continue
332332

0 commit comments

Comments
 (0)