Skip to content

Commit c2590b3

Browse files
TomAugspurgerjorisvandenbossche
authored andcommitted
DOC: document subclasses in API docs with selection of specific methods/attributes (#18202)
Removes our hack in the vendored numpydoc/numpydoc.py to exclude autosummary generation for certain methods. Instead, we'll explicitly list the relevant methods in a new Methods section in those docs. This let's us include things like IntervalIndex in the API docs, without having all the regular index methods in the table, and without auto-gen making pages for all of those.
1 parent 9c799e2 commit c2590b3

File tree

21 files changed

+558
-117
lines changed

21 files changed

+558
-117
lines changed

doc/source/api.rst

+257-68
Large diffs are not rendered by default.

doc/source/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
'sphinx.ext.doctest',
5656
'sphinx.ext.extlinks',
5757
'sphinx.ext.todo',
58-
'numpydoc', # used to parse numpy-style docstrings for autodoc
58+
'numpydoc',
5959
'IPython.sphinxext.ipython_directive',
6060
'IPython.sphinxext.ipython_console_highlighting',
6161
'sphinx.ext.intersphinx',

doc/source/contributing.rst

+21
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,27 @@ Some other important things to know about the docs:
317317
doc build. This approach means that code examples will always be up to date,
318318
but it does make the doc building a bit more complex.
319319

320+
- Our API documentation in ``doc/source/api.rst`` houses the auto-generated
321+
documentation from the docstrings. For classes, there are a few subtleties
322+
around controlling which methods and attributes have pages auto-generated.
323+
324+
We have two autosummary templates for classes.
325+
326+
1. ``_templates/autosummary/class.rst``. Use this when you want to
327+
automatically generate a page for every public method and attribute on the
328+
class. The ``Attributes`` and ``Methods`` sections will be automatically
329+
added to the class' rendered documentation by numpydoc. See ``DataFrame``
330+
for an example.
331+
332+
2. ``_templates/autosummary/class_without_autosummary``. Use this when you
333+
want to pick a subset of methods / attributes to auto-generate pages for.
334+
When using this template, you should include an ``Attributes`` and
335+
``Methods`` section in the class docstring. See ``CategoricalIndex`` for an
336+
example.
337+
338+
Every method should be included in a ``toctree`` in ``api.rst``, else Sphinx
339+
will emit a warning.
340+
320341
.. note::
321342

322343
The ``.rst`` files are used to automatically generate Markdown and HTML versions

doc/sphinxext/numpydoc/numpydoc.py

-7
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ def mangle_docstrings(app, what, name, obj, options, lines,
4242
class_members_toctree=app.config.numpydoc_class_members_toctree,
4343
)
4444

45-
# PANDAS HACK (to remove the list of methods/attributes for Categorical)
46-
no_autosummary = [".Categorical", "CategoricalIndex", "IntervalIndex",
47-
"RangeIndex", "Int64Index", "UInt64Index",
48-
"Float64Index", "PeriodIndex", "CategoricalDtype"]
49-
if what == "class" and any(name.endswith(n) for n in no_autosummary):
50-
cfg['class_members_list'] = False
51-
5245
if what == 'module':
5346
# Strip top title
5447
title_re = re.compile(sixu('^\\s*[#*=]{4,}\\n[a-z0-9 -]+\\n[#*=]{4,}\\s*'),

pandas/_libs/tslib.pyx

+28-2
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ class Timestamp(_Timestamp):
282282
@classmethod
283283
def fromordinal(cls, ordinal, freq=None, tz=None, offset=None):
284284
"""
285+
Timestamp.fromordinal(ordinal, freq=None, tz=None, offset=None)
286+
285287
passed an ordinal, translate and convert to a ts
286288
note: by definition there cannot be any tz info on the ordinal itself
287289
@@ -302,8 +304,10 @@ class Timestamp(_Timestamp):
302304
@classmethod
303305
def now(cls, tz=None):
304306
"""
305-
Return the current time in the local timezone. Equivalent
306-
to datetime.now([tz])
307+
Timestamp.now(tz=None)
308+
309+
Returns new Timestamp object representing current time local to
310+
tz.
307311
308312
Parameters
309313
----------
@@ -317,6 +321,8 @@ class Timestamp(_Timestamp):
317321
@classmethod
318322
def today(cls, tz=None):
319323
"""
324+
Timestamp.today(cls, tz=None)
325+
320326
Return the current time in the local timezone. This differs
321327
from datetime.today() in that it can be localized to a
322328
passed timezone.
@@ -330,18 +336,38 @@ class Timestamp(_Timestamp):
330336

331337
@classmethod
332338
def utcnow(cls):
339+
"""
340+
Timestamp.utcnow()
341+
342+
Return a new Timestamp representing UTC day and time.
343+
"""
333344
return cls.now('UTC')
334345

335346
@classmethod
336347
def utcfromtimestamp(cls, ts):
348+
"""
349+
Timestamp.utcfromtimestamp(ts)
350+
351+
Construct a naive UTC datetime from a POSIX timestamp.
352+
"""
337353
return cls(datetime.utcfromtimestamp(ts))
338354

339355
@classmethod
340356
def fromtimestamp(cls, ts):
357+
"""
358+
Timestamp.fromtimestamp(ts)
359+
360+
timestamp[, tz] -> tz's local time from POSIX timestamp.
361+
"""
341362
return cls(datetime.fromtimestamp(ts))
342363

343364
@classmethod
344365
def combine(cls, date, time):
366+
"""
367+
Timsetamp.combine(date, time)
368+
369+
date, time -> datetime with same date and time fields
370+
"""
345371
return cls(datetime.combine(date, time))
346372

347373
def __new__(cls, object ts_input=_no_input,

pandas/_libs/tslibs/nattype.pyx

+37-8
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,39 @@ class NaTType(_NaT):
336336
tzname = _make_error_func('tzname', datetime)
337337
utcoffset = _make_error_func('utcoffset', datetime)
338338

339-
# Timestamp has empty docstring for some methods.
340-
utcfromtimestamp = _make_error_func('utcfromtimestamp', None)
341-
fromtimestamp = _make_error_func('fromtimestamp', None)
342-
combine = _make_error_func('combine', None)
343-
utcnow = _make_error_func('utcnow', None)
344-
345339
# ----------------------------------------------------------------------
346340
# The remaining methods have docstrings copy/pasted from the analogous
347341
# Timestamp methods.
348342

343+
utcfromtimestamp = _make_error_func('utcfromtimestamp', # noqa:E128
344+
"""
345+
Timestamp.utcfromtimestamp(ts)
346+
347+
Construct a naive UTC datetime from a POSIX timestamp.
348+
"""
349+
)
350+
fromtimestamp = _make_error_func('fromtimestamp', # noqa:E128
351+
"""
352+
Timestamp.fromtimestamp(ts)
353+
354+
timestamp[, tz] -> tz's local time from POSIX timestamp.
355+
"""
356+
)
357+
combine = _make_error_func('combine', # noqa:E128
358+
"""
359+
Timsetamp.combine(date, time)
360+
361+
date, time -> datetime with same date and time fields
362+
"""
363+
)
364+
utcnow = _make_error_func('utcnow', # noqa:E128
365+
"""
366+
Timestamp.utcnow()
367+
368+
Return a new Timestamp representing UTC day and time.
369+
"""
370+
)
371+
349372
timestamp = _make_error_func('timestamp', # noqa:E128
350373
"""Return POSIX timestamp as float.""")
351374

@@ -372,6 +395,8 @@ class NaTType(_NaT):
372395
""")
373396
fromordinal = _make_error_func('fromordinal', # noqa:E128
374397
"""
398+
Timestamp.fromordinal(ordinal, freq=None, tz=None, offset=None)
399+
375400
passed an ordinal, translate and convert to a ts
376401
note: by definition there cannot be any tz info on the ordinal itself
377402
@@ -397,8 +422,10 @@ class NaTType(_NaT):
397422

398423
now = _make_nat_func('now', # noqa:E128
399424
"""
400-
Return the current time in the local timezone. Equivalent
401-
to datetime.now([tz])
425+
Timestamp.now(tz=None)
426+
427+
Returns new Timestamp object representing current time local to
428+
tz.
402429
403430
Parameters
404431
----------
@@ -407,6 +434,8 @@ class NaTType(_NaT):
407434
""")
408435
today = _make_nat_func('today', # noqa:E128
409436
"""
437+
Timestamp.today(cls, tz=None)
438+
410439
Return the current time in the local timezone. This differs
411440
from datetime.today() in that it can be localized to a
412441
passed timezone.

pandas/core/categorical.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ class Categorical(PandasObject):
194194
195195
.. versionadded:: 0.21.0
196196
197+
Methods
198+
-------
199+
from_codes
200+
__array__
201+
197202
Raises
198203
------
199204
ValueError
@@ -401,7 +406,7 @@ def ordered(self):
401406

402407
@property
403408
def dtype(self):
404-
"""The :ref:`~pandas.api.types.CategoricalDtype` for this instance"""
409+
"""The :class:`~pandas.api.types.CategoricalDtype` for this instance"""
405410
return self._dtype
406411

407412
@property

pandas/core/dtypes/dtypes.py

+9
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ class CategoricalDtype(ExtensionDtype):
120120
Must be unique, and must not contain any nulls.
121121
ordered : bool, default False
122122
123+
Attributes
124+
----------
125+
categories
126+
ordered
127+
128+
Methods
129+
-------
130+
None
131+
123132
Notes
124133
-----
125134
This class is useful for specifying the type of a ``Categorical``

pandas/core/indexes/category.py

+18
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ class CategoricalIndex(Index, accessor.PandasDelegate):
4646
name : object
4747
Name to be stored in the index
4848
49+
Attributes
50+
----------
51+
codes
52+
categories
53+
ordered
54+
55+
Methods
56+
-------
57+
rename_categories
58+
reorder_categories
59+
add_categories
60+
remove_categories
61+
remove_unused_categories
62+
set_categories
63+
as_ordered
64+
as_unordered
65+
map
66+
4967
See Also
5068
--------
5169
Categorical, Index

pandas/core/indexes/datetimes.py

+48
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,54 @@ class DatetimeIndex(DatelikeOps, TimelikeOps, DatetimeIndexOpsMixin,
203203
name : object
204204
Name to be stored in the index
205205
206+
Attributes
207+
----------
208+
year
209+
month
210+
day
211+
hour
212+
minute
213+
second
214+
microsecond
215+
nanosecond
216+
date
217+
time
218+
dayofyear
219+
weekofyear
220+
week
221+
dayofweek
222+
weekday
223+
weekday_name
224+
quarter
225+
tz
226+
freq
227+
freqstr
228+
is_month_start
229+
is_month_end
230+
is_quarter_start
231+
is_quarter_end
232+
is_year_start
233+
is_year_end
234+
is_leap_year
235+
inferred_freq
236+
237+
Methods
238+
-------
239+
normalize
240+
strftime
241+
snap
242+
tz_convert
243+
tz_localize
244+
round
245+
floor
246+
ceil
247+
to_datetime
248+
to_period
249+
to_perioddelta
250+
to_pydatetime
251+
to_series
252+
to_frame
253+
206254
Notes
207255
-----
208256
To learn more about the frequency strings, please see `this link

pandas/core/indexes/interval.py

+11
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ class IntervalIndex(IntervalMixin, Index):
121121
Name to be stored in the index.
122122
copy : boolean, default False
123123
Copy the meta-data
124+
mid
125+
values
126+
is_non_overlapping_monotonic
127+
128+
Methods
129+
-------
130+
from_arrays
131+
from_tuples
132+
from_breaks
133+
from_intervals
134+
contains
124135
125136
Examples
126137
---------

pandas/core/indexes/multi.py

+26
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,30 @@ class MultiIndex(Index):
9595
of iterables
9696
MultiIndex.from_tuples : Convert list of tuples to a MultiIndex
9797
Index : The base pandas Index type
98+
99+
Attributes
100+
----------
101+
names
102+
levels
103+
labels
104+
nlevels
105+
levshape
106+
107+
Methods
108+
-------
109+
from_arrays
110+
from_tuples
111+
from_product
112+
set_levels
113+
set_labels
114+
to_hierarchical
115+
to_frame
116+
is_lexsorted
117+
sortlevel
118+
droplevel
119+
swaplevel
120+
reorder_levels
121+
remove_unused_levels
98122
"""
99123

100124
# initialize to zero-length tuples to make everything work
@@ -1362,10 +1386,12 @@ def remove_unused_levels(self):
13621386

13631387
@property
13641388
def nlevels(self):
1389+
"""Integer number of levels in this MultiIndex."""
13651390
return len(self.levels)
13661391

13671392
@property
13681393
def levshape(self):
1394+
"""A tuple with the length of each level."""
13691395
return tuple(len(x) for x in self.levels)
13701396

13711397
def __reduce__(self):

0 commit comments

Comments
 (0)