Skip to content

Commit 320e72e

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent ca24f37 commit 320e72e

Some content is hidden

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

80 files changed

+246
-242
lines changed

asv_bench/benchmarks/frame_methods.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ def setup(self):
517517
self.df = DataFrame(np.random.randn(1000, 100))
518518

519519
self.s = Series(np.arange(1028.0))
520-
self.df2 = DataFrame({i: self.s for i in range(1028)})
520+
self.df2 = DataFrame(dict.fromkeys(range(1028), self.s))
521521
self.df3 = DataFrame(np.random.randn(1000, 3), columns=list("ABC"))
522522

523523
def time_apply_user_func(self):

pandas/_config/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ def inner(key: str, *args, **kwds):
775775
pkey = f"{prefix}.{key}"
776776
return func(pkey, *args, **kwds)
777777

778-
return cast(F, inner)
778+
return cast("F", inner)
779779

780780
_register_option = register_option
781781
_get_option = get_option

pandas/_config/localization.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ def get_locales(
156156
out_locales = []
157157
for x in split_raw_locales:
158158
try:
159-
out_locales.append(str(x, encoding=cast(str, options.display.encoding)))
159+
out_locales.append(
160+
str(x, encoding=cast("str", options.display.encoding))
161+
)
160162
except UnicodeError:
161163
# 'locale -a' is used to populated 'raw_locales' and on
162164
# Redhat 7 Linux (and maybe others) prints locale names

pandas/_testing/_warnings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class for all warnings. To raise multiple types of exceptions,
121121
)
122122
else:
123123
expected_warning = cast(
124-
Union[type[Warning], tuple[type[Warning], ...]],
124+
"Union[type[Warning], tuple[type[Warning], ...]]",
125125
expected_warning,
126126
)
127127
match = (
@@ -241,7 +241,7 @@ def _is_unexpected_warning(
241241
"""Check if the actual warning issued is unexpected."""
242242
if actual_warning and not expected_warning:
243243
return True
244-
expected_warning = cast(type[Warning], expected_warning)
244+
expected_warning = cast("type[Warning]", expected_warning)
245245
return bool(not issubclass(actual_warning.category, expected_warning))
246246

247247

pandas/_testing/asserters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def _check_types(left, right, obj: str = "Index") -> None:
283283

284284
# MultiIndex special comparison for little-friendly error messages
285285
if isinstance(left, MultiIndex):
286-
right = cast(MultiIndex, right)
286+
right = cast("MultiIndex", right)
287287

288288
for level in range(left.nlevels):
289289
lobj = f"{obj} level [{level}]"
@@ -776,11 +776,11 @@ def assert_extension_array_equal(
776776
# GH 52449
777777
if not check_dtype and left.dtype.kind in "mM":
778778
if not isinstance(left.dtype, np.dtype):
779-
l_unit = cast(DatetimeTZDtype, left.dtype).unit
779+
l_unit = cast("DatetimeTZDtype", left.dtype).unit
780780
else:
781781
l_unit = np.datetime_data(left.dtype)[0]
782782
if not isinstance(right.dtype, np.dtype):
783-
r_unit = cast(DatetimeTZDtype, right.dtype).unit
783+
r_unit = cast("DatetimeTZDtype", right.dtype).unit
784784
else:
785785
r_unit = np.datetime_data(right.dtype)[0]
786786
if (

pandas/compat/numpy/function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def validate_argsort_with_ascending(ascending: bool | int | None, args, kwargs)
169169
ascending = True
170170

171171
validate_argsort_kind(args, kwargs, max_fname_arg_count=3)
172-
ascending = cast(bool, ascending)
172+
ascending = cast("bool", ascending)
173173
return ascending
174174

175175

pandas/core/algorithms.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ def _ensure_data(values: ArrayLike) -> np.ndarray:
172172
return np.asarray(values)
173173

174174
elif is_complex_dtype(values.dtype):
175-
return cast(np.ndarray, values)
175+
return cast("np.ndarray", values)
176176

177177
# datetimelike
178178
elif needs_i8_conversion(values.dtype):
179179
npvalues = values.view("i8")
180-
npvalues = cast(np.ndarray, npvalues)
180+
npvalues = cast("np.ndarray", npvalues)
181181
return npvalues
182182

183183
# we have failed, return object
@@ -1289,9 +1289,9 @@ def searchsorted(
12891289

12901290
if is_integer(value):
12911291
# We know that value is int
1292-
value = cast(int, dtype.type(value))
1292+
value = cast("int", dtype.type(value))
12931293
else:
1294-
value = pd_array(cast(ArrayLike, value), dtype=dtype)
1294+
value = pd_array(cast("ArrayLike", value), dtype=dtype)
12951295
else:
12961296
# E.g. if `arr` is an array with dtype='datetime64[ns]'
12971297
# and `value` is a pd.Timestamp, we may need to convert value

pandas/core/apply.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,19 +322,19 @@ def transform(self) -> DataFrame | Series:
322322
return obj.T.transform(func, 0, *args, **kwargs).T
323323

324324
if is_list_like(func) and not is_dict_like(func):
325-
func = cast(list[AggFuncTypeBase], func)
325+
func = cast("list[AggFuncTypeBase]", func)
326326
# Convert func equivalent dict
327327
if is_series:
328328
func = {com.get_callable_name(v) or v: v for v in func}
329329
else:
330-
func = {col: func for col in obj}
330+
func = dict.fromkeys(obj, func)
331331

332332
if is_dict_like(func):
333-
func = cast(AggFuncTypeDict, func)
333+
func = cast("AggFuncTypeDict", func)
334334
return self.transform_dict_like(func)
335335

336336
# func is either str or callable
337-
func = cast(AggFuncTypeBase, func)
337+
func = cast("AggFuncTypeBase", func)
338338
try:
339339
result = self.transform_str_or_callable(func)
340340
except TypeError:
@@ -434,7 +434,7 @@ def compute_list_like(
434434
Data for result. When aggregating with a Series, this can contain any
435435
Python objects.
436436
"""
437-
func = cast(list[AggFuncTypeBase], self.func)
437+
func = cast("list[AggFuncTypeBase]", self.func)
438438
obj = self.obj
439439

440440
results = []
@@ -541,7 +541,7 @@ def compute_dict_like(
541541

542542
obj = self.obj
543543
is_groupby = isinstance(obj, (DataFrameGroupBy, SeriesGroupBy))
544-
func = cast(AggFuncTypeDict, self.func)
544+
func = cast("AggFuncTypeDict", self.func)
545545
func = self.normalize_dictlike_arg(op_name, selected_obj, func)
546546

547547
is_non_unique_col = (
@@ -666,7 +666,7 @@ def apply_str(self) -> DataFrame | Series:
666666
result: Series or DataFrame
667667
"""
668668
# Caller is responsible for checking isinstance(self.f, str)
669-
func = cast(str, self.func)
669+
func = cast("str", self.func)
670670

671671
obj = self.obj
672672

@@ -1262,7 +1262,7 @@ def numba_func(values, col_names, df_index, *args):
12621262
return numba_func
12631263

12641264
def apply_with_numba(self) -> dict[int, Any]:
1265-
func = cast(Callable, self.func)
1265+
func = cast("Callable", self.func)
12661266
args, kwargs = prepare_function_arguments(
12671267
func, self.args, self.kwargs, num_required_args=1
12681268
)
@@ -1404,7 +1404,7 @@ def numba_func(values, col_names_index, index, *args):
14041404
return numba_func
14051405

14061406
def apply_with_numba(self) -> dict[int, Any]:
1407-
func = cast(Callable, self.func)
1407+
func = cast("Callable", self.func)
14081408
args, kwargs = prepare_function_arguments(
14091409
func, self.args, self.kwargs, num_required_args=1
14101410
)
@@ -1551,7 +1551,7 @@ def apply_compat(self):
15511551

15521552
def apply_standard(self) -> DataFrame | Series:
15531553
# caller is responsible for ensuring that f is Callable
1554-
func = cast(Callable, self.func)
1554+
func = cast("Callable", self.func)
15551555
obj = self.obj
15561556

15571557
if isinstance(func, np.ufunc):

pandas/core/arrays/_mixins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def method(self, *args, **kwargs):
8585
order = "F" if flags.f_contiguous else "C"
8686
return result.reshape(self.shape, order=order)
8787

88-
return cast(F, method)
88+
return cast("F", method)
8989

9090

9191
# error: Definition of "delete/ravel/T/repeat/copy" in base class "NDArrayBacked"

pandas/core/arrays/arrow/accessors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def get_name(
445445
while level_name_or_index:
446446
# we need the cast, otherwise mypy complains about
447447
# getting ints, bytes, or str here, which isn't possible.
448-
level_name_or_index = cast(list, level_name_or_index)
448+
level_name_or_index = cast("list", level_name_or_index)
449449
name_or_index = level_name_or_index.pop()
450450
name = get_name(name_or_index, selected)
451451
selected = selected.type.field(selected.type.get_field_index(name))

pandas/core/arrays/arrow/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ def factorize(
12311231
uniques = type(self)(combined.dictionary)
12321232

12331233
if pa_version_under11p0 and pa.types.is_duration(pa_type):
1234-
uniques = cast(ArrowExtensionArray, uniques.astype(self.dtype))
1234+
uniques = cast("ArrowExtensionArray", uniques.astype(self.dtype))
12351235
return indices, uniques
12361236

12371237
def reshape(self, *args, **kwargs):
@@ -1991,7 +1991,7 @@ def __setitem__(self, key, value) -> None:
19911991

19921992
elif is_integer(key):
19931993
# fast path
1994-
key = cast(int, key)
1994+
key = cast("int", key)
19951995
n = len(self)
19961996
if key < 0:
19971997
key += n

pandas/core/arrays/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ def equals(self, other: object) -> bool:
15091509
"""
15101510
if type(self) != type(other):
15111511
return False
1512-
other = cast(ExtensionArray, other)
1512+
other = cast("ExtensionArray", other)
15131513
if self.dtype != other.dtype:
15141514
return False
15151515
elif len(self) != len(other):

pandas/core/arrays/categorical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2478,7 +2478,7 @@ def _mode(self, dropna: bool = True) -> Categorical:
24782478
mask = self.isna()
24792479

24802480
res_codes, _ = algorithms.mode(codes, mask=mask)
2481-
res_codes = cast(np.ndarray, res_codes)
2481+
res_codes = cast("np.ndarray", res_codes)
24822482
assert res_codes.dtype == codes.dtype
24832483
res = self._from_backing_data(res_codes)
24842484
return res

pandas/core/arrays/datetimelike.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def new_meth(self, *args, **kwargs):
191191
res_i8 = result.view("i8")
192192
return self._from_backing_data(res_i8)
193193

194-
return cast(F, new_meth)
194+
return cast("F", new_meth)
195195

196196

197197
# error: Definition of "_concat_same_type" in base class "NDArrayBacked" is
@@ -391,7 +391,7 @@ def __getitem__(self, key: PositionalIndexer2D) -> Self | DTScalarOrNaT:
391391
return result
392392
else:
393393
# At this point we know the result is an array.
394-
result = cast(Self, result)
394+
result = cast("Self", result)
395395
result._freq = self._get_getitem_freq(key)
396396
return result
397397

@@ -990,7 +990,7 @@ def _cmp_method(self, other, op):
990990
return result
991991

992992
if not isinstance(self.dtype, PeriodDtype):
993-
self = cast(TimelikeOps, self)
993+
self = cast("TimelikeOps", self)
994994
if self._creso != other._creso:
995995
if not isinstance(other, type(self)):
996996
# i.e. Timedelta/Timestamp, cast to ndarray and let
@@ -1637,7 +1637,7 @@ def _mode(self, dropna: bool = True):
16371637

16381638
i8modes, _ = algorithms.mode(self.view("i8"), mask=mask)
16391639
npmodes = i8modes.view(self._ndarray.dtype)
1640-
npmodes = cast(np.ndarray, npmodes)
1640+
npmodes = cast("np.ndarray", npmodes)
16411641
return self._from_backing_data(npmodes)
16421642

16431643
# ------------------------------------------------------------------
@@ -2198,7 +2198,7 @@ def _round(self, freq, mode, ambiguous, nonexistent):
21982198
)
21992199

22002200
values = self.view("i8")
2201-
values = cast(np.ndarray, values)
2201+
values = cast("np.ndarray", values)
22022202
nanos = get_unit_for_round(freq, self._creso)
22032203
if nanos == 0:
22042204
# GH 52761

pandas/core/arrays/datetimes.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ def _generate_range(
508508
# Nanosecond-granularity timestamps aren't always correctly
509509
# representable with doubles, so we limit the range that we
510510
# pass to np.linspace as much as possible
511-
periods = cast(int, periods)
511+
periods = cast("int", periods)
512512
i8values = (
513513
np.linspace(0, end._value - start._value, periods, dtype="int64")
514514
+ start._value
@@ -2430,7 +2430,7 @@ def _sequence_to_dt64(
24302430
if data_dtype == object or is_string_dtype(data_dtype):
24312431
# TODO: We do not have tests specific to string-dtypes,
24322432
# also complex or categorical or other extension
2433-
data = cast(np.ndarray, data)
2433+
data = cast("np.ndarray", data)
24342434
copy = False
24352435
if lib.infer_dtype(data, skipna=False) == "integer":
24362436
# Much more performant than going through array_to_datetime
@@ -2475,7 +2475,7 @@ def _sequence_to_dt64(
24752475
# so we need to handle these types.
24762476
if isinstance(data_dtype, DatetimeTZDtype):
24772477
# DatetimeArray -> ndarray
2478-
data = cast(DatetimeArray, data)
2478+
data = cast("DatetimeArray", data)
24792479
tz = _maybe_infer_tz(tz, data.tz)
24802480
result = data._ndarray
24812481

@@ -2484,7 +2484,7 @@ def _sequence_to_dt64(
24842484
if isinstance(data, DatetimeArray):
24852485
data = data._ndarray
24862486

2487-
data = cast(np.ndarray, data)
2487+
data = cast("np.ndarray", data)
24882488
result, copy = _construct_from_dt64_naive(
24892489
data, tz=tz, copy=copy, ambiguous=ambiguous
24902490
)
@@ -2495,7 +2495,7 @@ def _sequence_to_dt64(
24952495
if data.dtype != INT64_DTYPE:
24962496
data = data.astype(np.int64, copy=False)
24972497
copy = False
2498-
data = cast(np.ndarray, data)
2498+
data = cast("np.ndarray", data)
24992499
result = data.view(out_dtype)
25002500

25012501
if copy:
@@ -2760,7 +2760,7 @@ def _validate_dt64_dtype(dtype):
27602760
# Without this, things like adding an array of timedeltas and
27612761
# a tz-aware Timestamp (with a tz specific to its datetime) will
27622762
# be incorrect(ish?) for the array as a whole
2763-
dtype = cast(DatetimeTZDtype, dtype)
2763+
dtype = cast("DatetimeTZDtype", dtype)
27642764
dtype = DatetimeTZDtype(
27652765
unit=dtype.unit, tz=timezones.tz_standardize(dtype.tz)
27662766
)
@@ -2985,8 +2985,8 @@ def _generate_range(
29852985
# argument type "None"
29862986
start = end - (periods - 1) * offset # type: ignore[operator]
29872987

2988-
start = cast(Timestamp, start)
2989-
end = cast(Timestamp, end)
2988+
start = cast("Timestamp", start)
2989+
end = cast("Timestamp", end)
29902990

29912991
cur = start
29922992
if offset.n >= 0:

pandas/core/arrays/masked.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def _from_sequence(cls, scalars, *, dtype=None, copy: bool = False) -> Self:
148148
@classmethod
149149
@doc(ExtensionArray._empty)
150150
def _empty(cls, shape: Shape, dtype: ExtensionDtype) -> Self:
151-
dtype = cast(BaseMaskedDtype, dtype)
151+
dtype = cast("BaseMaskedDtype", dtype)
152152
values: np.ndarray = np.empty(shape, dtype=dtype.type)
153153
values.fill(dtype._internal_fill_value)
154154
mask = np.ones(shape, dtype=bool)

pandas/core/arrays/period.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def __init__(self, values, dtype: Dtype | None = None, copy: bool = False) -> No
245245
values = np.array(values, dtype="int64", copy=copy)
246246
if dtype is None:
247247
raise ValueError("dtype is not specified and cannot be inferred")
248-
dtype = cast(PeriodDtype, dtype)
248+
dtype = cast("PeriodDtype", dtype)
249249
NDArrayBacked.__init__(self, values, dtype)
250250

251251
# error: Signature of "_simple_new" incompatible with supertype "NDArrayBacked"

pandas/core/arrays/sparse/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ def __getitem__(
10311031

10321032
if com.is_bool_indexer(key):
10331033
# mypy doesn't know we have an array here
1034-
key = cast(np.ndarray, key)
1034+
key = cast("np.ndarray", key)
10351035
return self.take(np.arange(len(key), dtype=np.int32)[key])
10361036
elif hasattr(key, "__len__"):
10371037
return self.take(key)
@@ -1302,7 +1302,7 @@ def astype(self, dtype: AstypeArg | None = None, copy: bool = True):
13021302

13031303
dtype = self.dtype.update_dtype(dtype)
13041304
subtype = pandas_dtype(dtype._subtype_with_str)
1305-
subtype = cast(np.dtype, subtype) # ensured by update_dtype
1305+
subtype = cast("np.dtype", subtype) # ensured by update_dtype
13061306
values = ensure_wrapped_if_datetimelike(self.sp_values)
13071307
sp_values = astype_array(values, subtype, copy=copy)
13081308
sp_values = np.asarray(sp_values)

0 commit comments

Comments
 (0)