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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 6 additions & 5 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 2 additions & 1 deletion
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])

0 commit comments

Comments
 (0)