1

I'm trying to define a range that begins at hour 2 rather than hour 0, but the following code returns all ranges beginning/ending at hour 0.

import pandas as pd
start_date = '2005-1-1 02:00:00'
end_date = '2005-5-1 02:00:00'

date_range = pd.bdate_range(start=start_date, end=end_date, freq='H')
print(date_range)

The output for the code above is the following, but I want it to begin at specific hour (here 02:00:00 rather than 00:00:00):

DatetimeIndex(['2005-01-01 00:00:00', '2005-01-01 01:00:00',
           '2005-01-01 02:00:00', '2005-01-01 03:00:00',
           '2005-01-01 04:00:00', '2005-01-01 05:00:00',
           '2005-01-01 06:00:00', '2005-01-01 07:00:00',
           '2005-01-01 08:00:00', '2005-01-01 09:00:00',
           ...
           '2005-04-30 15:00:00', '2005-04-30 16:00:00',
           '2005-04-30 17:00:00', '2005-04-30 18:00:00',
           '2005-04-30 19:00:00', '2005-04-30 20:00:00',
           '2005-04-30 21:00:00', '2005-04-30 22:00:00',
           '2005-04-30 23:00:00', '2005-05-01 00:00:00'],
          dtype='datetime64[ns]', length=2881, freq='H')
4
  • Does date_range help in place of bdate_range ? Commented Jun 2, 2021 at 6:36
  • @shahkalpesh I'm not sure what you're referring to. I'm conducting a time series analysis and need to remove a few rows. As a result, I must define a custom range or my code will return NaN. Commented Jun 2, 2021 at 6:39
  • @Ali he is referring to using pd.date_range (which will preserve the time, but not respect business days) as opposed to pd.bdate_range (which will start from midnight, but respect the business days). Commented Jun 2, 2021 at 6:41
  • @Cyttorak Yes, It worked. Thanks Commented Jun 2, 2021 at 6:49

1 Answer 1

1

You could try something like this:

import pandas as pd

start_date = '2005-1-1 02:00:00'
end_date   = '2005-5-1 02:00:00'

date_range = pd.date_range(start=start_date, end=end_date, freq='H')
print(date_range)

Output:

DatetimeIndex(['2005-01-01 02:00:00', '2005-01-01 03:00:00',
               '2005-01-01 04:00:00', '2005-01-01 05:00:00',
               '2005-01-01 06:00:00', '2005-01-01 07:00:00',
               '2005-01-01 08:00:00', '2005-01-01 09:00:00',
               '2005-01-01 10:00:00', '2005-01-01 11:00:00',
               ...
               '2005-04-30 17:00:00', '2005-04-30 18:00:00',
               '2005-04-30 19:00:00', '2005-04-30 20:00:00',
               '2005-04-30 21:00:00', '2005-04-30 22:00:00',
               '2005-04-30 23:00:00', '2005-05-01 00:00:00',
               '2005-05-01 01:00:00', '2005-05-01 02:00:00'],
              dtype='datetime64[ns]', length=2881, freq='H')
   
Sign up to request clarification or add additional context in comments.

2 Comments

Only date_range = pd.date_range (start=start_date, end=end_date, freq='H') does the work. Thanks
However, using Delta does not provide me with the correct interval. Please edit your post so that I can accept your response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.