Skip to content

Commit 95f2616

Browse files
committed
DOC: Document pandas.core.dtypes.common
Closes pandas-devgh-15895.
1 parent f154966 commit 95f2616

File tree

1 file changed

+233
-5
lines changed

1 file changed

+233
-5
lines changed

pandas/core/dtypes/common.py

+233-5
Original file line numberDiff line numberDiff line change
@@ -82,46 +82,242 @@ 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+
259+
# TODO: do we need this function?
260+
# It seems like a repeat of is_datetime64tz_dtype.
261+
114262
return ((isinstance(array, ABCDatetimeIndex) and
115263
getattr(array, 'tz', None) is not None) or
116264
is_datetime64tz_dtype(array))
117265

118266

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

123292

124293
def is_datetime64_dtype(arr_or_dtype):
294+
"""
295+
Check whether an array-like or dtype is of the datetime64 dtype.
296+
297+
Parameters
298+
----------
299+
arr_or_dtype : array-like, dtype
300+
The array-like or dtype to check.
301+
302+
Returns
303+
-------
304+
is_datetime64_dtype : Whether or not the array-like or dtype is of
305+
the datetime64 dtype.
306+
307+
Examples
308+
--------
309+
>>> is_datetime64_dtype(object)
310+
False
311+
>>> is_datetime64_dtype(np.datetime64)
312+
True
313+
>>> is_datetime64_dtype(np.array([], dtype=int))
314+
False
315+
>>> is_datetime64_dtype(np.array([], dtype=np.datetime64))
316+
True
317+
>>> is_datetime64_dtype([1, 2, 3])
318+
False
319+
"""
320+
125321
if arr_or_dtype is None:
126322
return False
127323
try:
@@ -132,6 +328,38 @@ def is_datetime64_dtype(arr_or_dtype):
132328

133329

134330
def is_datetime64tz_dtype(arr_or_dtype):
331+
"""
332+
Check whether an array-like or dtype is of a DatetimeTZDtype dtype.
333+
334+
Parameters
335+
----------
336+
arr_or_dtype : array-like, dtype
337+
The array-like or dtype to check.
338+
339+
Returns
340+
-------
341+
is_datetime64tz_dtype : Whether or not the array-like or dtype is
342+
of a DatetimeTZDtype dtype.
343+
344+
Examples
345+
--------
346+
>>> is_datetime64tz_dtype(object)
347+
False
348+
>>> is_datetime64tz_dtype([1, 2, 3])
349+
False
350+
>>> is_datetime64tz_dtype(pd.DatetimeIndex([1, 2, 3])) # tz-naive
351+
False
352+
>>> is_datetime64tz_dtype(pd.DatetimeIndex([1, 2, 3], tz="US/Eastern"))
353+
True
354+
355+
>>> dtype = DatetimeTZDtype("ns", tz="US/Eastern")
356+
>>> s = pd.Series([], dtype=dtype)
357+
>>> is_datetime64tz_dtype(dtype)
358+
True
359+
>>> is_datetime64tz_dtype(s)
360+
True
361+
"""
362+
135363
if arr_or_dtype is None:
136364
return False
137365
return DatetimeTZDtype.is_dtype(arr_or_dtype)

0 commit comments

Comments
 (0)