Skip to content

Commit cb0d339

Browse files
authored
STYLE: dont use pd api types in tests (#39293)
1 parent 0d5a644 commit cb0d339

File tree

15 files changed

+42
-27
lines changed

15 files changed

+42
-27
lines changed

.pre-commit-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ repos:
168168
pandas/tests/io/excel/test_writers\.py
169169
|pandas/tests/io/pytables/common\.py
170170
|pandas/tests/io/pytables/test_store\.py$
171+
- id: no-pandas-api-types
172+
name: Check code for instances of pd.api.types
173+
entry: (pd|pandas)\.api\.types\.
174+
language: pygrep
175+
types: [python]
176+
files: ^pandas/tests/
171177
- repo: https://github.com/asottile/yesqa
172178
rev: v1.2.2
173179
hooks:

asv_bench/benchmarks/reshape.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pandas as pd
77
from pandas import DataFrame, MultiIndex, date_range, melt, wide_to_long
8+
from pandas.api.types import CategoricalDtype
89

910

1011
class Melt:
@@ -196,7 +197,7 @@ def setup(self):
196197
categories = list(string.ascii_letters[:12])
197198
s = pd.Series(
198199
np.random.choice(categories, size=1000000),
199-
dtype=pd.api.types.CategoricalDtype(categories),
200+
dtype=CategoricalDtype(categories),
200201
)
201202
self.s = s
202203

pandas/core/dtypes/inference.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,20 @@ def is_number(obj) -> bool:
5050
5151
Examples
5252
--------
53-
>>> pd.api.types.is_number(1)
53+
>>> from pandas.api.types import is_number
54+
>>> is_number(1)
5455
True
55-
>>> pd.api.types.is_number(7.15)
56+
>>> is_number(7.15)
5657
True
5758
5859
Booleans are valid because they are int subclass.
5960
60-
>>> pd.api.types.is_number(False)
61+
>>> is_number(False)
6162
True
6263
63-
>>> pd.api.types.is_number("foo")
64+
>>> is_number("foo")
6465
False
65-
>>> pd.api.types.is_number("5")
66+
>>> is_number("5")
6667
False
6768
"""
6869
return isinstance(obj, (Number, np.number))

pandas/core/generic.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5813,7 +5813,8 @@ def astype(
58135813
58145814
Convert to ordered categorical type with custom ordering:
58155815
5816-
>>> cat_dtype = pd.api.types.CategoricalDtype(
5816+
>>> from pandas.api.types import CategoricalDtype
5817+
>>> cat_dtype = CategoricalDtype(
58175818
... categories=[2, 1], ordered=True)
58185819
>>> ser.astype(cat_dtype)
58195820
0 1

pandas/testing.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Public testing utility functions.
33
"""
44

5+
56
from pandas._testing import (
67
assert_extension_array_equal,
78
assert_frame_equal,

pandas/tests/extension/arrow/arrays.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
register_extension_dtype,
2424
take,
2525
)
26+
from pandas.api.types import is_scalar
2627
from pandas.core.arraylike import OpsMixin
2728

2829

@@ -91,7 +92,7 @@ def __repr__(self):
9192
return f"{type(self).__name__}({repr(self._data)})"
9293

9394
def __getitem__(self, item):
94-
if pd.api.types.is_scalar(item):
95+
if is_scalar(item):
9596
return self._data.to_pandas()[item]
9697
else:
9798
vals = self._data.to_pandas()[item]

pandas/tests/extension/arrow/test_bool.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pandas as pd
55
import pandas._testing as tm
6+
from pandas.api.types import is_bool_dtype
67
from pandas.tests.extension import base
78

89
pytest.importorskip("pyarrow", minversion="0.13.0")
@@ -89,7 +90,7 @@ class TestReduceBoolean(base.BaseBooleanReduceTests):
8990

9091

9192
def test_is_bool_dtype(data):
92-
assert pd.api.types.is_bool_dtype(data)
93+
assert is_bool_dtype(data)
9394
assert pd.core.common.is_bool_indexer(data)
9495
s = pd.Series(range(len(data)))
9596
result = s[data]

pandas/tests/extension/base/dtype.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
import pandas as pd
7+
from pandas.api.types import is_object_dtype, is_string_dtype
78

89
from .base import BaseExtensionTests
910

@@ -41,10 +42,10 @@ def test_is_dtype_other_input(self, dtype):
4142
assert dtype.is_dtype([1, 2, 3]) is False
4243

4344
def test_is_not_string_type(self, dtype):
44-
return not pd.api.types.is_string_dtype(dtype)
45+
return not is_string_dtype(dtype)
4546

4647
def test_is_not_object_type(self, dtype):
47-
return not pd.api.types.is_object_dtype(dtype)
48+
return not is_object_dtype(dtype)
4849

4950
def test_eq_with_str(self, dtype):
5051
assert dtype == dtype.name

pandas/tests/extension/decimal/array.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
import numpy as np
1010

1111
from pandas.core.dtypes.base import ExtensionDtype
12-
from pandas.core.dtypes.common import is_dtype_equal, is_list_like, pandas_dtype
12+
from pandas.core.dtypes.common import is_dtype_equal, pandas_dtype
1313

1414
import pandas as pd
1515
from pandas.api.extensions import no_default, register_extension_dtype
16+
from pandas.api.types import is_list_like, is_scalar
1617
from pandas.core.arraylike import OpsMixin
1718
from pandas.core.arrays import ExtensionArray, ExtensionScalarOpsMixin
1819
from pandas.core.indexers import check_array_indexer
@@ -144,8 +145,8 @@ def astype(self, dtype, copy=True):
144145
return super().astype(dtype, copy=copy)
145146

146147
def __setitem__(self, key, value):
147-
if pd.api.types.is_list_like(value):
148-
if pd.api.types.is_scalar(key):
148+
if is_list_like(value):
149+
if is_scalar(key):
149150
raise ValueError("setting an array element with a sequence.")
150151
value = [decimal.Decimal(v) for v in value]
151152
else:

pandas/tests/extension/json/array.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import pandas as pd
2929
from pandas.api.extensions import ExtensionArray, ExtensionDtype
30+
from pandas.api.types import is_bool_dtype
3031

3132

3233
class JSONDtype(ExtensionDtype):
@@ -82,7 +83,7 @@ def __getitem__(self, item):
8283
return type(self)(self.data[item])
8384
else:
8485
item = pd.api.indexers.check_array_indexer(self, item)
85-
if pd.api.types.is_bool_dtype(item.dtype):
86+
if is_bool_dtype(item.dtype):
8687
return self._from_sequence([x for x, m in zip(self, item) if m])
8788
# integer
8889
return type(self)([self.data[i] for i in item])

pandas/tests/extension/list/array.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pandas.core.dtypes.base import ExtensionDtype
1616

1717
import pandas as pd
18+
from pandas.api.types import is_object_dtype, is_string_dtype
1819
from pandas.core.arrays import ExtensionArray
1920

2021

@@ -106,9 +107,7 @@ def astype(self, dtype, copy=True):
106107
if copy:
107108
return self.copy()
108109
return self
109-
elif pd.api.types.is_string_dtype(dtype) and not pd.api.types.is_object_dtype(
110-
dtype
111-
):
110+
elif is_string_dtype(dtype) and not is_object_dtype(dtype):
112111
# numpy has problems with astype(str) for nested elements
113112
return np.array([str(x) for x in self.data], dtype=dtype)
114113
return np.array(self.data, dtype=dtype, copy=copy)

pandas/tests/extension/test_floating.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import pandas as pd
2222
import pandas._testing as tm
23+
from pandas.api.types import is_float_dtype
2324
from pandas.core.arrays.floating import Float32Dtype, Float64Dtype
2425
from pandas.tests.extension import base
2526

@@ -101,7 +102,7 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError):
101102
if (
102103
hasattr(other, "dtype")
103104
and not is_extension_array_dtype(other.dtype)
104-
and pd.api.types.is_float_dtype(other.dtype)
105+
and is_float_dtype(other.dtype)
105106
):
106107
# other is np.float64 and would therefore always result in
107108
# upcasting, so keeping other as same numpy_dtype

pandas/tests/extension/test_integer.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
import numpy as np
1717
import pytest
1818

19-
from pandas.core.dtypes.common import is_extension_array_dtype
20-
2119
import pandas as pd
2220
import pandas._testing as tm
21+
from pandas.api.types import is_extension_array_dtype, is_integer_dtype
2322
from pandas.core.arrays.integer import (
2423
Int8Dtype,
2524
Int16Dtype,
@@ -119,7 +118,7 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError):
119118
if (
120119
hasattr(other, "dtype")
121120
and not is_extension_array_dtype(other.dtype)
122-
and pd.api.types.is_integer_dtype(other.dtype)
121+
and is_integer_dtype(other.dtype)
123122
):
124123
# other is np.int64 and would therefore always result in
125124
# upcasting, so keeping other as same numpy_dtype

pandas/tests/frame/test_ufunc.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pandas as pd
77
import pandas._testing as tm
8+
from pandas.api.types import is_extension_array_dtype
89

910
dtypes = [
1011
"int64",
@@ -28,7 +29,7 @@ def test_unary_unary(dtype):
2829
@pytest.mark.parametrize("dtype", dtypes)
2930
def test_unary_binary(request, dtype):
3031
# unary input, binary output
31-
if pd.api.types.is_extension_array_dtype(dtype) or isinstance(dtype, dict):
32+
if is_extension_array_dtype(dtype) or isinstance(dtype, dict):
3233
request.node.add_marker(
3334
pytest.mark.xfail(
3435
reason="Extension / mixed with multiple outputs not implemented."
@@ -63,9 +64,9 @@ def test_binary_input_dispatch_binop(dtype):
6364
@pytest.mark.parametrize("dtype_b", dtypes)
6465
def test_binary_input_aligns_columns(request, dtype_a, dtype_b):
6566
if (
66-
pd.api.types.is_extension_array_dtype(dtype_a)
67+
is_extension_array_dtype(dtype_a)
6768
or isinstance(dtype_a, dict)
68-
or pd.api.types.is_extension_array_dtype(dtype_b)
69+
or is_extension_array_dtype(dtype_b)
6970
or isinstance(dtype_b, dict)
7071
):
7172
request.node.add_marker(
@@ -98,7 +99,7 @@ def test_binary_input_aligns_columns(request, dtype_a, dtype_b):
9899

99100
@pytest.mark.parametrize("dtype", dtypes)
100101
def test_binary_input_aligns_index(request, dtype):
101-
if pd.api.types.is_extension_array_dtype(dtype) or isinstance(dtype, dict):
102+
if is_extension_array_dtype(dtype) or isinstance(dtype, dict):
102103
request.node.add_marker(
103104
pytest.mark.xfail(
104105
reason="Extension / mixed with multiple inputs not implemented."

pandas/tests/indexing/multiindex/test_loc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def test_loc_getitem_int_slice(self):
255255
def test_loc_getitem_nested_indexer(self, indexer_type_1, indexer_type_2):
256256
# GH #19686
257257
# .loc should work with nested indexers which can be
258-
# any list-like objects (see `pandas.api.types.is_list_like`) or slices
258+
# any list-like objects (see `is_list_like` (`pandas.api.types`)) or slices
259259

260260
def convert_nested_indexer(indexer_type, keys):
261261
if indexer_type == np.ndarray:

0 commit comments

Comments
 (0)