diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cff9c16f5d17a..03c837d2e3162 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -169,6 +169,12 @@ repos: pandas/tests/io/excel/test_writers\.py |pandas/tests/io/pytables/common\.py |pandas/tests/io/pytables/test_store\.py$ + - id: no-pandas-api-types + name: Check code for instances of pd.api.types + entry: (pd|pandas)\.api\.types\. + language: pygrep + types: [python] + files: ^pandas/tests/ - repo: https://github.com/asottile/yesqa rev: v1.2.2 hooks: diff --git a/asv_bench/benchmarks/reshape.py b/asv_bench/benchmarks/reshape.py index 9cec8a5f7d318..da1592a2f1ab0 100644 --- a/asv_bench/benchmarks/reshape.py +++ b/asv_bench/benchmarks/reshape.py @@ -5,6 +5,7 @@ import pandas as pd from pandas import DataFrame, MultiIndex, date_range, melt, wide_to_long +from pandas.api.types import CategoricalDtype class Melt: @@ -196,7 +197,7 @@ def setup(self): categories = list(string.ascii_letters[:12]) s = pd.Series( np.random.choice(categories, size=1000000), - dtype=pd.api.types.CategoricalDtype(categories), + dtype=CategoricalDtype(categories), ) self.s = s diff --git a/pandas/core/dtypes/inference.py b/pandas/core/dtypes/inference.py index 8f8ded2ad54b1..97bffb35c28d9 100644 --- a/pandas/core/dtypes/inference.py +++ b/pandas/core/dtypes/inference.py @@ -50,19 +50,20 @@ def is_number(obj) -> bool: Examples -------- - >>> pd.api.types.is_number(1) + >>> from pandas.api.types import is_number + >>> is_number(1) True - >>> pd.api.types.is_number(7.15) + >>> is_number(7.15) True Booleans are valid because they are int subclass. - >>> pd.api.types.is_number(False) + >>> is_number(False) True - >>> pd.api.types.is_number("foo") + >>> is_number("foo") False - >>> pd.api.types.is_number("5") + >>> is_number("5") False """ return isinstance(obj, (Number, np.number)) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 3eb41db88298c..d62672019812d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5813,7 +5813,8 @@ def astype( Convert to ordered categorical type with custom ordering: - >>> cat_dtype = pd.api.types.CategoricalDtype( + >>> from pandas.api.types import CategoricalDtype + >>> cat_dtype = CategoricalDtype( ... categories=[2, 1], ordered=True) >>> ser.astype(cat_dtype) 0 1 diff --git a/pandas/testing.py b/pandas/testing.py index 0445fa5b5efc0..841b55df48556 100644 --- a/pandas/testing.py +++ b/pandas/testing.py @@ -2,6 +2,7 @@ Public testing utility functions. """ + from pandas._testing import ( assert_extension_array_equal, assert_frame_equal, diff --git a/pandas/tests/extension/arrow/arrays.py b/pandas/tests/extension/arrow/arrays.py index 65c5102e22997..532d79d32b72f 100644 --- a/pandas/tests/extension/arrow/arrays.py +++ b/pandas/tests/extension/arrow/arrays.py @@ -21,6 +21,7 @@ register_extension_dtype, take, ) +from pandas.api.types import is_scalar from pandas.core.arraylike import OpsMixin @@ -89,7 +90,7 @@ def __repr__(self): return f"{type(self).__name__}({repr(self._data)})" def __getitem__(self, item): - if pd.api.types.is_scalar(item): + if is_scalar(item): return self._data.to_pandas()[item] else: vals = self._data.to_pandas()[item] diff --git a/pandas/tests/extension/arrow/test_bool.py b/pandas/tests/extension/arrow/test_bool.py index b731859a761a4..603216a0b5bbb 100644 --- a/pandas/tests/extension/arrow/test_bool.py +++ b/pandas/tests/extension/arrow/test_bool.py @@ -3,6 +3,7 @@ import pandas as pd import pandas._testing as tm +from pandas.api.types import is_bool_dtype from pandas.tests.extension import base pytest.importorskip("pyarrow", minversion="0.13.0") @@ -89,7 +90,7 @@ class TestReduceBoolean(base.BaseBooleanReduceTests): def test_is_bool_dtype(data): - assert pd.api.types.is_bool_dtype(data) + assert is_bool_dtype(data) assert pd.core.common.is_bool_indexer(data) s = pd.Series(range(len(data))) result = s[data] diff --git a/pandas/tests/extension/base/dtype.py b/pandas/tests/extension/base/dtype.py index 154fcdc38826d..128e0a9f81e91 100644 --- a/pandas/tests/extension/base/dtype.py +++ b/pandas/tests/extension/base/dtype.py @@ -4,6 +4,7 @@ import pytest import pandas as pd +from pandas.api.types import is_object_dtype, is_string_dtype from .base import BaseExtensionTests @@ -41,10 +42,10 @@ def test_is_dtype_other_input(self, dtype): assert dtype.is_dtype([1, 2, 3]) is False def test_is_not_string_type(self, dtype): - return not pd.api.types.is_string_dtype(dtype) + return not is_string_dtype(dtype) def test_is_not_object_type(self, dtype): - return not pd.api.types.is_object_dtype(dtype) + return not is_object_dtype(dtype) def test_eq_with_str(self, dtype): assert dtype == dtype.name diff --git a/pandas/tests/extension/decimal/array.py b/pandas/tests/extension/decimal/array.py index a713550dafa5c..44fcd936729f5 100644 --- a/pandas/tests/extension/decimal/array.py +++ b/pandas/tests/extension/decimal/array.py @@ -7,10 +7,11 @@ import numpy as np from pandas.core.dtypes.base import ExtensionDtype -from pandas.core.dtypes.common import is_dtype_equal, is_list_like, pandas_dtype +from pandas.core.dtypes.common import is_dtype_equal, pandas_dtype import pandas as pd from pandas.api.extensions import no_default, register_extension_dtype +from pandas.api.types import is_list_like, is_scalar from pandas.core.arraylike import OpsMixin from pandas.core.arrays import ExtensionArray, ExtensionScalarOpsMixin from pandas.core.indexers import check_array_indexer @@ -142,8 +143,8 @@ def astype(self, dtype, copy=True): return super().astype(dtype, copy=copy) def __setitem__(self, key, value): - if pd.api.types.is_list_like(value): - if pd.api.types.is_scalar(key): + if is_list_like(value): + if is_scalar(key): raise ValueError("setting an array element with a sequence.") value = [decimal.Decimal(v) for v in value] else: diff --git a/pandas/tests/extension/json/array.py b/pandas/tests/extension/json/array.py index e3cdeb9c1951f..d3dcb1b94a188 100644 --- a/pandas/tests/extension/json/array.py +++ b/pandas/tests/extension/json/array.py @@ -25,6 +25,7 @@ import pandas as pd from pandas.api.extensions import ExtensionArray, ExtensionDtype +from pandas.api.types import is_bool_dtype class JSONDtype(ExtensionDtype): @@ -80,7 +81,7 @@ def __getitem__(self, item): return type(self)(self.data[item]) else: item = pd.api.indexers.check_array_indexer(self, item) - if pd.api.types.is_bool_dtype(item.dtype): + if is_bool_dtype(item.dtype): return self._from_sequence([x for x, m in zip(self, item) if m]) # integer return type(self)([self.data[i] for i in item]) diff --git a/pandas/tests/extension/list/array.py b/pandas/tests/extension/list/array.py index d86f90e58d897..b430bec79a56e 100644 --- a/pandas/tests/extension/list/array.py +++ b/pandas/tests/extension/list/array.py @@ -13,6 +13,7 @@ from pandas.core.dtypes.base import ExtensionDtype import pandas as pd +from pandas.api.types import is_object_dtype, is_string_dtype from pandas.core.arrays import ExtensionArray @@ -104,9 +105,7 @@ def astype(self, dtype, copy=True): if copy: return self.copy() return self - elif pd.api.types.is_string_dtype(dtype) and not pd.api.types.is_object_dtype( - dtype - ): + elif is_string_dtype(dtype) and not is_object_dtype(dtype): # numpy has problems with astype(str) for nested elements return np.array([str(x) for x in self.data], dtype=dtype) return np.array(self.data, dtype=dtype, copy=copy) diff --git a/pandas/tests/extension/test_floating.py b/pandas/tests/extension/test_floating.py index c08c31e90fecc..440d7391c558f 100644 --- a/pandas/tests/extension/test_floating.py +++ b/pandas/tests/extension/test_floating.py @@ -20,6 +20,7 @@ import pandas as pd import pandas._testing as tm +from pandas.api.types import is_float_dtype from pandas.core.arrays.floating import Float32Dtype, Float64Dtype from pandas.tests.extension import base @@ -101,7 +102,7 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError): if ( hasattr(other, "dtype") and not is_extension_array_dtype(other.dtype) - and pd.api.types.is_float_dtype(other.dtype) + and is_float_dtype(other.dtype) ): # other is np.float64 and would therefore always result in # upcasting, so keeping other as same numpy_dtype diff --git a/pandas/tests/extension/test_integer.py b/pandas/tests/extension/test_integer.py index 99a32203053c6..2a469bb388fbe 100644 --- a/pandas/tests/extension/test_integer.py +++ b/pandas/tests/extension/test_integer.py @@ -16,10 +16,9 @@ import numpy as np import pytest -from pandas.core.dtypes.common import is_extension_array_dtype - import pandas as pd import pandas._testing as tm +from pandas.api.types import is_extension_array_dtype, is_integer_dtype from pandas.core.arrays.integer import ( Int8Dtype, Int16Dtype, @@ -119,7 +118,7 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError): if ( hasattr(other, "dtype") and not is_extension_array_dtype(other.dtype) - and pd.api.types.is_integer_dtype(other.dtype) + and is_integer_dtype(other.dtype) ): # other is np.int64 and would therefore always result in # upcasting, so keeping other as same numpy_dtype diff --git a/pandas/tests/frame/test_ufunc.py b/pandas/tests/frame/test_ufunc.py index c33fa0c077ad8..62b12f8a60307 100644 --- a/pandas/tests/frame/test_ufunc.py +++ b/pandas/tests/frame/test_ufunc.py @@ -5,6 +5,7 @@ import pandas as pd import pandas._testing as tm +from pandas.api.types import is_extension_array_dtype dtypes = [ "int64", @@ -28,7 +29,7 @@ def test_unary_unary(dtype): @pytest.mark.parametrize("dtype", dtypes) def test_unary_binary(request, dtype): # unary input, binary output - if pd.api.types.is_extension_array_dtype(dtype) or isinstance(dtype, dict): + if is_extension_array_dtype(dtype) or isinstance(dtype, dict): request.node.add_marker( pytest.mark.xfail( reason="Extension / mixed with multiple outputs not implemented." @@ -63,9 +64,9 @@ def test_binary_input_dispatch_binop(dtype): @pytest.mark.parametrize("dtype_b", dtypes) def test_binary_input_aligns_columns(request, dtype_a, dtype_b): if ( - pd.api.types.is_extension_array_dtype(dtype_a) + is_extension_array_dtype(dtype_a) or isinstance(dtype_a, dict) - or pd.api.types.is_extension_array_dtype(dtype_b) + or is_extension_array_dtype(dtype_b) or isinstance(dtype_b, dict) ): request.node.add_marker( @@ -98,7 +99,7 @@ def test_binary_input_aligns_columns(request, dtype_a, dtype_b): @pytest.mark.parametrize("dtype", dtypes) def test_binary_input_aligns_index(request, dtype): - if pd.api.types.is_extension_array_dtype(dtype) or isinstance(dtype, dict): + if is_extension_array_dtype(dtype) or isinstance(dtype, dict): request.node.add_marker( pytest.mark.xfail( reason="Extension / mixed with multiple inputs not implemented." diff --git a/pandas/tests/indexing/multiindex/test_loc.py b/pandas/tests/indexing/multiindex/test_loc.py index 813ca4f98be73..4c9912f1591c7 100644 --- a/pandas/tests/indexing/multiindex/test_loc.py +++ b/pandas/tests/indexing/multiindex/test_loc.py @@ -255,7 +255,7 @@ def test_loc_getitem_int_slice(self): def test_loc_getitem_nested_indexer(self, indexer_type_1, indexer_type_2): # GH #19686 # .loc should work with nested indexers which can be - # any list-like objects (see `pandas.api.types.is_list_like`) or slices + # any list-like objects (see `is_list_like` (`pandas.api.types`)) or slices def convert_nested_indexer(indexer_type, keys): if indexer_type == np.ndarray: