Skip to content

Commit 889e2a7

Browse files
committed
DOC: Document pandas.core.dtypes.common
Closes pandas-devgh-15895.
1 parent 39cc1d0 commit 889e2a7

File tree

1 file changed

+201
-5
lines changed

1 file changed

+201
-5
lines changed

pandas/core/dtypes/common.py

+201-5
Original file line numberDiff line numberDiff line change
@@ -82,46 +82,239 @@ def _ensure_categorical(arr):
8282

8383

8484
def is_object_dtype(arr_or_dtype):
85+
"""
86+
Check whether an array-like 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 array-like 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+
85112
if arr_or_dtype is None:
86113
return False
87114
tipo = _get_dtype_type(arr_or_dtype)
88115
return issubclass(tipo, np.object_)
89116

90117

91118
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+
143+
>>> from scipy.sparse import bsr_matrix
144+
>>> is_sparse(bsr_matrix([1, 2, 3]))
145+
False
146+
"""
147+
93148
return isinstance(array, (ABCSparseArray, ABCSparseSeries))
94149

95150

96151
def is_scipy_sparse(array):
97-
""" return if we are a scipy.sparse.spmatrix """
152+
"""
153+
Check whether an array-like is a scipy.sparse.spmatrix instance.
154+
155+
Parameters
156+
----------
157+
array : array-like
158+
The array-like to check.
159+
160+
Returns
161+
-------
162+
is_scipy_sparse : Whether or not the array-like is a
163+
scipy.sparse.spmatrix instance.
164+
165+
Notes
166+
-----
167+
If scipy is not installed, this function will always return False.
168+
169+
Examples
170+
--------
171+
>>> from scipy.sparse import bsr_matrix
172+
>>> is_scipy_sparse(bsr_matrix([1, 2, 3]))
173+
True
174+
>>> is_scipy_sparse(pd.SparseArray([1, 2, 3]))
175+
False
176+
>>> is_scipy_sparse(pd.SparseSeries([1, 2, 3]))
177+
False
178+
"""
179+
98180
global _is_scipy_sparse
181+
99182
if _is_scipy_sparse is None:
100183
try:
101184
from scipy.sparse import issparse as _is_scipy_sparse
102185
except ImportError:
103186
_is_scipy_sparse = lambda _: False
187+
104188
return _is_scipy_sparse(array)
105189

106190

107191
def is_categorical(array):
108-
""" return if we are a categorical possibility """
192+
"""
193+
Check whether an array-like is a Categorical instance.
194+
195+
Parameters
196+
----------
197+
array : array-like
198+
The array-like to check.
199+
200+
Returns
201+
-------
202+
is_categorical : Whether or not the array-like is a Categorical instance.
203+
204+
Examples
205+
--------
206+
>>> is_categorical([1, 2, 3])
207+
False
208+
209+
Categoricals and Series Categoricals will return True.
210+
211+
>>> cat = pd.Categorical([1, 2, 3])
212+
>>> is_categorical(cat)
213+
True
214+
>>> is_categorical(pd.Series(cat))
215+
True
216+
"""
217+
109218
return isinstance(array, ABCCategorical) or is_categorical_dtype(array)
110219

111220

112221
def is_datetimetz(array):
113-
""" return if we are a datetime with tz array """
222+
"""
223+
Check whether an array-like is a datetime array-like with a timezone
224+
component in its dtype.
225+
226+
Parameters
227+
----------
228+
array : array-like
229+
The array-like to check.
230+
231+
Returns
232+
-------
233+
is_datetimetz: : Whether or not the array-like is a datetime array-like
234+
with a timezone component in its dtype.
235+
236+
Examples
237+
--------
238+
>>> is_datetimetz([1, 2, 3])
239+
False
240+
241+
Although the following examples are both DatetimeIndex objects,
242+
the first one returns False because it has no timezone component
243+
unlike the second one, which returns True.
244+
245+
>>> is_datetimetz(pd.DatetimeIndex([1, 2, 3]))
246+
False
247+
>>> is_datetimetz(pd.DatetimeIndex([1, 2, 3], tz="US/Eastern"))
248+
True
249+
250+
The object need not be a DatetimeIndex object. It just needs to have
251+
a dtype which has a timezone component.
252+
253+
>>> dtype = DatetimeTZDtype("ns", tz="US/Eastern")
254+
>>> s = pd.Series([], dtype=dtype)
255+
>>> is_datetimetz(s)
256+
True
257+
"""
258+
114259
return ((isinstance(array, ABCDatetimeIndex) and
115260
getattr(array, 'tz', None) is not None) or
116261
is_datetime64tz_dtype(array))
117262

118263

119264
def is_period(array):
120-
""" return if we are a period array """
265+
"""
266+
Check whether an array-like is a periodical index.
267+
268+
Parameters
269+
----------
270+
array : array-like
271+
The array-like to check.
272+
273+
Returns
274+
-------
275+
is_period: : Whether or not the array-like is a periodical index.
276+
277+
Examples
278+
--------
279+
>>> is_period([1, 2, 3])
280+
False
281+
>>> is_period(pd.Index([1, 2, 3]))
282+
False
283+
>>> is_period(pd.PeriodIndex(["2017-01-01"], freq="D"))
284+
True
285+
"""
286+
121287
return isinstance(array, ABCPeriodIndex) or is_period_arraylike(array)
122288

123289

124290
def is_datetime64_dtype(arr_or_dtype):
291+
"""
292+
Check whether an array-like or dtype is of the datetime64 dtype.
293+
294+
Parameters
295+
----------
296+
arr_or_dtype : array-like, dtype
297+
The array-like or dtype to check.
298+
299+
Returns
300+
-------
301+
is_datetime64_dtype : Whether or not the array-like or dtype is of
302+
the datetime64 dtype.
303+
304+
Examples
305+
--------
306+
>>> is_datetime64_dtype(object)
307+
False
308+
>>> is_datetime64_dtype(np.datetime64)
309+
True
310+
>>> is_datetime64_dtype(np.array([], dtype=int))
311+
False
312+
>>> is_datetime64_dtype(np.array([], dtype=np.datetime64))
313+
True
314+
>>> is_datetime64_dtype([1, 2, 3])
315+
False
316+
"""
317+
125318
if arr_or_dtype is None:
126319
return False
127320
try:
@@ -132,6 +325,9 @@ def is_datetime64_dtype(arr_or_dtype):
132325

133326

134327
def is_datetime64tz_dtype(arr_or_dtype):
328+
"""
329+
Check
330+
"""
135331
if arr_or_dtype is None:
136332
return False
137333
return DatetimeTZDtype.is_dtype(arr_or_dtype)

0 commit comments

Comments
 (0)