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

calendar_plot() showing week number = 53 in the last week of January 2021 #19

Closed
xriva opened this issue Jul 5, 2021 · 5 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@xriva
Copy link

xriva commented Jul 5, 2021

Seems like a bug?

@e-hulten
Copy link
Owner

e-hulten commented Jul 5, 2021

Hi, thanks for reaching out!

You are absolutely right. I have found the bug and will release a new micro version (v0.1.3) tonight after work.

@e-hulten e-hulten added the bug Something isn't working label Jul 5, 2021
@e-hulten e-hulten self-assigned this Jul 5, 2021
e-hulten added a commit that referenced this issue Jul 5, 2021
- Add order preserving unique() function
- Improve input handling in preprocess_month()
  - Optional 'year' argument to avoid ambiguity
  - Improved error messages
- Remove ticklabels in month_plot when weeknum_label=False.
- Add optional 'year' argument to month_plot as well.
e-hulten added a commit that referenced this issue Jul 5, 2021
bug: Fix bug in calendar plot weeknum order (#19)
@e-hulten
Copy link
Owner

e-hulten commented Jul 5, 2021

I have now pushed the fix and uploaded the new version to PyPi as well. Try pip install july or pip install july==0.1.3 to get the latest version, and the bug should be gone.

Please don't hesitate to reach out if you encounter any other weird bugs, it is much appreciated.

Close #19

image

@e-hulten e-hulten closed this as completed Jul 5, 2021
@xriva
Copy link
Author

xriva commented Jul 6, 2021

Hi @e-hulten , I think week number should reset at each year? E.g. The first week of Jan 2021 might be showing as 53/1 in this case?

Otherwise, if you plot year 2021 alone, then the week number starts at 53, which is kind of counter intuitive.

@e-hulten
Copy link
Owner

e-hulten commented Jul 6, 2021

I agree that it seems a little counter-intuitive, but it deliberate and according to the ISO week date definition:

First week

The ISO 8601 definition for week 01 is the week with the first Thursday of the Gregorian year (i.e. of January) in it. The following definitions based on properties of this week are mutually equivalent, since the ISO week starts with Monday:

  • It is the first week with a majority (4 or more) of its days in January.
  • Its first day is the Monday nearest to 1 January.
  • It has 4 January in it. Hence the earliest possible first week extends from Monday 29 December (previous Gregorian year) to Sunday 4 January, the latest possible first week extends from Monday 4 January to Sunday 10 January.
  • It has the year's first working day in it, if Saturdays, Sundays and 1 January are not working days.
  • If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in W01. If it is on a Friday, it is part of W53 of the previous year. If it is on a Saturday, it is part of the last week of the previous year which is numbered W52 in a common year and W53 in a leap year. If it is on a Sunday, it is part of W52 of the previous year.

There are pros and cons with any definition, but to my knowledge this is the most widely used one. It is neat because all week numbers are uniquely defined, and all dates are uniquely defined by year, week number, and weekday number. I am currently not planning on changing this.

Workarounds

There are a couple of possible workarounds if you don't want 2021 starting with week 53. The first one is to set weeknum_label=False in calendar_plot() to remove the labels altogether:

import july
import numpy as np
import matplotlib.pyplot as plt 

dates = july.utils.date_range("2020-12-01", "2021-01-31")
data = np.random.randint(0, 20, len(dates))
july.calendar_plot(dates, data, ncols=2, weeknum_label=False)
plt.show()

image

The second option is to manually edit the yticklabels of the returned AxesSubplot object. This is an ok solution if you're only plotting a few months, but please note that you'd potentially have to edit the yticklabels of all the following months as well to avoid duplicate week number labels.

axes = july.calendar_plot(dates, data, ncols=2, weeknum_label=True)
axes[1].set_yticklabels([1, 2, 3, 4, 5, ""])
plt.show()

image

@xriva
Copy link
Author

xriva commented Jul 6, 2021

Thanks for the detailed explanations, didn't aware there's an ISO standard for that :-)
All good now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants