Skip to content

Commit 50e9404

Browse files
committed
CLN: assorted
1 parent 916cbe2 commit 50e9404

File tree

19 files changed

+78
-333
lines changed

19 files changed

+78
-333
lines changed

pandas/_libs/groupby.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ def group_skew(
950950
isna_entry = _treat_as_na(val, False)
951951

952952
if not isna_entry:
953-
# Based on RunningSats::Push from
953+
# Based on RunningStats::Push from
954954
# https://www.johndcook.com/blog/skewness_kurtosis/
955955
n1 = nobs[lab, j]
956956
n = n1 + 1

pandas/_libs/tslibs/timedeltas.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ cdef _timedelta_from_value_and_reso(cls, int64_t value, NPY_DATETIMEUNIT reso):
974974
"Only resolutions 's', 'ms', 'us', 'ns' are supported."
975975
)
976976

977-
td_base._value= value
977+
td_base._value = value
978978
td_base._is_populated = 0
979979
td_base._creso = reso
980980
return td_base

pandas/core/arrays/arrow/array.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -1131,13 +1131,7 @@ def take(
11311131
it's called by :meth:`Series.reindex`, or any other method
11321132
that causes realignment, with a `fill_value`.
11331133
"""
1134-
# TODO: Remove once we got rid of the (indices < 0) check
1135-
if not is_array_like(indices):
1136-
indices_array = np.asanyarray(indices)
1137-
else:
1138-
# error: Incompatible types in assignment (expression has type
1139-
# "Sequence[int]", variable has type "ndarray")
1140-
indices_array = indices # type: ignore[assignment]
1134+
indices_array = np.asanyarray(indices)
11411135

11421136
if len(self._pa_array) == 0 and (indices_array >= 0).any():
11431137
raise IndexError("cannot do a non-empty take")

pandas/core/arrays/sparse/array.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
maybe_box_datetimelike,
4343
)
4444
from pandas.core.dtypes.common import (
45-
is_array_like,
4645
is_bool_dtype,
4746
is_integer,
4847
is_list_like,
@@ -428,19 +427,16 @@ def __init__(
428427
# Union[int, Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]"
429428
data = np.array([], dtype=dtype) # type: ignore[arg-type]
430429

431-
if not is_array_like(data):
432-
try:
433-
# probably shared code in sanitize_series
434-
435-
data = sanitize_array(data, index=None)
436-
except ValueError:
437-
# NumPy may raise a ValueError on data like [1, []]
438-
# we retry with object dtype here.
439-
if dtype is None:
440-
dtype = np.dtype(object)
441-
data = np.atleast_1d(np.asarray(data, dtype=dtype))
442-
else:
443-
raise
430+
try:
431+
data = sanitize_array(data, index=None)
432+
except ValueError:
433+
# NumPy may raise a ValueError on data like [1, []]
434+
# we retry with object dtype here.
435+
if dtype is None:
436+
dtype = np.dtype(object)
437+
data = np.atleast_1d(np.asarray(data, dtype=dtype))
438+
else:
439+
raise
444440

445441
if copy:
446442
# TODO: avoid double copy when dtype forces cast.

pandas/core/dtypes/dtypes.py

-11
Original file line numberDiff line numberDiff line change
@@ -1665,17 +1665,6 @@ def _check_fill_value(self):
16651665
FutureWarning,
16661666
stacklevel=find_stack_level(),
16671667
)
1668-
elif isinstance(self.subtype, CategoricalDtype):
1669-
# TODO: is this even supported? It is reached in
1670-
# test_dtype_sparse_with_fill_value_not_present_in_data
1671-
if self.subtype.categories is None or val not in self.subtype.categories:
1672-
warnings.warn(
1673-
"Allowing arbitrary scalar fill_value in SparseDtype is "
1674-
"deprecated. In a future version, the fill_value must be "
1675-
"a valid value for the SparseDtype.subtype.",
1676-
FutureWarning,
1677-
stacklevel=find_stack_level(),
1678-
)
16791668
else:
16801669
dummy = np.empty(0, dtype=self.subtype)
16811670
dummy = ensure_wrapped_if_datetimelike(dummy)

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,9 @@ def __init__(
673673
manager = get_option("mode.data_manager")
674674

675675
# GH47215
676-
if index is not None and isinstance(index, set):
676+
if isinstance(index, set):
677677
raise ValueError("index cannot be a set")
678-
if columns is not None and isinstance(columns, set):
678+
if isinstance(columns, set):
679679
raise ValueError("columns cannot be a set")
680680

681681
if copy is None:

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
AlignJoin,
4848
AnyArrayLike,
4949
ArrayLike,
50+
Axes,
5051
Axis,
5152
AxisInt,
5253
CompressionOptions,
@@ -271,7 +272,7 @@ def __init__(self, data: Manager) -> None:
271272
def _init_mgr(
272273
cls,
273274
mgr: Manager,
274-
axes,
275+
axes: dict[Literal["index", "columns"], Axes | None],
275276
dtype: DtypeObj | None = None,
276277
copy: bool_t = False,
277278
) -> Manager:
@@ -3995,7 +3996,6 @@ class max_speed
39953996
):
39963997
return self.copy(deep=None)
39973998
elif self.ndim == 1:
3998-
# TODO: be consistent here for DataFrame vs Series
39993999
raise TypeError(
40004000
f"{type(self).__name__}.take requires a sequence of integers, "
40014001
"not slice."

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4191,7 +4191,7 @@ def _convert_slice_indexer(self, key: slice, kind: Literal["loc", "getitem"]):
41914191

41924192
# TODO(GH#50617): once Series.__[gs]etitem__ is removed we should be able
41934193
# to simplify this.
4194-
if isinstance(self.dtype, np.dtype) and self.dtype.kind == "f":
4194+
if lib.is_np_dtype(self.dtype, "f"):
41954195
# We always treat __getitem__ slicing as label-based
41964196
# translate to locations
41974197
return self.slice_indexer(start, stop, step)

pandas/core/internals/blocks.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def make_block_same_class(
233233
values,
234234
placement: BlockPlacement | None = None,
235235
refs: BlockValuesRefs | None = None,
236-
) -> Block:
236+
) -> Self:
237237
"""Wrap given values in a block of same type as self."""
238238
# Pre-2.0 we called ensure_wrapped_if_datetimelike because fastparquet
239239
# relied on it, as of 2.0 the caller is responsible for this.
@@ -503,6 +503,7 @@ def convert(
503503
# ---------------------------------------------------------------------
504504
# Array-Like Methods
505505

506+
@final
506507
@cache_readonly
507508
def dtype(self) -> DtypeObj:
508509
return self.values.dtype
@@ -559,7 +560,7 @@ def to_native_types(self, na_rep: str = "nan", quoting=None, **kwargs) -> Block:
559560
return self.make_block(result)
560561

561562
@final
562-
def copy(self, deep: bool = True) -> Block:
563+
def copy(self, deep: bool = True) -> Self:
563564
"""copy constructor"""
564565
values = self.values
565566
refs: BlockValuesRefs | None
@@ -1499,7 +1500,8 @@ def quantile(
14991500
result = ensure_block_shape(result, ndim=2)
15001501
return new_block_2d(result, placement=self._mgr_locs)
15011502

1502-
def round(self, decimals: int, using_cow: bool = False) -> Block:
1503+
@final
1504+
def round(self, decimals: int, using_cow: bool = False) -> Self:
15031505
"""
15041506
Rounds the values.
15051507
If the block is not of an integer or float dtype, nothing happens.
@@ -1765,9 +1767,7 @@ def putmask(self, mask, new, using_cow: bool = False) -> list[Block]:
17651767

17661768
if using_cow and self.refs.has_reference():
17671769
values = values.copy()
1768-
self = self.make_block_same_class( # type: ignore[assignment]
1769-
values.T if values.ndim == 2 else values
1770-
)
1770+
self = self.make_block_same_class(values.T if values.ndim == 2 else values)
17711771

17721772
try:
17731773
# Caller is responsible for ensuring matching lengths

pandas/core/internals/managers.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -939,8 +939,7 @@ def take(
939939
-------
940940
BlockManager
941941
"""
942-
assert isinstance(indexer, np.ndarray), type(indexer)
943-
assert indexer.dtype == np.intp, indexer.dtype
942+
# Caller is responsible for ensuring indexer annotation is accurate
944943

945944
n = self.shape[axis]
946945
indexer = maybe_convert_indices(indexer, n, verify=verify)

pandas/core/missing.py

-2
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,6 @@ def interpolate_array_2d(
311311
limit_direction: str = "forward",
312312
limit_area: str | None = None,
313313
fill_value: Any | None = None,
314-
coerce: bool = False,
315-
downcast: str | None = None,
316314
**kwargs,
317315
) -> None:
318316
"""

pandas/core/reshape/pivot.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ def pivot(
511511
data: DataFrame,
512512
*,
513513
columns: IndexLabel,
514-
index: IndexLabel | lib.NoDefault = lib.NoDefault,
515-
values: IndexLabel | lib.NoDefault = lib.NoDefault,
514+
index: IndexLabel | lib.NoDefault = lib.no_default,
515+
values: IndexLabel | lib.NoDefault = lib.no_default,
516516
) -> DataFrame:
517517
columns_listlike = com.convert_to_list_like(columns)
518518

@@ -522,24 +522,24 @@ def pivot(
522522
data = data.copy(deep=False)
523523
data.index = data.index.copy()
524524
data.index.names = [
525-
name if name is not None else lib.NoDefault for name in data.index.names
525+
name if name is not None else lib.no_default for name in data.index.names
526526
]
527527

528528
indexed: DataFrame | Series
529-
if values is lib.NoDefault:
530-
if index is not lib.NoDefault:
529+
if values is lib.no_default:
530+
if index is not lib.no_default:
531531
cols = com.convert_to_list_like(index)
532532
else:
533533
cols = []
534534

535-
append = index is lib.NoDefault
535+
append = index is lib.no_default
536536
# error: Unsupported operand types for + ("List[Any]" and "ExtensionArray")
537537
# error: Unsupported left operand type for + ("ExtensionArray")
538538
indexed = data.set_index(
539539
cols + columns_listlike, append=append # type: ignore[operator]
540540
)
541541
else:
542-
if index is lib.NoDefault:
542+
if index is lib.no_default:
543543
if isinstance(data.index, MultiIndex):
544544
# GH 23955
545545
index_list = [
@@ -569,7 +569,7 @@ def pivot(
569569
# "Hashable"
570570
result = indexed.unstack(columns_listlike) # type: ignore[arg-type]
571571
result.index.names = [
572-
name if name is not lib.NoDefault else None for name in result.index.names
572+
name if name is not lib.no_default else None for name in result.index.names
573573
]
574574

575575
return result

pandas/core/strings/accessor.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
DtypeObj,
2121
F,
2222
Scalar,
23+
npt,
2324
)
2425
from pandas.util._decorators import Appender
2526
from pandas.util._exceptions import find_stack_level
@@ -3366,7 +3367,7 @@ def casefold(self):
33663367
)
33673368

33683369

3369-
def cat_safe(list_of_columns: list, sep: str):
3370+
def cat_safe(list_of_columns: list[npt.NDArray[np.object_]], sep: str):
33703371
"""
33713372
Auxiliary function for :meth:`str.cat`.
33723373

pandas/tests/io/json/test_pandas.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -433,13 +433,13 @@ def test_frame_empty(self):
433433
assert not df._is_mixed_type
434434

435435
data = StringIO(df.to_json())
436-
tm.assert_frame_equal(
437-
read_json(data, dtype=dict(df.dtypes)),
438-
df,
439-
check_index_type=False,
440-
)
436+
result = read_json(data, dtype=dict(df.dtypes))
437+
tm.assert_frame_equal(result, df, check_index_type=False)
438+
439+
def test_frame_empty_to_json(self):
441440
# GH 7445
442-
result = DataFrame({"test": []}, index=[]).to_json(orient="columns")
441+
df = DataFrame({"test": []}, index=[])
442+
result = df.to_json(orient="columns")
443443
expected = '{"test":{}}'
444444
assert result == expected
445445

pandas/tests/resample/test_base.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ def test_asfreq_fill_value(series, create_index):
8080

8181
@all_ts
8282
def test_resample_interpolate(frame):
83-
# # 12925
83+
# GH#12925
8484
df = frame
85-
tm.assert_frame_equal(
86-
df.resample("1T").asfreq().interpolate(), df.resample("1T").interpolate()
87-
)
85+
result = df.resample("1T").asfreq().interpolate()
86+
expected = df.resample("1T").interpolate()
87+
tm.assert_frame_equal(result, expected)
8888

8989

9090
def test_raises_on_non_datetimelike_index():
@@ -95,7 +95,7 @@ def test_raises_on_non_datetimelike_index():
9595
"but got an instance of 'RangeIndex'"
9696
)
9797
with pytest.raises(TypeError, match=msg):
98-
xp.resample("A").mean()
98+
xp.resample("A")
9999

100100

101101
@all_ts
@@ -132,13 +132,17 @@ def test_resample_empty_series(freq, empty_series_dti, resample_method, request)
132132

133133

134134
@all_ts
135-
@pytest.mark.parametrize("freq", ["M", "D", "H"])
136-
def test_resample_nat_index_series(request, freq, series, resample_method):
135+
@pytest.mark.parametrize(
136+
"freq",
137+
[
138+
pytest.param("M", marks=pytest.mark.xfail(reason="Don't know why this fails")),
139+
"D",
140+
"H",
141+
],
142+
)
143+
def test_resample_nat_index_series(freq, series, resample_method):
137144
# GH39227
138145

139-
if freq == "M":
140-
request.node.add_marker(pytest.mark.xfail(reason="Don't know why this fails"))
141-
142146
ser = series.copy()
143147
ser.index = PeriodIndex([NaT] * len(ser), freq=freq)
144148
rs = ser.resample(freq)

pandas/tests/series/indexing/test_take.py

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
import pandas._testing as tm
66

77

8+
def test_take_validate_axis():
9+
# GH#51022
10+
ser = Series([-1, 5, 6, 2, 4])
11+
12+
msg = "No axis named foo for object type Series"
13+
with pytest.raises(ValueError, match=msg):
14+
ser.take([1, 2], axis="foo")
15+
16+
817
def test_take():
918
ser = Series([-1, 5, 6, 2, 4])
1019

pandas/tests/series/methods/test_reindex.py

+6
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ def test_reindex_pad():
132132
expected = Series([0, 0, 2, 2, 4, 4, 6, 6, 8, 8])
133133
tm.assert_series_equal(reindexed, expected)
134134

135+
136+
def test_reindex_pad2():
135137
# GH4604
136138
s = Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"])
137139
new_index = ["a", "g", "c", "f"]
@@ -148,13 +150,17 @@ def test_reindex_pad():
148150
result = s.reindex(new_index, method="ffill")
149151
tm.assert_series_equal(result, expected)
150152

153+
154+
def test_reindex_inference():
151155
# inference of new dtype
152156
s = Series([True, False, False, True], index=list("abcd"))
153157
new_index = "agc"
154158
result = s.reindex(list(new_index)).ffill()
155159
expected = Series([True, True, False], index=list(new_index))
156160
tm.assert_series_equal(result, expected)
157161

162+
163+
def test_reindex_downcasting():
158164
# GH4618 shifted series downcasting
159165
s = Series(False, index=range(0, 5))
160166
result = s.shift(1).bfill()

0 commit comments

Comments
 (0)