Skip to content

Commit afb7c76

Browse files
authored
ENH: Improve api.types (#274)
* ENH: Improve api.types Clean and improve the typing for api.types Add tests * TST: Add tests for missing Add tests for two missing functions Change import location * MAINT: Remove is_{decimal,period}
1 parent e37f513 commit afb7c76

18 files changed

+258
-175
lines changed

pandas-stubs/_libs/lib.pyi

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
no_default = None
2-
31
def infer_dtype(value: object, skipna: bool = ...) -> str: ...
2+
def is_iterator(obj: object) -> bool: ...
3+
def is_scalar(val: object) -> bool: ...
4+
def is_list_like(obj: object, allow_sets: bool = ...) -> bool: ...
5+
def is_interval(val: object) -> bool: ...
6+
def is_complex(val: object) -> bool: ...
7+
def is_bool(val: object) -> bool: ...
8+
def is_integer(val: object) -> bool: ...
9+
def is_float(val: object) -> bool: ...

pandas-stubs/api/extensions/__init__.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ from pandas.core.arrays import (
88
ExtensionScalarOpsMixin as ExtensionScalarOpsMixin,
99
)
1010

11-
from pandas._libs.lib import no_default as no_default
12-
1311
from pandas.core.dtypes.dtypes import (
1412
ExtensionDtype as ExtensionDtype,
1513
register_extension_dtype as register_extension_dtype,

pandas-stubs/api/types/__init__.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ from pandas.core.dtypes.api import (
44
is_array_like as is_array_like,
55
is_bool as is_bool,
66
is_bool_dtype as is_bool_dtype,
7-
is_categorical as is_categorical,
87
is_categorical_dtype as is_categorical_dtype,
98
is_complex as is_complex,
109
is_complex_dtype as is_complex_dtype,
@@ -15,7 +14,6 @@ from pandas.core.dtypes.api import (
1514
is_dict_like as is_dict_like,
1615
is_dtype_equal as is_dtype_equal,
1716
is_extension_array_dtype as is_extension_array_dtype,
18-
is_extension_type as is_extension_type,
1917
is_file_like as is_file_like,
2018
is_float as is_float,
2119
is_float_dtype as is_float_dtype,

pandas-stubs/core/arrays/sparse/__init__.pyi

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,5 @@ from .accessor import (
22
SparseAccessor as SparseAccessor,
33
SparseFrameAccessor as SparseFrameAccessor,
44
)
5-
from .array import (
6-
BlockIndex as BlockIndex,
7-
IntIndex as IntIndex,
8-
SparseArray as SparseArray,
9-
)
5+
from .array import SparseArray as SparseArray
106
from .dtype import SparseDtype as SparseDtype

pandas-stubs/core/arrays/sparse/accessor.pyi

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
from pandas.core.accessor import (
2-
PandasDelegate as PandasDelegate,
3-
delegate_names as delegate_names,
4-
)
5-
from pandas.core.arrays.sparse.array import SparseArray as SparseArray
6-
from pandas.core.arrays.sparse.dtype import SparseDtype as SparseDtype
7-
8-
from pandas.core.dtypes.cast import find_common_type as find_common_type
1+
from pandas.core.accessor import PandasDelegate
92

103
class BaseAccessor:
114
def __init__(self, data=...) -> None: ...

pandas-stubs/core/arrays/sparse/array.pyi

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,9 @@
11
import numpy as np
22
from pandas.core.arrays import (
3-
ExtensionArray as ExtensionArray,
3+
ExtensionArray,
44
ExtensionOpsMixin,
55
)
6-
from pandas.core.arrays.sparse.dtype import SparseDtype as SparseDtype
76
from pandas.core.base import PandasObject as PandasObject
8-
from pandas.core.construction import sanitize_array as sanitize_array
9-
from pandas.core.indexers import check_array_indexer as check_array_indexer
10-
from pandas.core.missing import interpolate_2d as interpolate_2d
11-
12-
from pandas._libs.sparse import (
13-
BlockIndex as BlockIndex,
14-
IntIndex as IntIndex,
15-
SparseIndex as SparseIndex,
16-
)
17-
from pandas._libs.tslibs import NaT as NaT
18-
from pandas.errors import PerformanceWarning as PerformanceWarning
19-
20-
from pandas.core.dtypes.cast import (
21-
astype_nansafe as astype_nansafe,
22-
construct_1d_arraylike_from_scalar as construct_1d_arraylike_from_scalar,
23-
find_common_type as find_common_type,
24-
)
25-
from pandas.core.dtypes.common import (
26-
is_array_like as is_array_like,
27-
is_bool_dtype as is_bool_dtype,
28-
is_datetime64_any_dtype as is_datetime64_any_dtype,
29-
is_dtype_equal as is_dtype_equal,
30-
is_integer as is_integer,
31-
is_object_dtype as is_object_dtype,
32-
is_scalar as is_scalar,
33-
is_string_dtype as is_string_dtype,
34-
pandas_dtype as pandas_dtype,
35-
)
36-
from pandas.core.dtypes.generic import ABCSeries as ABCSeries
37-
from pandas.core.dtypes.missing import (
38-
isna as isna,
39-
na_value_for_dtype as na_value_for_dtype,
40-
notna as notna,
41-
)
427

438
class SparseArray(PandasObject, ExtensionArray, ExtensionOpsMixin):
449
def __init__(

pandas-stubs/core/arrays/sparse/dtype.pyi

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,10 @@ from pandas._typing import (
33
Scalar,
44
)
55

6-
from pandas.core.dtypes.base import ExtensionDtype as ExtensionDtype
7-
from pandas.core.dtypes.cast import astype_nansafe as astype_nansafe
8-
from pandas.core.dtypes.common import (
9-
is_bool_dtype as is_bool_dtype,
10-
is_object_dtype as is_object_dtype,
11-
is_scalar as is_scalar,
12-
is_string_dtype as is_string_dtype,
13-
pandas_dtype as pandas_dtype,
14-
)
6+
from pandas.core.dtypes.base import ExtensionDtype
157
from pandas.core.dtypes.dtypes import (
168
register_extension_dtype as register_extension_dtype,
179
)
18-
from pandas.core.dtypes.missing import (
19-
isna as isna,
20-
na_value_for_dtype as na_value_for_dtype,
21-
)
2210

2311
# merged types from pylance
2412

pandas-stubs/core/dtypes/api.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ from pandas.core.dtypes.common import (
22
is_array_like as is_array_like,
33
is_bool as is_bool,
44
is_bool_dtype as is_bool_dtype,
5-
is_categorical as is_categorical,
65
is_categorical_dtype as is_categorical_dtype,
76
is_complex as is_complex,
87
is_complex_dtype as is_complex_dtype,
@@ -13,7 +12,6 @@ from pandas.core.dtypes.common import (
1312
is_dict_like as is_dict_like,
1413
is_dtype_equal as is_dtype_equal,
1514
is_extension_array_dtype as is_extension_array_dtype,
16-
is_extension_type as is_extension_type,
1715
is_file_like as is_file_like,
1816
is_float as is_float,
1917
is_float_dtype as is_float_dtype,

pandas-stubs/core/dtypes/cast.pyi

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +0,0 @@
1-
import numpy as np
2-
3-
from pandas._typing import Dtype
4-
5-
def is_nested_object(obj) -> bool: ...
6-
def infer_dtype_from(val, pandas_dtype: bool = ...): ...
7-
def infer_dtype_from_scalar(val, pandas_dtype: bool = ...): ...
8-
def infer_dtype_from_array(arr, pandas_dtype: bool = ...): ...
9-
def invalidate_string_dtypes(dtype_set) -> None: ...
10-
def coerce_indexer_dtype(indexer, categories): ...
11-
def astype_nansafe(arr, dtype, copy: bool = ..., skipna: bool = ...): ...
12-
def soft_convert_objects(
13-
values: np.ndarray,
14-
datetime: bool = ...,
15-
numeric: bool = ...,
16-
timedelta: bool = ...,
17-
coerce: bool = ...,
18-
copy: bool = ...,
19-
): ...
20-
def convert_dtypes(
21-
input_array,
22-
convert_string: bool = ...,
23-
convert_integer: bool = ...,
24-
convert_boolean: bool = ...,
25-
) -> Dtype: ...
26-
def find_common_type(types): ...
27-
def construct_1d_arraylike_from_scalar(value, length: int, dtype): ...
28-
def construct_1d_object_array_from_listlike(values): ...

pandas-stubs/core/dtypes/common.pyi

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
from typing import Callable
1+
from typing import Union
22

33
import numpy as np
4+
import pandas as pd
5+
6+
from pandas._typing import (
7+
ArrayLike,
8+
DtypeObj,
9+
npt,
10+
)
411

512
from pandas.core.dtypes.inference import (
613
is_array_like as is_array_like,
@@ -21,37 +28,28 @@ from pandas.core.dtypes.inference import (
2128
is_scalar as is_scalar,
2229
)
2330

24-
def classes(*klasses) -> Callable: ...
25-
def classes_and_not_datetimelike(*klasses) -> Callable: ...
26-
def is_object_dtype(arr_or_dtype) -> bool: ...
27-
def is_sparse(arr) -> bool: ...
28-
def is_scipy_sparse(arr) -> bool: ...
29-
def is_categorical(arr) -> bool: ...
30-
def is_datetime64_dtype(arr_or_dtype) -> bool: ...
31-
def is_datetime64tz_dtype(arr_or_dtype) -> bool: ...
32-
def is_timedelta64_dtype(arr_or_dtype) -> bool: ...
33-
def is_period_dtype(arr_or_dtype) -> bool: ...
34-
def is_interval_dtype(arr_or_dtype) -> bool: ...
35-
def is_categorical_dtype(arr_or_dtype) -> bool: ...
36-
def is_string_dtype(arr_or_dtype) -> bool: ...
31+
_ArrayOrDtype = Union[ArrayLike, npt.DTypeLike, pd.Series, pd.DataFrame]
32+
33+
def is_object_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
34+
def is_sparse(arr: ArrayLike | pd.Series | pd.DataFrame) -> bool: ...
35+
def is_datetime64_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
36+
def is_datetime64tz_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
37+
def is_timedelta64_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
38+
def is_period_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
3739
def is_dtype_equal(source, target) -> bool: ...
38-
def is_any_int_dtype(arr_or_dtype) -> bool: ...
39-
def is_integer_dtype(arr_or_dtype) -> bool: ...
40-
def is_signed_integer_dtype(arr_or_dtype) -> bool: ...
41-
def is_unsigned_integer_dtype(arr_or_dtype) -> bool: ...
42-
def is_int64_dtype(arr_or_dtype) -> bool: ...
43-
def is_datetime64_any_dtype(arr_or_dtype) -> bool: ...
44-
def is_datetime64_ns_dtype(arr_or_dtype) -> bool: ...
45-
def is_timedelta64_ns_dtype(arr_or_dtype) -> bool: ...
46-
def is_datetime_or_timedelta_dtype(arr_or_dtype) -> bool: ...
47-
def is_numeric_v_string_like(a, b): ...
48-
def is_datetimelike_v_numeric(a, b): ...
49-
def needs_i8_conversion(arr_or_dtype) -> bool: ...
50-
def is_numeric_dtype(arr_or_dtype) -> bool: ...
51-
def is_float_dtype(arr_or_dtype) -> bool: ...
52-
def is_bool_dtype(arr_or_dtype) -> bool: ...
53-
def is_extension_type(arr) -> bool: ...
54-
def is_extension_array_dtype(arr_or_dtype) -> bool: ...
55-
def is_complex_dtype(arr_or_dtype) -> bool: ...
56-
def infer_dtype_from_object(dtype): ...
57-
def pandas_dtype(dtype): ...
40+
def is_interval_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
41+
def is_categorical_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
42+
def is_string_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
43+
def is_integer_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
44+
def is_signed_integer_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
45+
def is_unsigned_integer_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
46+
def is_int64_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
47+
def is_datetime64_any_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
48+
def is_datetime64_ns_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
49+
def is_timedelta64_ns_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
50+
def is_numeric_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
51+
def is_float_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
52+
def is_bool_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
53+
def is_extension_array_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
54+
def is_complex_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
55+
def pandas_dtype(dtype: object) -> DtypeObj: ...

pandas-stubs/core/dtypes/concat.pyi

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
def concat_compat(to_concat, axis: int = ...): ...
1+
from typing import (
2+
TypeVar,
3+
Union,
4+
)
5+
6+
from pandas import (
7+
Categorical,
8+
CategoricalIndex,
9+
Series,
10+
)
11+
12+
_CatT = TypeVar("_CatT", bound=Union[Categorical, CategoricalIndex, Series])
13+
214
def union_categoricals(
3-
to_union, sort_categories: bool = ..., ignore_order: bool = ...
4-
): ...
15+
to_union: list[_CatT], sort_categories: bool = ..., ignore_order: bool = ...
16+
) -> Categorical: ...

pandas-stubs/core/dtypes/dtypes.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ from typing import (
66
from pandas.core.indexes.base import Index
77

88
from pandas._libs.tslibs import ( # , timezones as timezones
9-
NaT as NaT,
109
Period as Period,
1110
Timestamp,
1211
)

pandas-stubs/core/dtypes/generic.pyi

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
from pandas import (
2-
DataFrame,
3-
Index,
4-
Series,
5-
)
1+
from pandas import Series
62
from pandas.core.arrays import ExtensionArray
7-
from pandas.core.generic import NDFrame
83

9-
ABCIndex = type[Index]
10-
11-
ABCNDFrame = type[NDFrame]
124
ABCSeries = type[Series]
13-
ABCDataFrame = type[DataFrame]
14-
155
ABCExtensionArray = type[ExtensionArray]
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
def is_bool(obj) -> bool: ...
2-
def is_integer(obj) -> bool: ...
3-
def is_float(obj) -> bool: ...
4-
def is_complex(obj) -> bool: ...
5-
def is_scalar(obj) -> bool: ...
6-
def is_decimal(obj) -> bool: ...
7-
def is_interval(obj) -> bool: ...
8-
def is_list_like(obj) -> bool: ...
9-
def is_number(obj) -> bool: ...
10-
def is_iterator(obj) -> bool: ...
11-
def is_file_like(obj) -> bool: ...
12-
def is_re(obj) -> bool: ...
13-
def is_re_compilable(obj) -> bool: ...
14-
def is_array_like(obj) -> bool: ...
15-
def is_nested_list_like(obj) -> bool: ...
16-
def is_dict_like(obj) -> bool: ...
17-
def is_named_tuple(obj) -> bool: ...
18-
def is_hashable(obj) -> bool: ...
19-
def is_sequence(obj) -> bool: ...
1+
from pandas._libs import lib
2+
3+
is_bool = lib.is_bool
4+
is_integer = lib.is_integer
5+
is_float = lib.is_float
6+
is_complex = lib.is_complex
7+
is_scalar = lib.is_scalar
8+
is_interval = lib.is_interval
9+
is_list_like = lib.is_list_like
10+
is_iterator = lib.is_iterator
11+
12+
def is_number(obj: object) -> bool: ...
13+
def is_file_like(obj: object) -> bool: ...
14+
def is_re(obj: object) -> bool: ...
15+
def is_array_like(obj: object) -> bool: ...
16+
def is_re_compilable(obj: object) -> bool: ...
17+
def is_dict_like(obj: object) -> bool: ...
18+
def is_named_tuple(obj: object) -> bool: ...
19+
def is_hashable(obj: object) -> bool: ...

pandas-stubs/core/dtypes/missing.pyi

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,3 @@ def notna(obj: Index | list | ArrayLike) -> np.ndarray: ...
3636
def notna(obj: Scalar) -> bool: ...
3737

3838
notnull = notna
39-
40-
def array_equivalent(left, right, strict_nan: bool = ...) -> bool: ...
41-
def na_value_for_dtype(dtype, compat: bool = ...): ...
42-
def remove_na_arraylike(arr): ...

pandas-stubs/core/ops/dispatch.pyi

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import numpy as np
22

3-
from pandas.core.dtypes.generic import (
4-
ABCExtensionArray as ABCExtensionArray,
5-
ABCSeries,
6-
)
3+
from pandas.core.dtypes.generic import ABCSeries
74

85
def should_extension_dispatch(left: ABCSeries, right) -> bool: ...

tests/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212

1313
def check(actual: T, klass: type, dtype: type | None = None, attr: str = "left") -> T:
14-
1514
if not isinstance(actual, klass):
1615
raise RuntimeError(f"Expected type '{klass}' but got '{type(actual)}'")
1716
if dtype is None:

0 commit comments

Comments
 (0)