@@ -329,6 +329,9 @@ The documentation of the return is also similar to the parameters. But in this
329329case, no name will be provided, unless the method returns or yields more than
330330one value (a tuple of values).
331331
332+ The types for "Returns" and "Yields" are the same as the ones for the
333+ "Parameters". Also, the description must finish with a dot.
334+
332335For example, with a single value:
333336
334337.. code-block :: python
@@ -367,3 +370,253 @@ With more than one value:
367370 letters = ' ' .join(random.choice(string.ascii_lowercase)
368371 for i in range (length))
369372 return length, letters
373+
374+ If the method yields its value:
375+
376+ .. code-block :: python
377+
378+ def sample_values ():
379+ """ Generate an infinite sequence of random numbers.
380+
381+ The values are sampled from a continuos uniform distribution between
382+ 0 and 1.
383+
384+ Yields
385+ ------
386+ float
387+ Random number generated.
388+ """
389+ while True :
390+ yield random.random()
391+
392+
393+ Section 5: See also
394+ ~~~~~~~~~~~~~~~~~~~
395+
396+ This is an optional section, used to let users know about pandas functionality
397+ related to the one being documented.
398+
399+ An obvious example would be the `head() ` and `tail() ` method. As `tail() ` does
400+ the equivalent as `head() ` but at the end of the `Series ` or `DataFrame `
401+ instead of at the beginning, it is good to let the users know about it.
402+
403+ To give an intuition on what can be considered related, here there are some
404+ examples:
405+
406+ * `loc ` and `iloc `, as they do the same, but in one case providing indices and
407+ int the other positions
408+ * `max ` and `min `, as they do the opposite
409+ * `iterrows `, `itertuples ` and `iteritems `, as it is easy that a user looking
410+ for the method to iterate over columns ends up in the method to iterate
411+ over rows, and vice-versa
412+ * `fillna ` and `dropna `, as both methods are used to handle missing values
413+ * `read_csv ` and `to_csv `, as they are complementary
414+ * `merge ` and `join `, as one is a generalization of the other
415+ * `astype ` and `pandas.to_datetime `, as users may be reading the documentation
416+ of `astype ` to know how to cast as a date, and the way to do it is with
417+ `pandas.to_datetime `
418+
419+ But when deciding what is related, you should mainly use your common sense and
420+ think about what can be useful for the users reading the documentation,
421+ especially the less experienced ones.
422+
423+ This section, as the previous, also has a header, "See Also" (note the capital
424+ S and A) in this case. Also followed by the line with hyphens, and preceded by
425+ a blank line.
426+
427+ After the header, we will add a line for each related method or function,
428+ followed by a space, a colon, another space, and a short description that
429+ illustrated what this method or function does, and why is it relevant in
430+ this context. The description must also finish with a dot.
431+
432+ Note that in "Returns" and "Yields", the description is located in the
433+ following line than the type. But in this section it is located in the same
434+ line, with a colon in between.
435+
436+ For example:
437+
438+ .. code-block :: python
439+
440+ class Series :
441+ def head (self ):
442+ """ Return the first 5 elements of the Series.
443+
444+ This function is mainly useful to preview the values of the
445+ Series without displaying the whole of it.
446+
447+ Return
448+ ------
449+ pandas.Series
450+ Subset of the original series with the 5 first values.
451+
452+ See Also
453+ --------
454+ tail : Return the last 5 elements of the Series.
455+ """
456+ return self .iloc[:5 ]
457+
458+ Section 6: Notes
459+ ~~~~~~~~~~~~~~~~
460+
461+ This is an optional section used for notes about the implementation of the
462+ algorithm.
463+
464+ Feel free to skip it, unless you are familiar with the implementation of the
465+ algorithm, or you discover some counter-intuitive behavior while writing the
466+ examples for the function.
467+
468+ This section follows the same format as the extended summary section.
469+
470+ Section 7: Examples
471+ ~~~~~~~~~~~~~~~~~~~
472+
473+ This is one of the most important sections of a docstring, even if it is
474+ placed in the last position. As often, people understand concepts better
475+ with examples, than with accurate explanations.
476+
477+ Examples in docstrings are also unit tests, and besides illustrating the
478+ usage of the function or method, they need to be valid Python code, that in a
479+ deterministic way returns the presented output.
480+
481+ They are presented as a session in the Python terminal. `>>> ` is used to
482+ present code. `... ` is used for code continuing from the previous line.
483+ Output is presented immediately after the last line of code generating the
484+ output (no blank lines in between). Comments describing the examples can
485+ be added with blank lines before and after them.
486+
487+ The way to present examples is as follows:
488+ 1. Create the data required to demostrate the usage
489+ 2. Show a very basic example that gives an idea of the most common use case
490+ 3. Add examples that illustrate how the parameters can be used
491+
492+ A simple example could be:
493+
494+ .. code-block :: python
495+
496+ class Series :
497+ def head (self , n = 5 ):
498+ """ Return the first elements of the Series.
499+
500+ This function is mainly useful to preview the values of the
501+ Series without displaying the whole of it.
502+
503+ Parameters
504+ ----------
505+ n : int
506+ Number of values to return.
507+
508+ Return
509+ ------
510+ pandas.Series
511+ Subset of the original series with the n first values.
512+
513+ See Also
514+ --------
515+ tail : Return the last n elements of the Series.
516+
517+ Examples
518+ --------
519+ >>> import pandas
520+ >>> s = pandas.Series(['Ant', 'Bear', 'Cow', 'Dog', 'Falcon',
521+ ... 'Lion', 'Monkey', 'Rabbit', 'Zebra'])
522+ >>> s.head()
523+ 0 Ant
524+ 1 Bear
525+ 2 Cow
526+ 3 Dog
527+ 4 Falcon
528+ dtype: object
529+
530+ With the `n` parameter, we can change the number of returned rows:
531+
532+ >>> s.head(n=3)
533+ 0 Ant
534+ 1 Bear
535+ 2 Cow
536+ dtype: object
537+ """
538+ return self .iloc[:n]
539+
540+ Conventions for the examples
541+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
542+
543+ .. note ::
544+ numpydoc recommends avoiding "obvious" imports and importing them with
545+ aliases, so for example `import numpy as np `. While this is now an standard
546+ in the data ecosystem of Python, it doesn't seem a good practise, for the
547+ next reasons:
548+
549+ * The code is not executable anymore (as doctests for example)
550+
551+ * New users not familiar with the convention can't simply copy and run it
552+
553+ * Users may use aliases (even if it is a bad Python practise except
554+ in rare cases), but if maintainers want to use `pd ` instead of `pandas `,
555+ why do not name the module `pd ` directly?
556+
557+ * As this is becoming more standard, there are an increasing number of
558+ aliases in scientific Python code, including `np `, `pd `, `plt `, `sp `,
559+ `pm `... which makes reading code harder
560+
561+ All examples must start with the required imports, one per line (as
562+ recommended in `PEP-8 <https://www.python.org/dev/peps/pep-0008/#imports >`_)
563+ and avoiding aliases. Avoid excessive imports, but if needed, imports from
564+ the standard library go first, followed by third-party libraries (like
565+ numpy) and importing pandas in the last place.
566+
567+ When illustrating examples with a single `Series ` use the name `s `, and if
568+ illustrating with a single `DataFrame ` use the name `df `. If a set of
569+ homogeneous `Series ` or `DataFrame ` is used, name them `s1 `, `s2 `, `s3 `...
570+ or `df1 `, `df2 `, `df3 `... If the data is not homogeneous, and more than
571+ one structure is needed, name them with something meaningful, for example
572+ `df_main ` and `df_to_join `.
573+
574+ Data used in the example should be as compact as possible. The number of rows
575+ is recommended to be 4, unless the example requires a larger number. As for
576+ example in the head method, where it requires to be higher than 5, to show
577+ the example with the default values.
578+
579+ Avoid using data without interpretation, like a matrix of random numbers
580+ with columns A, B, C, D... And instead use a meaningful example, which makes
581+ it easier to understand the concept. Unless required by the example, use
582+ names of animals, to keep examples consistent. And numerical properties of
583+ them.
584+
585+ **Wrong: **
586+
587+ .. code-block :: python
588+
589+ def method ():
590+ """ A sample DataFrame method.
591+
592+ Examples
593+ --------
594+ >>> import numpy
595+ >>> import pandas
596+ >>> df = pandas.DataFrame(numpy.random.randn(3, 3),
597+ ... columns=('a', 'b', 'c'))
598+ """
599+ pass
600+
601+ **Good: **
602+
603+ .. code-block :: python
604+
605+ def method ():
606+ """ A sample DataFrame method.
607+
608+ Examples
609+ --------
610+ >>> import numpy
611+ >>> import pandas
612+ >>> df = pandas.DataFrame([389., 24., 80.5, numpy.nan]
613+ ... columns=('max_speed'),
614+ ... index=['falcon', 'parrot', 'lion', 'monkey'])
615+ """
616+ pass
617+
618+ Once you finished the docstring
619+ -------------------------------
620+
621+ When you finished the changes to the docstring, go to the
622+ :ref: `instructions to submit your changes <pandas_pr >` to continue.
0 commit comments