Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: document subclasses in API docs with selection of specific methods/attributes #18202

Merged
merged 34 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
500ab71
DOC: Remove numpydoc hack, list methods explicitly
TomAugspurger Nov 9, 2017
257fc8a
[WIP]: API doc
TomAugspurger Nov 9, 2017
d59c0b5
More explicit methods
TomAugspurger Nov 10, 2017
cb57504
More updates
TomAugspurger Nov 10, 2017
5e4cf6d
typos
TomAugspurger Nov 10, 2017
346a73b
Merge remote-tracking branch 'upstream/master' into api-doc-methods
TomAugspurger Nov 11, 2017
b5902ff
more
TomAugspurger Nov 12, 2017
af1e866
Merge remote-tracking branch 'upstream/master' into api-doc-methods
TomAugspurger Nov 12, 2017
874aefe
Add accessors to hidden
TomAugspurger Nov 12, 2017
32c2816
Remove dupe section
TomAugspurger Nov 12, 2017
38673bd
Added Categorical
TomAugspurger Nov 12, 2017
e30335e
More fixups
TomAugspurger Nov 13, 2017
38efdcb
More cleanups
TomAugspurger Nov 13, 2017
5e8bcbe
PEP8
TomAugspurger Nov 13, 2017
8695727
Hide more items
TomAugspurger Nov 13, 2017
4476a5f
Fixups
TomAugspurger Nov 13, 2017
a5005e1
Matching NaT
TomAugspurger Nov 13, 2017
aa92592
Fix typo
TomAugspurger Nov 13, 2017
8841035
Merge remote-tracking branch 'upstream/master' into api-doc-methods
TomAugspurger Nov 13, 2017
1527cdb
PEP8
TomAugspurger Nov 13, 2017
7ca3434
Fix ordering, escaping
TomAugspurger Nov 14, 2017
5a0e10e
Add notes on contributing
TomAugspurger Nov 14, 2017
c8504d1
Fix kwds again
TomAugspurger Nov 14, 2017
f7ed015
Wording
TomAugspurger Nov 14, 2017
094f6f6
Fix ref
TomAugspurger Nov 14, 2017
1c2160f
Another fixup
TomAugspurger Nov 14, 2017
6e99be2
Series of dtype category
TomAugspurger Nov 14, 2017
3bd03db
Move more methods to hidden
TomAugspurger Nov 14, 2017
ba257cc
Hide get_loc
TomAugspurger Nov 14, 2017
169e833
Merge remote-tracking branch 'upstream/master' into api-doc-methods
TomAugspurger Nov 14, 2017
f846713
Just hide attributes
TomAugspurger Nov 14, 2017
141107a
Cleanup hidden
TomAugspurger Nov 14, 2017
3a224be
remove some methods from api.rst
jorisvandenbossche Nov 15, 2017
34834fa
fix warning for float64index
jorisvandenbossche Nov 15, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
340 changes: 274 additions & 66 deletions doc/source/api.rst

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
'sphinx.ext.doctest',
'sphinx.ext.extlinks',
'sphinx.ext.todo',
'numpydoc', # used to parse numpy-style docstrings for autodoc
'numpydoc',
'IPython.sphinxext.ipython_directive',
'IPython.sphinxext.ipython_console_highlighting',
'sphinx.ext.intersphinx',
Expand Down
21 changes: 21 additions & 0 deletions doc/source/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,27 @@ Some other important things to know about the docs:
doc build. This approach means that code examples will always be up to date,
but it does make the doc building a bit more complex.

- Our API documentation in ``doc/source/api.rst`` houses the auto-generated
documentation from the docstrings. For classes, there are a few subtleties
around controlling which methods and attributes have pages auto-generated.

We have two autosummary templates for classes.

1. ``_templates/autosummary/class.rst``. Use this when you want to
automatically generate a page for every public method and attribute on the
class. The ``Attributes`` and ``Methods`` sections will be automatically
added to the class' rendered documentation by numpydoc. See ``DataFrame``
for an example.

2. ``_templates/autosummary/class_without_autosummary``. Use this when you
want to pick a subset of methods / attributes to auto-generate pages for.
When using this template, you should include an ``Attributes`` and
``Methods`` section in the class docstring. See ``CategoricalIndex`` for an
example.

Every method should be included in a ``toctree`` in ``api.rst``, else Sphinx
will emit a warning.

.. note::

The ``.rst`` files are used to automatically generate Markdown and HTML versions
Expand Down
7 changes: 0 additions & 7 deletions doc/sphinxext/numpydoc/numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ def mangle_docstrings(app, what, name, obj, options, lines,
class_members_toctree=app.config.numpydoc_class_members_toctree,
)

# PANDAS HACK (to remove the list of methods/attributes for Categorical)
no_autosummary = [".Categorical", "CategoricalIndex", "IntervalIndex",
"RangeIndex", "Int64Index", "UInt64Index",
"Float64Index", "PeriodIndex", "CategoricalDtype"]
if what == "class" and any(name.endswith(n) for n in no_autosummary):
cfg['class_members_list'] = False

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is also some other code related to this hack that then could be removed (eg the place where I define the 'class_members_list' config). See cf40991 that added it

if what == 'module':
# Strip top title
title_re = re.compile(sixu('^\\s*[#*=]{4,}\\n[a-z0-9 -]+\\n[#*=]{4,}\\s*'),
Expand Down
30 changes: 28 additions & 2 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ class Timestamp(_Timestamp):
@classmethod
def fromordinal(cls, ordinal, freq=None, tz=None, offset=None):
"""
Timestamp.fromordinal(ordinal, freq=None, tz=None, offset=None)

passed an ordinal, translate and convert to a ts
note: by definition there cannot be any tz info on the ordinal itself

Expand All @@ -302,8 +304,10 @@ class Timestamp(_Timestamp):
@classmethod
def now(cls, tz=None):
"""
Return the current time in the local timezone. Equivalent
to datetime.now([tz])
Timestamp.now(tz=None)

Returns new Timestamp object representing current time local to
tz.

Parameters
----------
Expand All @@ -317,6 +321,8 @@ class Timestamp(_Timestamp):
@classmethod
def today(cls, tz=None):
"""
Timestamp.today(cls, tz=None)

Return the current time in the local timezone. This differs
from datetime.today() in that it can be localized to a
passed timezone.
Expand All @@ -330,18 +336,38 @@ class Timestamp(_Timestamp):

@classmethod
def utcnow(cls):
"""
Timestamp.utcnow()

Return a new Timestamp representing UTC day and time.
"""
return cls.now('UTC')

@classmethod
def utcfromtimestamp(cls, ts):
"""
Timestamp.utcfromtimestamp(ts)

Construct a naive UTC datetime from a POSIX timestamp.
"""
return cls(datetime.utcfromtimestamp(ts))

@classmethod
def fromtimestamp(cls, ts):
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sp?

Timestamp.fromtimestamp(ts)

timestamp[, tz] -> tz's local time from POSIX timestamp.
"""
return cls(datetime.fromtimestamp(ts))

@classmethod
def combine(cls, date, time):
"""
Timsetamp.combine(date, time)

date, time -> datetime with same date and time fields
"""
return cls(datetime.combine(date, time))

def __new__(cls, object ts_input=_no_input,
Expand Down
45 changes: 37 additions & 8 deletions pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -336,16 +336,39 @@ class NaTType(_NaT):
tzname = _make_error_func('tzname', datetime)
utcoffset = _make_error_func('utcoffset', datetime)

# Timestamp has empty docstring for some methods.
utcfromtimestamp = _make_error_func('utcfromtimestamp', None)
fromtimestamp = _make_error_func('fromtimestamp', None)
combine = _make_error_func('combine', None)
utcnow = _make_error_func('utcnow', None)

# ----------------------------------------------------------------------
# The remaining methods have docstrings copy/pasted from the analogous
# Timestamp methods.

utcfromtimestamp = _make_error_func('utcfromtimestamp', # noqa:E128
"""
Timestamp.utcfromtimestamp(ts)

Construct a naive UTC datetime from a POSIX timestamp.
"""
)
fromtimestamp = _make_error_func('fromtimestamp', # noqa:E128
"""
Timestamp.fromtimestamp(ts)

timestamp[, tz] -> tz's local time from POSIX timestamp.
"""
)
combine = _make_error_func('combine', # noqa:E128
"""
Timsetamp.combine(date, time)

date, time -> datetime with same date and time fields
"""
)
utcnow = _make_error_func('utcnow', # noqa:E128
"""
Timestamp.utcnow()

Return a new Timestamp representing UTC day and time.
"""
)

timestamp = _make_error_func('timestamp', # noqa:E128
"""Return POSIX timestamp as float.""")

Expand All @@ -372,6 +395,8 @@ class NaTType(_NaT):
""")
fromordinal = _make_error_func('fromordinal', # noqa:E128
"""
Timestamp.fromordinal(ordinal, freq=None, tz=None, offset=None)

passed an ordinal, translate and convert to a ts
note: by definition there cannot be any tz info on the ordinal itself

Expand All @@ -397,8 +422,10 @@ class NaTType(_NaT):

now = _make_nat_func('now', # noqa:E128
"""
Return the current time in the local timezone. Equivalent
to datetime.now([tz])
Timestamp.now(tz=None)

Returns new Timestamp object representing current time local to
tz.

Parameters
----------
Expand All @@ -407,6 +434,8 @@ class NaTType(_NaT):
""")
today = _make_nat_func('today', # noqa:E128
"""
Timestamp.today(cls, tz=None)

Return the current time in the local timezone. This differs
from datetime.today() in that it can be localized to a
passed timezone.
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ class Categorical(PandasObject):

.. versionadded:: 0.21.0

Methods
-------
from_codes
__array__

Raises
------
ValueError
Expand Down Expand Up @@ -401,7 +406,7 @@ def ordered(self):

@property
def dtype(self):
"""The :ref:`~pandas.api.types.CategoricalDtype` for this instance"""
"""The :class:`~pandas.api.types.CategoricalDtype` for this instance"""
return self._dtype

@property
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ class CategoricalDtype(ExtensionDtype):
Must be unique, and must not contain any nulls.
ordered : bool, default False

Attributes
----------
categories
ordered

Methods
-------
None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the problem that if you don't include this 'None', that it then automatically generated the 'Methods' section and thus included too much?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm not sure how best to avoid that. This is where class-level numpydoc_show_class_members flag would be nice. Having None there isn't great since it shows up in the output.


Notes
-----
This class is useful for specifying the type of a ``Categorical``
Expand Down
18 changes: 18 additions & 0 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ class CategoricalIndex(Index, accessor.PandasDelegate):
name : object
Name to be stored in the index

Attributes
----------
codes
categories
ordered

Methods
-------
rename_categories
reorder_categories
add_categories
remove_categories
remove_unused_categories
set_categories
as_ordered
as_unordered
map

See Also
--------
Categorical, Index
Expand Down
48 changes: 48 additions & 0 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,54 @@ class DatetimeIndex(DatelikeOps, TimelikeOps, DatetimeIndexOpsMixin,
name : object
Name to be stored in the index

Attributes
----------
year
month
day
hour
minute
second
microsecond
nanosecond
date
time
dayofyear
weekofyear
week
dayofweek
weekday
weekday_name
quarter
tz
freq
freqstr
is_month_start
is_month_end
is_quarter_start
is_quarter_end
is_year_start
is_year_end
is_leap_year
inferred_freq

Methods
-------
normalize
strftime
snap
tz_convert
tz_localize
round
floor
ceil
to_datetime
to_period
to_perioddelta
to_pydatetime
to_series
to_frame

Notes
-----
To learn more about the frequency strings, please see `this link
Expand Down
11 changes: 11 additions & 0 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ class IntervalIndex(IntervalMixin, Index):
Name to be stored in the index.
copy : boolean, default False
Copy the meta-data
mid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, if it's just the attribute name the attribute docstring is filled in. But currently that's buggy because @cache_readonly doesn't copy of the docstring.

values
is_non_overlapping_monotonic

Methods
-------
from_arrays
from_tuples
from_breaks
from_intervals
contains

Examples
---------
Expand Down
26 changes: 26 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ class MultiIndex(Index):
of iterables
MultiIndex.from_tuples : Convert list of tuples to a MultiIndex
Index : The base pandas Index type

Attributes
----------
names
levels
labels
nlevels
levshape

Methods
-------
from_arrays
from_tuples
from_product
set_levels
set_labels
to_hierarchical
to_frame
is_lexsorted
sortlevel
droplevel
swaplevel
reorder_levels
remove_unused_levels
"""

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

@property
def nlevels(self):
"""Integer number of levels in this MultiIndex."""
return len(self.levels)

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

def __reduce__(self):
Expand Down
Loading