Skip to content

DOC: update the series.dt.weekofyear docstring #20218

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

Closed
wants to merge 10 commits into from
Closed

DOC: update the series.dt.weekofyear docstring #20218

wants to merge 10 commits into from

Conversation

koaning
Copy link

@koaning koaning commented Mar 10, 2018

Checklist for the pandas documentation sprint (ignore this if you are doing
an unrelated PR):

  • PR title is "DOC: update the docstring"
  • The validation script passes: scripts/validate_docstrings.py <your-function-or-method>
  • The PEP8 style check passes: git diff upstream/master -u -- "*.py" | flake8 --diff
  • The html version looks good: python doc/make.py --single <your-function-or-method>
  • It has been proofread on language by another sprint participant

Please include the output of the validation script below between the "```" ticks:

################################################################################
###################### Docstring (pandas.Series.dt.week)  ######################
################################################################################

The week ordinal of the year.

Return the week ordinal of the year. Note that there can be
counter intuitive edge cases around the yearchange. For example
the first of january could be in week 52 of the previous year.
Monday indicates the start of a new week.

See Also
--------
pandas.Series.dt.week : Identical method.
pandas.Series.dt.weekofyear : Identical method.

Returns
-------
ndarray of integers indicating week

Examples
--------
>>> dates = pd.date_range("2016-12-31", "2017-01-08", freq="D")
>>> s = pd.Series(dates)
>>> pd.DataFrame({'date':s, 'week':s.dt.week, 'day':s.dt.strftime("%A")})
        date  week        day
0 2016-12-31    52   Saturday
1 2017-01-01    52     Sunday
2 2017-01-02     1     Monday
3 2017-01-03     1    Tuesday
4 2017-01-04     1  Wednesday
5 2017-01-05     1   Thursday
6 2017-01-06     1     Friday
7 2017-01-07     1   Saturday
8 2017-01-08     1     Sunday

Note what `series.dt.week` and `series.dt.weekofyear` are the same.

>>> s.dt.week
0    52
1    52
2     1
3     1
4     1
5     1
6     1
7     1
8     1
dtype: int64
>>> s.dt.weekofyear
0    52
1    52
2     1
3     1
4     1
5     1
6     1
7     1
8     1
dtype: int64

################################################################################
################################## Validation ##################################
################################################################################

Docstring for "pandas.Series.dt.week" correct. :)

If the validation script still gives errors, but you think there is a good reason
to deviate in this case (and there are certainly such cases), please state this
explicitly.

@pep8speaks
Copy link

pep8speaks commented Mar 10, 2018

Hello @koaning! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on March 10, 2018 at 21:39 Hours UTC

@koaning
Copy link
Author

koaning commented Mar 10, 2018

Mhm. Strange, locally I didn't seem to have PEP8 issues. Will check!

@@ -1707,7 +1707,64 @@ def freq(self, value):
nanosecond = _field_accessor('nanosecond', 'ns',
"The nanoseconds of the datetime")
weekofyear = _field_accessor('weekofyear', 'woy',
"The week ordinal of the year")
"""
Copy link
Author

Choose a reason for hiding this comment

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

This is where E128 is still complaining a bit. I don't know what the nicest way is to resolve this since we're a bit unconventional with the _field_accessor accepting a docstring here. I could add tabs to the docstring, but it would make the code much less readable (as well cause ugly things to the docstring).

Copy link
Author

Choose a reason for hiding this comment

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

@TomAugspurger would love advice.

Copy link
Contributor

Choose a reason for hiding this comment

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

@koaning maybe just make a new variable called _weekofyear_doc and assign it to like

    weekofyear = _field_accessor('weekofyear', 'woy', _weekofyear_doc)

Copy link
Author

Choose a reason for hiding this comment

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

you'll get a lot of variables in that file, but that works for me. will make the change. thanks!

the first of january could be in week 52 of the previous year.
Monday indicates the start of a new week.

See Also
Copy link
Contributor

Choose a reason for hiding this comment

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

I think Returns goes before See Also

Copy link
Author

Choose a reason for hiding this comment

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

well spotted. fixed.

--------
>>> dates = pd.date_range("2016-12-31", "2017-01-08", freq="D")
>>> s = pd.Series(dates)
>>> pd.DataFrame({'date':s, 'week':s.dt.week, 'day':s.dt.strftime("%A")})
Copy link
Contributor

Choose a reason for hiding this comment

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

pep8: space after colons

Copy link
Author

Choose a reason for hiding this comment

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

oops. fixed in next commit.

@koaning
Copy link
Author

koaning commented Mar 10, 2018

@TomAugspurger

  • i made a variable for the docstring
  • i reordered the docstring -> see also is now below returns
  • i added a space after the colon #mybad
  • i confirmed the html still looks good on my end

you can see the new output of the validation script below:

################################################################################
###################### Docstring (pandas.Series.dt.week)  ######################
################################################################################

The week ordinal of the year.

Return the week ordinal of the year. Note that there can be
counter intuitive edge cases around the yearchange. For example
the first of january could be in week 52 of the previous year.
Monday indicates the start of a new week.

Returns
-------
ndarray of integers indicating the week number

See Also
--------
pandas.Series.dt.week : Identical method.
pandas.Series.dt.weekofyear : Identical method.

Examples
--------
>>> dates = pd.date_range("2016-12-31", "2017-01-08", freq="D")
>>> s = pd.Series(dates)
>>> pd.DataFrame({'dt': s, 'week': s.dt.week, 'day': s.dt.strftime("%A")})
          dt  week        day
0 2016-12-31    52   Saturday
1 2017-01-01    52     Sunday
2 2017-01-02     1     Monday
3 2017-01-03     1    Tuesday
4 2017-01-04     1  Wednesday
5 2017-01-05     1   Thursday
6 2017-01-06     1     Friday
7 2017-01-07     1   Saturday
8 2017-01-08     1     Sunday

Note what `series.dt.week` and `series.dt.weekofyear` are the same.

>>> s.dt.week
0    52
1    52
2     1
3     1
4     1
5     1
6     1
7     1
8     1
dtype: int64
>>> s.dt.weekofyear
0    52
1    52
2     1
3     1
4     1
5     1
6     1
7     1
8     1
dtype: int64

################################################################################
################################## Validation ##################################
################################################################################

Docstring for "pandas.Series.dt.week" correct. :)

@koaning
Copy link
Author

koaning commented Mar 10, 2018

@TomAugspurger should be good to go now.

Copy link
Contributor

@TomAugspurger TomAugspurger left a comment

Choose a reason for hiding this comment

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

Couple small ones I missed first time around. Ping me here or in chat when you're done.

@@ -1706,9 +1706,67 @@ def freq(self, value):
"The microseconds of the datetime")
nanosecond = _field_accessor('nanosecond', 'ns',
"The nanoseconds of the datetime")
weekofyear = _field_accessor('weekofyear', 'woy',
"The week ordinal of the year")
_weekofyear_doc = """
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah sorry could you add a trailing backslash to the end of this line? Then we don't have a blank line at the start of the docstring.

Copy link
Author

Choose a reason for hiding this comment

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

When I do that the validation script gives an error.

Docstring text (summary) should start in the line immediately after the opening quotes (not in the same line, or leaving a blank line in between)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, nvm you're good then.


Returns
-------
ndarray of integers indicating the week number
Copy link
Contributor

Choose a reason for hiding this comment

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

I thikn

days : ndarray
    integers indicating the week number

ndarray is the type, the rest is the ddescription.

Copy link
Author

Choose a reason for hiding this comment

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

Just to confirm, what you're suggesting is different than the docstring guide for the sprint.

Returns
-------
int
    The sum of `num1` and `num2`

example found here: https://python-sprints.github.io/pandas/guide/pandas_docstring.html

7 2017-01-07 1 Saturday
8 2017-01-08 1 Sunday

Note that `series.dt.week` and `series.dt.weekofyear` are the same.
Copy link
Contributor

Choose a reason for hiding this comment

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

series -> Series

Copy link
Author

Choose a reason for hiding this comment

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

done.

@jorisvandenbossche
Copy link
Member

General question @TomAugspurger : how do we deal with the fact this is docstring for both DatetimeIndex and Series.dt ?

@TomAugspurger
Copy link
Contributor

Good question. How about just noting that "This method is available on both Series with datetime values or DatetimeIndex" in the extended summary?

And then both in the example.

@koaning
Copy link
Author

koaning commented Mar 10, 2018

@TomAugspurger will add that.

@jorisvandenbossche picking up gitter chat here.

weekofyear = _field_accessor('weekofyear', 'woy', _ weekofyear_doc)
week = weekofyear

Do we want both properties to have the same docstring? If so, do we mind the verbose example that explains you could do both?

@TomAugspurger
Copy link
Contributor

I think same docstring is fine.

Return the week ordinal of the year. Note that there can be
counter intuitive edge cases around a year change. For example
the first of january could be in week 52 of the previous year.
Monday indicates the start of a new week. This method is available
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a link to wikipedia (I think this goes by common week of year)


Returns
-------
ndarry
Copy link
Contributor

Choose a reason for hiding this comment

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

this returns an Index

--------
>>> dates = pd.date_range("2016-12-31", "2017-01-08", freq="D")
>>> s = pd.Series(dates)
>>> pd.DataFrame({'dt': s, 'week': s.dt.week, 'day': s.dt.strftime("%A")})
Copy link
Contributor

Choose a reason for hiding this comment

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

use s.dt.day_name()

7 1
8 1
dtype: int64
>>> s.dt.weekofyear
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need to repeat them, use weekofyear one

@datapythonista
Copy link
Member

Closing as stale. Also, the code this PR is based on does not live there anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants