Skip to content

STYLE enable ruff TCH on pandas/core/arrays #51754

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

Closed
wants to merge 8 commits into from
Closed
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 LICENSES/SCIPY_LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.

175 changes: 84 additions & 91 deletions pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
from pandas.compat import is_numpy_dev as _is_numpy_dev # pyright: ignore # noqa:F401

try:
from pandas._libs import hashtable as _hashtable, lib as _lib, tslib as _tslib
from pandas._libs import (
hashtable as _hashtable,
lib as _lib,
tslib as _tslib,
)
except ImportError as _err: # pragma: no cover
_module = _err.name
raise ImportError(
Expand All @@ -34,149 +38,138 @@
del _tslib, _lib, _hashtable

from pandas._config import (
get_option,
set_option,
reset_option,
describe_option,
get_option,
option_context,
options,
reset_option,
set_option,
)

# let init-time option registration happen
import pandas.core.config_init # pyright: ignore # noqa:F401
from pandas.util._print_versions import show_versions
from pandas.util._tester import test

from pandas.core.api import (
# dtype
from pandas import (
api,
arrays,
errors,
io,
plotting,
testing,
tseries,
)

# use the closest tagged version if possible
from pandas._version import get_versions
from pandas.core.api import ( # dtype; missing; indexes; tseries; conversion; misc
NA,
ArrowDtype,
BooleanDtype,
Categorical,
CategoricalDtype,
CategoricalIndex,
DataFrame,
DateOffset,
DatetimeIndex,
DatetimeTZDtype,
Flags,
Float32Dtype,
Float64Dtype,
Grouper,
Index,
IndexSlice,
Int8Dtype,
Int16Dtype,
Int32Dtype,
Int64Dtype,
Interval,
IntervalDtype,
IntervalIndex,
MultiIndex,
NamedAgg,
NaT,
Period,
PeriodDtype,
PeriodIndex,
RangeIndex,
Series,
StringDtype,
Timedelta,
TimedeltaIndex,
Timestamp,
UInt8Dtype,
UInt16Dtype,
UInt32Dtype,
UInt64Dtype,
Float32Dtype,
Float64Dtype,
CategoricalDtype,
PeriodDtype,
IntervalDtype,
DatetimeTZDtype,
StringDtype,
BooleanDtype,
# missing
NA,
array,
bdate_range,
date_range,
factorize,
interval_range,
isna,
isnull,
notna,
notnull,
# indexes
Index,
CategoricalIndex,
RangeIndex,
MultiIndex,
IntervalIndex,
TimedeltaIndex,
DatetimeIndex,
PeriodIndex,
IndexSlice,
# tseries
NaT,
Period,
period_range,
Timedelta,
set_eng_float_format,
timedelta_range,
Timestamp,
date_range,
bdate_range,
Interval,
interval_range,
DateOffset,
# conversion
to_numeric,
to_datetime,
to_numeric,
to_timedelta,
# misc
Flags,
Grouper,
factorize,
unique,
value_counts,
NamedAgg,
array,
Categorical,
set_eng_float_format,
Series,
DataFrame,
)

from pandas.core.arrays.sparse import SparseDtype

from pandas.tseries.api import infer_freq
from pandas.tseries import offsets

from pandas.core.computation.api import eval

# let init-time option registration happen
import pandas.core.config_init # pyright: ignore # noqa:F401
from pandas.core.reshape.api import (
concat,
crosstab,
cut,
from_dummies,
get_dummies,
lreshape,
melt,
wide_to_long,
merge,
merge_asof,
merge_ordered,
crosstab,
pivot,
pivot_table,
get_dummies,
from_dummies,
cut,
qcut,
wide_to_long,
)

from pandas import api, arrays, errors, io, plotting, tseries
from pandas import testing
from pandas.util._print_versions import show_versions

from pandas.io.api import (
# excel
from pandas.io.api import ( # excel; parsers; pickle; pytables; sql; misc
ExcelFile,
ExcelWriter,
read_excel,
# parsers
read_csv,
read_fwf,
read_table,
# pickle
read_pickle,
to_pickle,
# pytables
HDFStore,
read_hdf,
# sql
read_sql,
read_sql_query,
read_sql_table,
# misc
read_clipboard,
read_parquet,
read_orc,
read_csv,
read_excel,
read_feather,
read_fwf,
read_gbq,
read_hdf,
read_html,
read_xml,
read_json,
read_stata,
read_orc,
read_parquet,
read_pickle,
read_sas,
read_spss,
read_sql,
read_sql_query,
read_sql_table,
read_stata,
read_table,
read_xml,
to_pickle,
)

from pandas.io.json._normalize import json_normalize

from pandas.util._tester import test

# use the closest tagged version if possible
from pandas._version import get_versions
from pandas.tseries import offsets
from pandas.tseries.api import infer_freq

v = get_versions()
__version__ = v.get("closest-tag", v["version"])
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/algos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ cpdef ndarray[int64_t, ndim=1] unique_deltas(const int64_t[:] arr):

@cython.wraparound(False)
@cython.boundscheck(False)
def is_lexsorted(list_of_arrays: list) -> bool:
def is_lexsorted(list_of_arrays: list) -> bint:
cdef:
Py_ssize_t i
Py_ssize_t n, nlevels
Expand Down Expand Up @@ -752,7 +752,7 @@ def is_monotonic(ndarray[numeric_object_t, ndim=1] arr, bint timelike):
tuple
is_monotonic_inc : bool
is_monotonic_dec : bool
is_strict_monotonic : bool
is_unique : bool
"""
cdef:
Py_ssize_t i, n
Expand Down
10 changes: 5 additions & 5 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -256,24 +256,24 @@ cdef class IndexEngine:

cdef _do_monotonic_check(self):
cdef:
bint is_strict_monotonic
bint is_unique
if self.mask is not None and np.any(self.mask):
self.monotonic_inc = 0
self.monotonic_dec = 0
else:
try:
values = self.values
self.monotonic_inc, self.monotonic_dec, is_strict_monotonic = \
self.monotonic_inc, self.monotonic_dec, is_unique = \
self._call_monotonic(values)
except TypeError:
self.monotonic_inc = 0
self.monotonic_dec = 0
is_strict_monotonic = 0
is_unique = 0

self.need_monotonic_check = 0

# we can only be sure of uniqueness if is_strict_monotonic=1
if is_strict_monotonic:
# we can only be sure of uniqueness if is_unique=1
if is_unique:
self.unique = 1
self.need_unique_check = 0

Expand Down
22 changes: 6 additions & 16 deletions pandas/_libs/join.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -834,10 +834,10 @@ def asof_join_forward_on_X_by_Y(numeric_t[:] left_values,
return left_indexer, right_indexer


def asof_join_nearest_on_X_by_Y(ndarray[numeric_t] left_values,
ndarray[numeric_t] right_values,
ndarray[by_t] left_by_values,
ndarray[by_t] right_by_values,
def asof_join_nearest_on_X_by_Y(numeric_t[:] left_values,
numeric_t[:] right_values,
by_t[:] left_by_values,
by_t[:] right_by_values,
bint allow_exact_matches=True,
tolerance=None,
bint use_hashtable=True):
Expand All @@ -850,17 +850,7 @@ def asof_join_nearest_on_X_by_Y(ndarray[numeric_t] left_values,
numeric_t bdiff, fdiff

# search both forward and backward
# TODO(cython3):
# Bug in beta1 preventing Cython from choosing
# right specialization when one fused memview is None
# Doesn't matter what type we choose
# (nothing happens anyways since it is None)
# GH 51640
if left_by_values is not None and left_by_values.dtype != object:
by_dtype = f"{left_by_values.dtype}_t"
else:
by_dtype = object
bli, bri = asof_join_backward_on_X_by_Y[f"{left_values.dtype}_t", by_dtype](
bli, bri = asof_join_backward_on_X_by_Y(
left_values,
right_values,
left_by_values,
Expand All @@ -869,7 +859,7 @@ def asof_join_nearest_on_X_by_Y(ndarray[numeric_t] left_values,
tolerance,
use_hashtable
)
fli, fri = asof_join_forward_on_X_by_Y[f"{left_values.dtype}_t", by_dtype](
fli, fri = asof_join_forward_on_X_by_Y(
left_values,
right_values,
left_by_values,
Expand Down
8 changes: 4 additions & 4 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ def memory_usage_of_objects(arr: object[:]) -> int64_t:

Does not include the actual bytes of the pointers
"""
cdef:
Py_ssize_t i
Py_ssize_t n
int64_t size = 0
i: Py_ssize_t
n: Py_ssize_t
size: int64_t

size = 0
n = len(arr)
for i in range(n):
size += arr[i].__sizeof__()
Expand Down
8 changes: 2 additions & 6 deletions pandas/_libs/sparse_op_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ def get_dispatch(dtypes):

{{for opname, dtype, rdtype in get_dispatch(dtypes)}}

{{if opname == "pow"}}
@cython.cpow(True) # Cython 3 matches Python pow, which isn't what we want here
{{endif}}

@cython.wraparound(False)
@cython.boundscheck(False)
cdef tuple block_op_{{opname}}_{{dtype}}({{dtype}}_t[:] x_,
Expand Down Expand Up @@ -231,9 +229,7 @@ cdef tuple block_op_{{opname}}_{{dtype}}({{dtype}}_t[:] x_,

return out, out_index, {{(opname, 'xfill', 'yfill', dtype) | get_op}}

{{if opname == "pow"}}
@cython.cpow(True) # Cython 3 matches Python pow, which isn't what we want here
{{endif}}

@cython.wraparound(False)
@cython.boundscheck(False)
cdef tuple int_op_{{opname}}_{{dtype}}({{dtype}}_t[:] x_,
Expand Down
Loading