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: Add BusinessHour offset #7905

Merged
merged 1 commit into from
May 5, 2015
Merged

Conversation

sinhrks
Copy link
Member

@sinhrks sinhrks commented Aug 2, 2014

Closes #2469. Create BusinessHour offset to specify business hours on BusinessDay. Appreciated if any feedbacks regarding bahaviours, keywords, etc.

Basic

By default, use 9:00 - 17:00 as business hours. Adding BusinessHour will increment timestamp by hourly on the days belong to BusinessDay offset.

bh = pd.offsets.BusinessHour()
repr(bh)
# <BusinessHour: BH=09:00-17:00>

pd.Timestamp('2014-08-01 10:00') + bh
#2014-08-01 11:00:00
pd.Timestamp('2014-08-01 08:00') + bh
#2014-08-01 10:00:00

# move to next Business Day
pd.Timestamp('2014-08-01 19:00') + bh
#2014-08-04 10:00:00

# if exceeds the closing time, remaining are added to next day
pd.Timestamp('2014-08-01 16:45') + bh
#2014-08-04 09:45:00

# if n != 1
pd.Timestamp('2014-08-01 10:00') + pd.offsets.BusinessHour(2)
#2014-08-01 12:00:00

pd.Timestamp('2014-08-01 10:00') + pd.offsets.BusinessHour(-3)
#2014-07-31 15:00:00

# Create Index
pd.date_range('2014-07-08 10:00', freq='BH', periods=40)
# [2014-07-08 10:00:00, ..., 2014-07-14 17:00:00]
# Length: 40, Freq: BH, Timezone: None

Specify Opening/Closing Hour

Allow to specify opening/closing time using start and end keyword by hour:minute string or datetime.time.

bh = pd.offsets.BusinessHour(start='11:00', end=datetime.time(20, 0))
repr(bh)
# <BusinessHour: BH=11:00-20:00>

pd.Timestamp('2014-08-01 10:00') + bh
#2014-08-01 12:00:00
pd.Timestamp('2014-08-01 13:00') + bh
#2014-08-01 14:00:00
pd.Timestamp('2014-08-01 19:00') + bh
#2014-08-04 11:00:00

# if end < start, it will be midnight business hour
bh = pd.offsets.BusinessHour(start='17:00', end='9:00')
repr(bh)
# <BusinessHour: BH=17:00-09:00>

pd.Timestamp('2014-07-31 10:00') + bh
#2014-07-31 18:00:00
pd.Timestamp('2014-07-31 23:00') + bh
#2014-08-01 00:00:00
pd.Timestamp('2014-07-31 01:00') + bh
#2014-07-31 02:00:00
pd.Timestamp('2014-08-01 13:00') + bh
#2014-08-01 18:00:00

Edge cases

onOffset should include both edges.

pd.offsets.BusinessHour().onOffset(pd.Timestamp('2014-08-01 09:00'))
# True

pd.offsets.BusinessHour().onOffset(pd.Timestamp('2014-08-01 17:00'))
# True

If result is on the end-edge of business hour, move to next

# not 2014-08-01 17:00
pd.Timestamp('2014-08-01 16:00') + pd.offsets.BusinessHour()
#2014-08-04 09:00

# not 2014-08-01 09:00
pd.Timestamp('2014-08-01 10:00') - pd.offsets.BusinessHour()
#2014-07:31 17:00

In case of midnight business hour, distinguish the date by its opening hour

#2014-08-02 is Saturday, but handled as valid because its business hour starts on 08-01
pd.Timestamp('2014-08-02 01:00') + pd.offsets.BusinessHour(start='17:00', end='9:00')
#2014-08:02 02:00

Remainings:

  • doc
  • impl & tests
    • Index creation
      • Confirm better conditions whether inferred_freq results in BH or H
    • normalize
    • tsplot (This can't be done without ENH: Cleanup backend for Offsets and Period #5148, skipped)
    • Correct edge handling for rollback and rollforward
    • handling nanosecond (minor issue, but current apply_wraps may add preserved nanosecond after the closing time)
  • tests
    • n larger than business shours
    • Specifying start and end
    • midnight business hours
    • Error cases for initialization
    • Better fix for test_apply_out_of_range timezone check, which fails because of DST difference.

@jreback
Copy link
Contributor

jreback commented Aug 3, 2014

why would you not add this as an optional feature of BusinessDay instead? (e.g. normally start=None,end=None, otherwise could be specified ?

@sinhrks
Copy link
Member Author

sinhrks commented Aug 3, 2014

Because units are different. A day in BDay, an hour in BusinessHour.

@jreback
Copy link
Contributor

jreback commented Aug 5, 2014

@sinhrks ok, makes sense

@sinhrks sinhrks changed the title (WIP) ENH: Add BusinessHour offset ENH: Add BusinessHour offset Aug 11, 2014
@sinhrks
Copy link
Member Author

sinhrks commented Aug 11, 2014

OK, I think this can be reviewed now.

Considering to simplify the internal process, but API has been fixed as far as I'm concerned.

@jreback
Copy link
Contributor

jreback commented Jan 18, 2015

@sinhrks can you rebase / revisit

@sinhrks
Copy link
Member Author

sinhrks commented Jan 19, 2015

Rebased.

@sinhrks sinhrks force-pushed the businesshour branch 2 times, most recently from c8d4772 to 04f7e9b Compare February 15, 2015 03:29
@jreback
Copy link
Contributor

jreback commented Apr 8, 2015

@sinhrks let's rebase again; this looked ok

@sinhrks sinhrks force-pushed the businesshour branch 3 times, most recently from 5cc605e to 962df63 Compare April 9, 2015 15:57
@sinhrks
Copy link
Member Author

sinhrks commented Apr 10, 2015

@jreback Thanks, rebased and now green.

@jreback jreback added this to the 0.16.1 milestone Apr 12, 2015
@jreback
Copy link
Contributor

jreback commented Apr 28, 2015

@sinhrks looks good.

@jorisvandenbossche want to take a quick look thru docs?

jreback added a commit that referenced this pull request May 5, 2015
@jreback jreback merged commit d92e5e3 into pandas-dev:master May 5, 2015
@jreback
Copy link
Contributor

jreback commented May 5, 2015

thanks @sinhrks very nice!

@sinhrks sinhrks deleted the businesshour branch May 6, 2015 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hourly Frequency Business Days
2 participants