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

ENH: weekday of year, common behavior accessor #6936

Open
trailsquirrel opened this issue Apr 23, 2014 · 12 comments
Open

ENH: weekday of year, common behavior accessor #6936

trailsquirrel opened this issue Apr 23, 2014 · 12 comments
Labels
Datetime Datetime data dtype Enhancement

Comments

@trailsquirrel
Copy link

See below. Dates from the last week of December 2013 are interpreted as being from the first week of the year. Is this due to the fact December 30th was a Monday?

import pandas as pd

dt = '2013-12-30'
pd.to_datetime(dt, coerce=True).year, pd.to_datetime(dt, coerce=True).weekofyear
# I expected: (2013, 53) since this is day 364 and the start of the 53rd week
# Instead it returned: (2013, 1)

My current work-around is:

import pandas as pd
import numpy as np

dt = '2013-12-30'
doy = pd.to_datetime(dt, coerce=True).dayofyear
(np.floor(doy / 7) + 1).astype('int')
# Returns 53
pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.5.final.0
python-bits: 64
OS: Linux
OS-release: 3.11.0-15-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.13.1
Cython: 0.20.1
numpy: 1.8.1
scipy: 0.12.0
statsmodels: 0.4.2
IPython: 2.0.0
sphinx: 1.2.2
patsy: None
scikits.timeseries: None
dateutil: 1.5
pytz: 2012c
bottleneck: None
tables: 2.4.0
numexpr: 2.0.1
matplotlib: 1.2.1
openpyxl: 1.5.8
xlrd: 0.9.2
xlwt: 0.7.4
xlsxwriter: None
sqlalchemy: None
lxml: 3.2.0
bs4: 4.2.0
html5lib: 0.95-dev
bq: None
apiclient: None
@jreback
Copy link
Contributor

jreback commented Apr 23, 2014

related bug (not excactly dupe) of #6538

want to have a go at fixing?

@jreback jreback added the Bug label Apr 23, 2014
@jreback jreback added this to the 0.15.0 milestone Apr 23, 2014
@trailsquirrel
Copy link
Author

Do you know the reason for the code that exists under the 'woy' case? It seems overly complicated, but I may be missing a corner case where my simple solution doesn't work. Don't mind helping out, but want to make sure I understand the issue.

https://github.com/pydata/pandas/blob/master/pandas/tslib.pyx#L2255

@jreback
Copy link
Contributor

jreback commented Apr 23, 2014

their are some tests in pandas/tseries/tests/test_timeseries.py

I am not sure of the reason for the estimate then verify (though maybe its the leap year issue).

you can add in your posted case as a test, have it fail, then fix (and don't break other tests).

@trailsquirrel
Copy link
Author

So I fixed it and it broke the existing weekofyear test. After digging in, the existing code does conform to the ISO standard. Though it is not intuitive and leads to unexpected results when you try to do group bys using it.

Thoughts on adding a new DatetimeIndex attribute to help solve this? It may make sense to create an iso_weekofyear attribute from the existing to clearly distinguish them.

@jreback
Copy link
Contributor

jreback commented Apr 24, 2014

that makes sense

changing the name would require a deprecation cycle. maybe add as weekofyear_common?

open to adding

@jreback jreback added API Design and removed Bug labels May 5, 2014
@jreback jreback modified the milestones: 0.14.1, 0.15.0 May 5, 2014
@jreback jreback modified the milestones: 0.15.0, 0.14.1, 0.15.1 Jun 30, 2014
@jreback jreback modified the milestones: 0.15.1, 0.15.0 Sep 4, 2014
@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 6, 2015
@vitorcurtis
Copy link

Sorry about the question, but where I can find weekofyear_common?

@jreback
Copy link
Contributor

jreback commented Aug 9, 2017

see #7028, but was closed as stale (not finished), you are welcome to revive.

@jreback jreback changed the title Odd behavior from datetime objects and .weekofyear ENH: weekday of year, common behavior accessor Aug 9, 2017
@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Aug 9, 2017

@vitorcurtis This is an open issue, which means it is not yet implemented (update, yes see @jreback answer)

@jorisvandenbossche
Copy link
Member

BTW, when someone wants to implement this, important question that first should be answered is what would exactly be the definition of such a 'common week'.
ISO weeks are well defined, so we need an alternative definition, see also comment on the closed PR #7028 (comment)

@vitorcurtis
Copy link

I believe that more definitions are never a good path, so my opinion is to use the definition PR #7028.
Actually, we may be flexible.
We could alert of this ISO issue and also solve the problem by simply using one or parameters for the weekofyear, as:
weekofyear(iso=False, first_weekday='Monday')
weekofyear(iso=False, first_weekday='Thursday')
...
What do you think?

@SHxKM
Copy link

SHxKM commented Mar 9, 2020

Any progress with this?

@jreback
Copy link
Contributor

jreback commented Mar 9, 2020

Any progress with this?

you are welcome to submit a PR

@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants