Skip to content

Commit 3face07

Browse files
jorisvandenbosschejreback
authored andcommitted
DOC: better document Dtypes docstrings + avoid sphinx warnings (#26067)
1 parent 9f04bb8 commit 3face07

File tree

4 files changed

+170
-45
lines changed

4 files changed

+170
-45
lines changed

doc/source/reference/arrays.rst

+39
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ If the data are tz-aware, then every value in the array must have the same timez
146146
:toctree: api/
147147

148148
arrays.DatetimeArray
149+
150+
.. autosummary::
151+
:toctree: api/
152+
:template: autosummary/class_without_autosummary.rst
153+
149154
DatetimeTZDtype
150155

151156
.. _api.arrays.timedelta:
@@ -260,6 +265,11 @@ Every period in a ``PeriodArray`` must have the same ``freq``.
260265
:toctree: api/
261266

262267
arrays.PeriodArray
268+
269+
.. autosummary::
270+
:toctree: api/
271+
:template: autosummary/class_without_autosummary.rst
272+
263273
PeriodDtype
264274

265275
.. _api.arrays.interval:
@@ -296,6 +306,11 @@ A collection of intervals may be stored in an :class:`arrays.IntervalArray`.
296306
:toctree: api/
297307

298308
arrays.IntervalArray
309+
310+
.. autosummary::
311+
:toctree: api/
312+
:template: autosummary/class_without_autosummary.rst
313+
299314
IntervalDtype
300315

301316
.. _api.arrays.integer_na:
@@ -310,6 +325,11 @@ Pandas provides this through :class:`arrays.IntegerArray`.
310325
:toctree: api/
311326

312327
arrays.IntegerArray
328+
329+
.. autosummary::
330+
:toctree: api/
331+
:template: autosummary/class_without_autosummary.rst
332+
313333
Int8Dtype
314334
Int16Dtype
315335
Int32Dtype
@@ -396,8 +416,27 @@ be stored efficiently as a :class:`SparseArray`.
396416
:toctree: api/
397417

398418
SparseArray
419+
420+
.. autosummary::
421+
:toctree: api/
422+
:template: autosummary/class_without_autosummary.rst
423+
399424
SparseDtype
400425

401426
The ``Series.sparse`` accessor may be used to access sparse-specific attributes
402427
and methods if the :class:`Series` contains sparse values. See
403428
:ref:`api.series.sparse` for more.
429+
430+
431+
432+
.. Dtype attributes which are manually listed in their docstrings: including
433+
.. it here to make sure a docstring page is built for them
434+
435+
..
436+
.. autosummary::
437+
:toctree: api/
438+
439+
DatetimeTZDtype.unit
440+
DatetimeTZDtype.tz
441+
PeriodDtype.freq
442+
IntervalDtype.subtype

pandas/core/arrays/integer.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,17 @@ def integer_arithmetic_method(self, other):
682682

683683
module = sys.modules[__name__]
684684

685+
_dtype_docstring = """
686+
An ExtensionDtype for {dtype} integer data.
687+
688+
Attributes
689+
----------
690+
None
691+
692+
Methods
693+
-------
694+
None
695+
"""
685696

686697
# create the Dtype
687698
_dtypes = {}
@@ -695,7 +706,8 @@ def integer_arithmetic_method(self, other):
695706
classname = "{}Dtype".format(name)
696707
numpy_dtype = getattr(np, dtype)
697708
attributes_dict = {'type': numpy_dtype,
698-
'name': name}
709+
'name': name,
710+
'__doc__': _dtype_docstring.format(dtype=dtype)}
699711
dtype_type = register_extension_dtype(
700712
type(classname, (_IntegerDtype, ), attributes_dict)
701713
)

pandas/core/arrays/sparse.py

+8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ class SparseDtype(ExtensionDtype):
7272
=========== ==========
7373
7474
The default value may be overridden by specifying a `fill_value`.
75+
76+
Attributes
77+
----------
78+
None
79+
80+
Methods
81+
-------
82+
None
7583
"""
7684
# We include `_is_na_fill_value` in the metadata to avoid hash collisions
7785
# between SparseDtype(float, 0.0) and SparseDtype(float, nan).

pandas/core/dtypes/dtypes.py

+110-44
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class CategoricalDtypeType(type):
169169
@register_extension_dtype
170170
class CategoricalDtype(PandasExtensionDtype, ExtensionDtype):
171171
"""
172-
Type for categorical data with the categories and orderedness
172+
Type for categorical data with the categories and orderedness.
173173
174174
.. versionchanged:: 0.21.0
175175
@@ -334,6 +334,9 @@ def _finalize(self, categories, ordered, fastpath=False):
334334
self._ordered = ordered
335335

336336
def __setstate__(self, state):
337+
# for pickle compat. __get_state__ is defined in the
338+
# PandasExtensionDtype superclass and uses the public properties to
339+
# pickle -> need to set the settable private ones here (see GH26067)
337340
self._categories = state.pop('categories', None)
338341
self._ordered = state.pop('ordered', False)
339342

@@ -570,13 +573,40 @@ def _is_boolean(self):
570573

571574
@register_extension_dtype
572575
class DatetimeTZDtype(PandasExtensionDtype, ExtensionDtype):
573-
574576
"""
575-
A np.dtype duck-typed class, suitable for holding a custom datetime with tz
576-
dtype.
577+
An ExtensionDtype for timezone-aware datetime data.
578+
579+
**This is not an actual numpy dtype**, but a duck type.
580+
581+
Parameters
582+
----------
583+
unit : str, default "ns"
584+
The precision of the datetime data. Currently limited
585+
to ``"ns"``.
586+
tz : str, int, or datetime.tzinfo
587+
The timezone.
588+
589+
Attributes
590+
----------
591+
unit
592+
tz
593+
594+
Methods
595+
-------
596+
None
577597
578-
THIS IS NOT A REAL NUMPY DTYPE, but essentially a sub-class of
579-
np.datetime64[ns]
598+
Raises
599+
------
600+
pytz.UnknownTimeZoneError
601+
When the requested timezone cannot be found.
602+
603+
Examples
604+
--------
605+
>>> pd.DatetimeTZDtype(tz='UTC')
606+
datetime64[ns, UTC]
607+
608+
>>> pd.DatetimeTZDtype(tz='dateutil/US/Central')
609+
datetime64[ns, tzfile('/usr/share/zoneinfo/US/Central')]
580610
"""
581611
type = Timestamp # type: Type[Timestamp]
582612
kind = 'M' # type: str_type
@@ -589,30 +619,6 @@ class DatetimeTZDtype(PandasExtensionDtype, ExtensionDtype):
589619
_cache = {} # type: Dict[str_type, PandasExtensionDtype]
590620

591621
def __init__(self, unit="ns", tz=None):
592-
"""
593-
An ExtensionDtype for timezone-aware datetime data.
594-
595-
Parameters
596-
----------
597-
unit : str, default "ns"
598-
The precision of the datetime data. Currently limited
599-
to ``"ns"``.
600-
tz : str, int, or datetime.tzinfo
601-
The timezone.
602-
603-
Raises
604-
------
605-
pytz.UnknownTimeZoneError
606-
When the requested timezone cannot be found.
607-
608-
Examples
609-
--------
610-
>>> pd.core.dtypes.dtypes.DatetimeTZDtype(tz='UTC')
611-
datetime64[ns, UTC]
612-
613-
>>> pd.core.dtypes.dtypes.DatetimeTZDtype(tz='dateutil/US/Central')
614-
datetime64[ns, tzfile('/usr/share/zoneinfo/US/Central')]
615-
"""
616622
if isinstance(unit, DatetimeTZDtype):
617623
unit, tz = unit.unit, unit.tz
618624

@@ -718,17 +724,40 @@ def __eq__(self, other):
718724
str(self.tz) == str(other.tz))
719725

720726
def __setstate__(self, state):
721-
# for pickle compat.
727+
# for pickle compat. __get_state__ is defined in the
728+
# PandasExtensionDtype superclass and uses the public properties to
729+
# pickle -> need to set the settable private ones here (see GH26067)
722730
self._tz = state['tz']
723731
self._unit = state['unit']
724732

725733

726734
@register_extension_dtype
727735
class PeriodDtype(ExtensionDtype, PandasExtensionDtype):
728736
"""
729-
A Period duck-typed class, suitable for holding a period with freq dtype.
737+
An ExtensionDtype for Period data.
738+
739+
**This is not an actual numpy dtype**, but a duck type.
740+
741+
Parameters
742+
----------
743+
freq : str or DateOffset
744+
The frequency of this PeriodDtype
745+
746+
Attributes
747+
----------
748+
freq
730749
731-
THIS IS NOT A REAL NUMPY DTYPE, but essentially a sub-class of np.int64.
750+
Methods
751+
-------
752+
None
753+
754+
Examples
755+
--------
756+
>>> pd.PeriodDtype(freq='D')
757+
period[D]
758+
759+
>>> pd.PeriodDtype(freq=pd.offsets.MonthEnd())
760+
period[M]
732761
"""
733762
type = Period # type: Type[Period]
734763
kind = 'O' # type: str_type
@@ -751,7 +780,9 @@ def __new__(cls, freq=None):
751780

752781
elif freq is None:
753782
# empty constructor for pickle compat
754-
return object.__new__(cls)
783+
u = object.__new__(cls)
784+
u._freq = None
785+
return u
755786

756787
if not isinstance(freq, ABCDateOffset):
757788
freq = cls._parse_dtype_strict(freq)
@@ -760,10 +791,15 @@ def __new__(cls, freq=None):
760791
return cls._cache[freq.freqstr]
761792
except KeyError:
762793
u = object.__new__(cls)
763-
u.freq = freq
794+
u._freq = freq
764795
cls._cache[freq.freqstr] = u
765796
return u
766797

798+
@property
799+
def freq(self):
800+
"""The frequency object of this PeriodDtype."""
801+
return self._freq
802+
767803
@classmethod
768804
def _parse_dtype_strict(cls, freq):
769805
if isinstance(freq, str):
@@ -817,6 +853,12 @@ def __eq__(self, other):
817853

818854
return isinstance(other, PeriodDtype) and self.freq == other.freq
819855

856+
def __setstate__(self, state):
857+
# for pickle compat. __get_state__ is defined in the
858+
# PandasExtensionDtype superclass and uses the public properties to
859+
# pickle -> need to set the settable private ones here (see GH26067)
860+
self._freq = state['freq']
861+
820862
@classmethod
821863
def is_dtype(cls, dtype):
822864
"""
@@ -849,9 +891,27 @@ def construct_array_type(cls):
849891
@register_extension_dtype
850892
class IntervalDtype(PandasExtensionDtype, ExtensionDtype):
851893
"""
852-
A Interval duck-typed class, suitable for holding an interval
894+
An ExtensionDtype for Interval data.
853895
854-
THIS IS NOT A REAL NUMPY DTYPE
896+
**This is not an actual numpy dtype**, but a duck type.
897+
898+
Parameters
899+
----------
900+
subtype : str, np.dtype
901+
The dtype of the Interval bounds.
902+
903+
Attributes
904+
----------
905+
subtype
906+
907+
Methods
908+
-------
909+
None
910+
911+
Examples
912+
--------
913+
>>> pd.IntervalDtype(subtype='int64')
914+
interval[int64]
855915
"""
856916
name = 'interval'
857917
kind = None # type: Optional[str_type]
@@ -863,11 +923,6 @@ class IntervalDtype(PandasExtensionDtype, ExtensionDtype):
863923
_cache = {} # type: Dict[str_type, PandasExtensionDtype]
864924

865925
def __new__(cls, subtype=None):
866-
"""
867-
Parameters
868-
----------
869-
subtype : the dtype of the Interval
870-
"""
871926
from pandas.core.dtypes.common import (
872927
is_categorical_dtype, is_string_dtype, pandas_dtype)
873928

@@ -877,7 +932,7 @@ def __new__(cls, subtype=None):
877932
# we are called as an empty constructor
878933
# generally for pickle compat
879934
u = object.__new__(cls)
880-
u.subtype = None
935+
u._subtype = None
881936
return u
882937
elif (isinstance(subtype, str) and
883938
subtype.lower() == 'interval'):
@@ -903,10 +958,15 @@ def __new__(cls, subtype=None):
903958
return cls._cache[str(subtype)]
904959
except KeyError:
905960
u = object.__new__(cls)
906-
u.subtype = subtype
961+
u._subtype = subtype
907962
cls._cache[str(subtype)] = u
908963
return u
909964

965+
@property
966+
def subtype(self):
967+
"""The dtype of the Interval bounds."""
968+
return self._subtype
969+
910970
@classmethod
911971
def construct_array_type(cls):
912972
"""
@@ -963,6 +1023,12 @@ def __eq__(self, other):
9631023
from pandas.core.dtypes.common import is_dtype_equal
9641024
return is_dtype_equal(self.subtype, other.subtype)
9651025

1026+
def __setstate__(self, state):
1027+
# for pickle compat. __get_state__ is defined in the
1028+
# PandasExtensionDtype superclass and uses the public properties to
1029+
# pickle -> need to set the settable private ones here (see GH26067)
1030+
self._subtype = state['subtype']
1031+
9661032
@classmethod
9671033
def is_dtype(cls, dtype):
9681034
"""

0 commit comments

Comments
 (0)