Skip to content

Week Start/End Resamping: WS/WE frequencies #15449

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 1 commit into from

Conversation

aiguofer
Copy link
Contributor

@aiguofer aiguofer commented Feb 17, 2017

This is just some initial work to start the conversation.

@aiguofer
Copy link
Contributor Author

aiguofer commented Feb 17, 2017

With the above changes, the alias works for my original scenario:

import pandas as pd
df = pd.DataFrame(pd.date_range('2016-02-01', '2016-02-29').rename('dt'))
df['val'] = 1
df.groupby(pd.Grouper(key='dt',freq='WS'))['val'].sum()

dt
2016-02-01    7
2016-02-08    7
2016-02-15    7
2016-02-22    7
2016-02-29    1
Freq: WS-MON, Name: val, dtype: int64

as well as OP's:

import pandas as pd
import datetime as dt

daily_daterange = pd.date_range(start=dt.datetime(2013, 1, 31),
                                end=dt.datetime(2013,2,28),
                                freq='B')

daily_dataset = pd.DataFrame({'value' : range(len(daily_daterange))}, index=daily_daterange)

# week begin bound to monday
daily_dataset.resample('WS-MON').mean()
Out[460]: 
            value
2013-01-28    0.5
2013-02-04    4.0
2013-02-11    9.0
2013-02-18   14.0
2013-02-25   18.5

# week end bound to sunday
daily_dataset.resample('W-SUN').mean()
Out[461]: 
            value
2013-02-03    0.5
2013-02-10    4.0
2013-02-17    9.0
2013-02-24   14.0
2013-03-03   18.5

However, it raises some questions:

  • Should there be a WeekEnd and WeekBegin offset? if so, how do we keep API compatibility with the existing Week?
  • Would it be confusing to have WeekEnd (would people think it refers to weekends)?
  • If there is a WeekEnd and WeekBegin it seems like they would always require a weekday so that we can bind to beginning or end, would the following defaults make sense? WeekEnd.weekday = 6 and WeekBegin.weekday = 0?
  • Right now, if you do pd.Timestamp('2017-02-17') + pd.offsets.Week() it adds a week (7 days). There is no equivalent for Months or Quarters for example. It seems like it would make sense to have Week, WeekEnd and WeekBegin, but I'm not sure if that would cause confusion.
  • How often are people using aliases vs offsets?

@jreback jreback added the Frequency DateOffsets label Feb 18, 2017
@chris-b1
Copy link
Contributor

Some initial thoughts:

Should there be a WeekEnd and WeekBegin offset? if so, how do we keep API compatibility with the >existing Week?

As you note - Week is currently playing two roles - Week() is a fixed 7 day offset, Week(weekday=3) is an anchored offset. So we could deprecate the latter, in favor of WeekEnd(weekday=3).

Would it be confusing to have WeekEnd (would people think it refers to weekends)?

Not ideal, but not sure if there's a better name.

How often are people using aliases vs offsets?

Aliases are likely used more, for ranges and resampling, but at least personally, I use offsets fairly frequently for shifting dates.

df['date_plus_1yr'] = df['date'] + pd.DateOffset(years=1)

@aiguofer
Copy link
Contributor Author

I wonder how often people use something like:

df['date_plus_1wk'] = df['date'] + pd.offsets.Week()

It seems like Week is the only DateOffset that offers a 'fixed' offset, but there are no Year or Month fixed offsets. Would we want to add those as well? (although I don't see much benefit from those when you could do any of that directly with DateOffset). I think the anchored functionality is more interesting, so WeekEnd and WeekBegin might work well; and I like the idea of deprecating the 'fixed' use of Week.

@jreback
Copy link
Contributor

jreback commented Apr 18, 2017

@aiguofer love to have this, but will need quite a bit of testing.

@aiguofer
Copy link
Contributor Author

Yeah... I haven't had time to get back to this. I'd really like to follow the Boy Scout Rule, but indeed that probably means changing some things, and writing lots of tests.

Hopefully I'll have some time to get back to this in the coming weeks.

@jreback
Copy link
Contributor

jreback commented May 13, 2017

closing as stale

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Frequency DateOffsets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Week Start/End Resamping: WS/WE frequencies
3 participants