@@ -82,6 +82,9 @@ about reStructuredText can be found in:
8282- `Quick reStructuredText reference <http://docutils.sourceforge.net/docs/user/rst/quickref.html >`_
8383- `Full reStructuredText specification <http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html >`_
8484
85+ Pandas has some helpers for sharing docstrings between related classes, see
86+ :ref: `docstring.sharing `.
87+
8588The rest of this document will summarize all the above guides, and will
8689provide additional convention specific to the pandas project.
8790
@@ -916,3 +919,79 @@ plot will be generated automatically when building the documentation.
916919 >>> s.plot()
917920 """
918921 pass
922+
923+ .. _docstring.sharing :
924+
925+ Sharing Docstrings
926+ ------------------
927+
928+ Pandas has a system for sharing docstrings, with slight variations, between
929+ classes. This helps us keep docstrings consistent, while keeping things clear
930+ for the user reading. It comes at the cost of some complexity when writing.
931+
932+ Each shared docstring will have a base template with variables, like
933+ ``%(klass)s ``. The variables filled in later on using the ``Substitution ``
934+ decorator. Finally, docstrings can be appended to with the ``Appender ``
935+ decorator.
936+
937+ In this example, we'll create a parent docstring normally (this is like
938+ ``pandas.core.generic.NDFrame ``. Then we'll have two children (like
939+ ``pandas.core.series.Series `` and ``pandas.core.frame.DataFrame ``). We'll
940+ substitute the children's class names in this docstring.
941+
942+ .. code-block :: python
943+
944+ class Parent :
945+ def my_function (self ):
946+ """ Apply my function to %(klass)s."""
947+ ...
948+
949+ class ChildA (Parent ):
950+ @Substitution (klass = " ChildA" )
951+ @Appender (Parent.my_function.__doc__ )
952+ def my_function (self ):
953+ ...
954+
955+ class ChildB (Parent ):
956+ @Substitution (klass = " ChildB" )
957+ @Appender (Parent.my_function.__doc__ )
958+ def my_function (self ):
959+ ...
960+
961+ The resulting docstrings are
962+
963+ .. code-block :: python
964+
965+ >> > print (Parent.my_function.__doc__ )
966+ Apply my function to % (klass)s.
967+ >> > print (ChildA.my_function.__doc__ )
968+ Apply my function to ChildA.
969+ >> > print (ChildB.my_function.__doc__ )
970+ Apply my function to ChildB.
971+
972+ Notice two things:
973+
974+ 1. We "append" the parent docstring to the children docstrings, which are
975+ initially empty.
976+ 2. Python decorators are applied inside out. So the order is Append then
977+ Substitution, even though Substitution comes first in the file.
978+
979+ Our files will often contain a module-level ``_shared_doc_kwargs `` with some
980+ common substitution values (things like ``klass ``, ``axes ``, etc).
981+
982+ You can substitute and append in one shot with something like
983+
984+ .. code-block :: python
985+
986+ @Appender (template % _shared_doc_kwargs)
987+ def my_function (self ):
988+ ...
989+
990+ where ``template `` may come from a module-level ``_shared_docs `` dictionary
991+ mapping function names to docstrings. Wherever possible, we prefer using
992+ ``Appender `` and ``Substitution ``, since the docstring-writing processes is
993+ slightly closer to normal.
994+
995+ See ``pandas.core.generic.NDFrame.fillna `` for an example template, and
996+ ``pandas.core.series.Series.fillna `` and ``pandas.core.generic.frame.fillna ``
997+ for the filled versions.
0 commit comments