Skip to content

Commit e38daf0

Browse files
authored
CLN: assorted (pandas-dev#49979)
1 parent f5323ab commit e38daf0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+109
-167
lines changed

pandas/_libs/tslibs/conversion.pyx

+7-7
Original file line numberDiff line numberDiff line change
@@ -666,20 +666,20 @@ cpdef inline datetime localize_pydatetime(datetime dt, tzinfo tz):
666666

667667

668668
cdef tzinfo convert_timezone(
669-
tzinfo tz_in,
670-
tzinfo tz_out,
671-
bint found_naive,
672-
bint found_tz,
673-
bint utc_convert,
669+
tzinfo tz_in,
670+
tzinfo tz_out,
671+
bint found_naive,
672+
bint found_tz,
673+
bint utc_convert,
674674
):
675675
"""
676676
Validate that ``tz_in`` can be converted/localized to ``tz_out``.
677677
678678
Parameters
679679
----------
680-
tz_in : tzinfo
680+
tz_in : tzinfo or None
681681
Timezone info of element being processed.
682-
tz_out : tzinfo
682+
tz_out : tzinfo or None
683683
Timezone info of output.
684684
found_naive : bool
685685
Whether a timezone-naive element has been found so far.

pandas/_testing/asserters.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ def assert_interval_array_equal(
531531
def assert_period_array_equal(left, right, obj: str = "PeriodArray") -> None:
532532
_check_isinstance(left, right, PeriodArray)
533533

534-
assert_numpy_array_equal(left._data, right._data, obj=f"{obj}._data")
534+
assert_numpy_array_equal(left._ndarray, right._ndarray, obj=f"{obj}._ndarray")
535535
assert_attr_equal("freq", left, right, obj=obj)
536536

537537

@@ -541,7 +541,7 @@ def assert_datetime_array_equal(
541541
__tracebackhide__ = True
542542
_check_isinstance(left, right, DatetimeArray)
543543

544-
assert_numpy_array_equal(left._data, right._data, obj=f"{obj}._data")
544+
assert_numpy_array_equal(left._ndarray, right._ndarray, obj=f"{obj}._ndarray")
545545
if check_freq:
546546
assert_attr_equal("freq", left, right, obj=obj)
547547
assert_attr_equal("tz", left, right, obj=obj)
@@ -552,7 +552,7 @@ def assert_timedelta_array_equal(
552552
) -> None:
553553
__tracebackhide__ = True
554554
_check_isinstance(left, right, TimedeltaArray)
555-
assert_numpy_array_equal(left._data, right._data, obj=f"{obj}._data")
555+
assert_numpy_array_equal(left._ndarray, right._ndarray, obj=f"{obj}._ndarray")
556556
if check_freq:
557557
assert_attr_equal("freq", left, right, obj=obj)
558558

pandas/core/arrays/base.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1646,13 +1646,11 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
16461646

16471647

16481648
class ExtensionArraySupportsAnyAll(ExtensionArray):
1649-
def any(self, *, skipna: bool = True) -> bool: # type: ignore[empty-body]
1650-
# error: Missing return statement
1651-
pass
1649+
def any(self, *, skipna: bool = True) -> bool:
1650+
raise AbstractMethodError(self)
16521651

1653-
def all(self, *, skipna: bool = True) -> bool: # type: ignore[empty-body]
1654-
# error: Missing return statement
1655-
pass
1652+
def all(self, *, skipna: bool = True) -> bool:
1653+
raise AbstractMethodError(self)
16561654

16571655

16581656
class ExtensionOpsMixin:

pandas/core/arrays/categorical.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
Literal,
1212
Sequence,
1313
TypeVar,
14-
Union,
1514
cast,
1615
overload,
1716
)
@@ -511,7 +510,7 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
511510
result = self.copy() if copy else self
512511

513512
elif is_categorical_dtype(dtype):
514-
dtype = cast("Union[str, CategoricalDtype]", dtype)
513+
dtype = cast(CategoricalDtype, dtype)
515514

516515
# GH 10696/18593/18630
517516
dtype = self.dtype.update_dtype(dtype)

pandas/core/arrays/datetimelike.py

-7
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,6 @@ def _check_compatible_with(self, other: DTScalarOrNaT) -> None:
257257
"""
258258
raise AbstractMethodError(self)
259259

260-
# ------------------------------------------------------------------
261-
# NDArrayBackedExtensionArray compat
262-
263-
@cache_readonly
264-
def _data(self) -> np.ndarray:
265-
return self._ndarray
266-
267260
# ------------------------------------------------------------------
268261

269262
def _box_func(self, x):

pandas/core/dtypes/cast.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1137,9 +1137,7 @@ def maybe_cast_to_datetime(
11371137

11381138
# TODO: _from_sequence would raise ValueError in cases where
11391139
# _ensure_nanosecond_dtype raises TypeError
1140-
# Incompatible types in assignment (expression has type "Union[dtype[Any],
1141-
# ExtensionDtype]", variable has type "Optional[dtype[Any]]")
1142-
dtype = _ensure_nanosecond_dtype(dtype) # type: ignore[assignment]
1140+
_ensure_nanosecond_dtype(dtype)
11431141

11441142
if is_timedelta64_dtype(dtype):
11451143
res = TimedeltaArray._from_sequence(value, dtype=dtype)
@@ -1177,12 +1175,11 @@ def sanitize_to_nanoseconds(values: np.ndarray, copy: bool = False) -> np.ndarra
11771175
return values
11781176

11791177

1180-
def _ensure_nanosecond_dtype(dtype: DtypeObj) -> DtypeObj:
1178+
def _ensure_nanosecond_dtype(dtype: DtypeObj) -> None:
11811179
"""
11821180
Convert dtypes with granularity less than nanosecond to nanosecond
11831181
11841182
>>> _ensure_nanosecond_dtype(np.dtype("M8[us]"))
1185-
dtype('<M8[us]')
11861183
11871184
>>> _ensure_nanosecond_dtype(np.dtype("M8[D]"))
11881185
Traceback (most recent call last):
@@ -1219,7 +1216,6 @@ def _ensure_nanosecond_dtype(dtype: DtypeObj) -> DtypeObj:
12191216
f"dtype={dtype} is not supported. Supported resolutions are 's', "
12201217
"'ms', 'us', and 'ns'"
12211218
)
1222-
return dtype
12231219

12241220

12251221
# TODO: other value-dependent functions to standardize here include

pandas/core/dtypes/missing.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import pandas._libs.missing as libmissing
1919
from pandas._libs.tslibs import (
2020
NaT,
21-
Period,
2221
iNaT,
2322
)
2423

@@ -749,10 +748,8 @@ def isna_all(arr: ArrayLike) -> bool:
749748
if dtype.kind == "f" and isinstance(dtype, np.dtype):
750749
checker = nan_checker
751750

752-
elif (
753-
(isinstance(dtype, np.dtype) and dtype.kind in ["m", "M"])
754-
or isinstance(dtype, DatetimeTZDtype)
755-
or dtype.type is Period
751+
elif (isinstance(dtype, np.dtype) and dtype.kind in ["m", "M"]) or isinstance(
752+
dtype, (DatetimeTZDtype, PeriodDtype)
756753
):
757754
# error: Incompatible types in assignment (expression has type
758755
# "Callable[[Any], Any]", variable has type "ufunc")

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7476,7 +7476,7 @@ def _cmp_method(self, other, op):
74767476
return self._construct_result(new_data)
74777477

74787478
def _arith_method(self, other, op):
7479-
if ops.should_reindex_frame_op(self, other, op, 1, 1, None, None):
7479+
if ops.should_reindex_frame_op(self, other, op, 1, None, None):
74807480
return ops.frame_arith_method_with_reindex(self, other, op)
74817481

74827482
axis: Literal[1] = 1 # only relevant for Series other case

pandas/core/groupby/groupby.py

-2
Original file line numberDiff line numberDiff line change
@@ -1638,8 +1638,6 @@ def array_func(values: ArrayLike) -> ArrayLike:
16381638

16391639
return result
16401640

1641-
# TypeError -> we may have an exception in trying to aggregate
1642-
# continue and exclude the block
16431641
new_mgr = data.grouped_reduce(array_func)
16441642

16451643
res = self._wrap_agged_manager(new_mgr)

pandas/core/indexes/multi.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,9 @@ def _set_levels(
841841

842842
self._reset_cache()
843843

844-
def set_levels(self, levels, *, level=None, verify_integrity: bool = True):
844+
def set_levels(
845+
self, levels, *, level=None, verify_integrity: bool = True
846+
) -> MultiIndex:
845847
"""
846848
Set new levels on MultiIndex. Defaults to returning new index.
847849
@@ -856,8 +858,7 @@ def set_levels(self, levels, *, level=None, verify_integrity: bool = True):
856858
857859
Returns
858860
-------
859-
new index (of same type and class...etc) or None
860-
The same type as the caller or None if ``inplace=True``.
861+
MultiIndex
861862
862863
Examples
863864
--------

pandas/core/internals/array_manager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -754,9 +754,9 @@ def fast_xs(self, loc: int) -> SingleArrayManager:
754754
result = dtype.construct_array_type()._from_sequence(values, dtype=dtype)
755755
# for datetime64/timedelta64, the np.ndarray constructor cannot handle pd.NaT
756756
elif is_datetime64_ns_dtype(dtype):
757-
result = DatetimeArray._from_sequence(values, dtype=dtype)._data
757+
result = DatetimeArray._from_sequence(values, dtype=dtype)._ndarray
758758
elif is_timedelta64_ns_dtype(dtype):
759-
result = TimedeltaArray._from_sequence(values, dtype=dtype)._data
759+
result = TimedeltaArray._from_sequence(values, dtype=dtype)._ndarray
760760
else:
761761
result = np.array(values, dtype=dtype)
762762
return SingleArrayManager([result], [self._axes[1]])

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2298,6 +2298,6 @@ def external_values(values: ArrayLike) -> ArrayLike:
22982298
# NB: for datetime64tz this is different from np.asarray(values), since
22992299
# that returns an object-dtype ndarray of Timestamps.
23002300
# Avoid raising in .astype in casting from dt64tz to dt64
2301-
return values._data
2301+
return values._ndarray
23022302
else:
23032303
return values

pandas/core/ops/__init__.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
from __future__ import annotations
77

88
import operator
9-
from typing import TYPE_CHECKING
9+
from typing import (
10+
TYPE_CHECKING,
11+
cast,
12+
)
1013

1114
import numpy as np
1215

@@ -333,7 +336,7 @@ def to_series(right):
333336

334337

335338
def should_reindex_frame_op(
336-
left: DataFrame, right, op, axis, default_axis, fill_value, level
339+
left: DataFrame, right, op, axis: int, fill_value, level
337340
) -> bool:
338341
"""
339342
Check if this is an operation between DataFrames that will need to reindex.
@@ -347,7 +350,7 @@ def should_reindex_frame_op(
347350
if not isinstance(right, ABCDataFrame):
348351
return False
349352

350-
if fill_value is None and level is None and axis is default_axis:
353+
if fill_value is None and level is None and axis == 1:
351354
# TODO: any other cases we should handle here?
352355

353356
# Intersection is always unique so we have to check the unique columns
@@ -435,26 +438,23 @@ def _maybe_align_series_as_frame(frame: DataFrame, series: Series, axis: AxisInt
435438

436439
def flex_arith_method_FRAME(op):
437440
op_name = op.__name__.strip("_")
438-
default_axis = "columns"
439441

440442
na_op = get_array_op(op)
441443
doc = make_flex_doc(op_name, "dataframe")
442444

443445
@Appender(doc)
444-
def f(self, other, axis=default_axis, level=None, fill_value=None):
446+
def f(self, other, axis: Axis = "columns", level=None, fill_value=None):
447+
axis = self._get_axis_number(axis) if axis is not None else 1
448+
axis = cast(int, axis)
445449

446-
if should_reindex_frame_op(
447-
self, other, op, axis, default_axis, fill_value, level
448-
):
450+
if should_reindex_frame_op(self, other, op, axis, fill_value, level):
449451
return frame_arith_method_with_reindex(self, other, op)
450452

451453
if isinstance(other, ABCSeries) and fill_value is not None:
452454
# TODO: We could allow this in cases where we end up going
453455
# through the DataFrame path
454456
raise NotImplementedError(f"fill_value {fill_value} not supported.")
455457

456-
axis = self._get_axis_number(axis) if axis is not None else 1
457-
458458
other = maybe_prepare_scalar_for_op(other, self.shape)
459459
self, other = align_method_FRAME(self, other, axis, flex=True, level=level)
460460

@@ -480,14 +480,13 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
480480

481481
def flex_comp_method_FRAME(op):
482482
op_name = op.__name__.strip("_")
483-
default_axis = "columns" # because we are "flex"
484483

485484
doc = _flex_comp_doc_FRAME.format(
486485
op_name=op_name, desc=_op_descriptions[op_name]["desc"]
487486
)
488487

489488
@Appender(doc)
490-
def f(self, other, axis=default_axis, level=None):
489+
def f(self, other, axis: Axis = "columns", level=None):
491490
axis = self._get_axis_number(axis) if axis is not None else 1
492491

493492
self, other = align_method_FRAME(self, other, axis, flex=True, level=level)

pandas/tests/apply/test_invalid_arg.py

-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ def test_transform_wont_agg_series(string_series, func):
355355
@pytest.mark.parametrize(
356356
"op_wrapper", [lambda x: x, lambda x: [x], lambda x: {"A": x}, lambda x: {"A": [x]}]
357357
)
358-
@pytest.mark.filterwarnings("ignore:.*Select only valid:FutureWarning")
359358
def test_transform_reducer_raises(all_reductions, frame_or_series, op_wrapper):
360359
# GH 35964
361360
op = op_wrapper(all_reductions)

pandas/tests/arithmetic/test_datetime64.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2437,7 +2437,7 @@ def test_dt64arr_addsub_object_dtype_2d():
24372437

24382438
assert isinstance(result, DatetimeArray)
24392439
assert result.freq is None
2440-
tm.assert_numpy_array_equal(result._data, expected._data)
2440+
tm.assert_numpy_array_equal(result._ndarray, expected._ndarray)
24412441

24422442
with tm.assert_produces_warning(PerformanceWarning):
24432443
# Case where we expect to get a TimedeltaArray back

pandas/tests/arrays/datetimes/test_constructors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ def test_freq_infer_raises(self):
122122
def test_copy(self):
123123
data = np.array([1, 2, 3], dtype="M8[ns]")
124124
arr = DatetimeArray(data, copy=False)
125-
assert arr._data is data
125+
assert arr._ndarray is data
126126

127127
arr = DatetimeArray(data, copy=True)
128-
assert arr._data is not data
128+
assert arr._ndarray is not data
129129

130130

131131
class TestSequenceToDT64NS:

pandas/tests/arrays/period/test_astype.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ def test_astype_copies():
4242
result = arr.astype(np.int64, copy=False)
4343

4444
# Add the `.base`, since we now use `.asi8` which returns a view.
45-
# We could maybe override it in PeriodArray to return ._data directly.
46-
assert result.base is arr._data
45+
# We could maybe override it in PeriodArray to return ._ndarray directly.
46+
assert result.base is arr._ndarray
4747

4848
result = arr.astype(np.int64, copy=True)
49-
assert result is not arr._data
50-
tm.assert_numpy_array_equal(result, arr._data.view("i8"))
49+
assert result is not arr._ndarray
50+
tm.assert_numpy_array_equal(result, arr._ndarray.view("i8"))
5151

5252

5353
def test_astype_categorical():

0 commit comments

Comments
 (0)