@@ -82,20 +82,102 @@ def _ensure_categorical(arr):
82
82
83
83
84
84
def is_object_dtype (arr_or_dtype ):
85
+ """
86
+ Check whether an object or dtype is of the object dtype.
87
+
88
+ Parameters
89
+ ----------
90
+ arr_or_dtype : array-like, dtype
91
+ The array-like or dtype to check.
92
+
93
+ Returns
94
+ -------
95
+ is_object_dtype : Whether or not the object or dtype is of
96
+ the object dtype.
97
+
98
+ Examples
99
+ --------
100
+ >>> is_object_dtype(object)
101
+ True
102
+ >>> is_object_dtype(int)
103
+ False
104
+ >>> is_object_dtype(np.array([], dtype=object))
105
+ True
106
+ >>> is_object_dtype(np.array([], dtype=int))
107
+ False
108
+ >>> is_object_dtype([1, 2, 3])
109
+ False
110
+ """
111
+
85
112
if arr_or_dtype is None :
86
113
return False
87
114
tipo = _get_dtype_type (arr_or_dtype )
88
115
return issubclass (tipo , np .object_ )
89
116
90
117
91
118
def is_sparse (array ):
92
- """ return if we are a sparse array """
119
+ """
120
+ Check whether an array-like is a pandas sparse array.
121
+
122
+ Parameters
123
+ ----------
124
+ array : array-like
125
+ The array-like to check.
126
+
127
+ Returns
128
+ -------
129
+ is_sparse : Whether or not the array-like is a pandas sparse array.
130
+
131
+ Examples
132
+ --------
133
+ >>> is_sparse(np.array([1, 2, 3]))
134
+ False
135
+ >>> is_sparse(pd.SparseArray([1, 2, 3]))
136
+ True
137
+ >>> is_sparse(pd.SparseSeries([1, 2, 3]))
138
+ True
139
+
140
+ This function checks only for pandas sparse array instances, so
141
+ sparse arrays from other libraries will return False.
142
+ >>> from scipy.sparse import bsr_matrix
143
+ >>> is_sparse(bsr_matrix([1, 2, 3]))
144
+ False
145
+ """
146
+
93
147
return isinstance (array , (ABCSparseArray , ABCSparseSeries ))
94
148
95
149
96
150
def is_scipy_sparse (array ):
97
- """ return if we are a scipy.sparse.spmatrix """
151
+ """
152
+ Check whether an array-like is a scipy.sparse.spmatrix instance.
153
+
154
+ Parameters
155
+ ----------
156
+ array : array-like
157
+ The array-like to check.
158
+
159
+ Returns
160
+ -------
161
+ is_scipy_sparse : Whether or not the array-like is a
162
+ scipy.sparse.spmatrix instance.
163
+
164
+ Notes
165
+ -----
166
+ If scipy is not installed, this function will always return False.
167
+
168
+ Examples
169
+ --------
170
+ >>> from scipy.sparse import bsr_matrix
171
+ >>> is_scipy_sparse(bsr_matrix([1, 2, 3]))
172
+ True
173
+ >>> is_scipy_sparse(pd.SparseArray([1, 2, 3]))
174
+ False
175
+ >>> is_scipy_sparse(pd.SparseSeries([1, 2, 3]))
176
+ False
177
+ """
178
+
98
179
global _is_scipy_sparse
180
+
99
181
if _is_scipy_sparse is None :
100
182
try :
101
183
from scipy .sparse import issparse as _is_scipy_sparse
@@ -105,7 +187,31 @@ def is_scipy_sparse(array):
105
187
106
188
107
189
def is_categorical (array ):
108
- """ return if we are a categorical possibility """
190
+ """
191
+ Check whether an array-like is a Categorical instance.
192
+
193
+ Parameters
194
+ ----------
195
+ array : array-like
196
+ The array-like to check.
197
+
198
+ Returns
199
+ -------
200
+ is_categorical : Whether or not the array-like is a Categorical instance.
201
+
202
+ Examples
203
+ --------
204
+ >>> is_categorical([1, 2, 3])
205
+ False
206
+
207
+ Categoricals and Series Categoricals will return True.
208
+ >>> cat = pd.Categorical([1, 2, 3])
209
+ >>> is_categorical(cat)
210
+ True
211
+ >>> is_categorical(pd.Series(cat))
212
+ True
213
+ """
214
+
109
215
return isinstance (array , ABCCategorical ) or is_categorical_dtype (array )
110
216
111
217
0 commit comments