how can i load all week days, between a start date and an end date, to a data frame?

  • Last Update :
  • Techknowledgy :

You can also use date_range for this purpose.

In[3]: pd.date_range('2011-01-05', '2011-01-09', freq = BDay())

Out[3]: DatetimeIndex(['2011-01-05', '2011-01-06', '2011-01-07'], dtype = 'datetime64[ns]', freq = 'B', tz = None)

Or even simpler

In[7]: pd.bdate_range('2011-01-05', '2011-01-09')

Out[7]: DatetimeIndex(['2011-01-05', '2011-01-06', '2011-01-07'], dtype = 'datetime64[ns]', freq = 'B', tz = None)

As of v0.14 you can use holiday calendars.

from pandas.tseries.holiday
import USFederalHolidayCalendar
from pandas.tseries.offsets
import CustomBusinessDay

us_bd = CustomBusinessDay(calendar = USFederalHolidayCalendar())
print pd.DatetimeIndex(start = '2010-01-01', end = '2010-01-15', freq = us_bd)

returns:

DatetimeIndex(['2010-01-04', '2010-01-05', '2010-01-06', '2010-01-07',
      '2010-01-08', '2010-01-11', '2010-01-12', '2010-01-13',
      '2010-01-14', '2010-01-15'
   ],
   dtype = 'datetime64[ns]', freq = 'C')

Use BDay() to get the business days in range.

from pandas.tseries.offsets
import *

In[185]: s
Out[185]:
   2011 - 01 - 01 - 0.011629
2011 - 01 - 02 - 0.089666
2011 - 01 - 03 - 1.314430
2011 - 01 - 04 - 1.867307
2011 - 01 - 05 0.779609
2011 - 01 - 06 0.588950
2011 - 01 - 07 - 2.505803
2011 - 01 - 08 0.800262
2011 - 01 - 09 0.376406
2011 - 01 - 10 - 0.469988
Freq: D

In[186]: s.asfreq(BDay())
Out[186]:
   2011 - 01 - 03 - 1.314430
2011 - 01 - 04 - 1.867307
2011 - 01 - 05 0.779609
2011 - 01 - 06 0.588950
2011 - 01 - 07 - 2.505803
2011 - 01 - 10 - 0.469988
Freq: B

With slicing:

In[187]: x = datetime(2011, 1, 5)

In[188]: y = datetime(2011, 1, 9)

In[189]: s.ix[x: y]
Out[189]:
   2011 - 01 - 05 0.779609
2011 - 01 - 06 0.588950
2011 - 01 - 07 - 2.505803
2011 - 01 - 08 0.800262
2011 - 01 - 09 0.376406
Freq: D

In[190]: s.ix[x: y].asfreq(BDay())
Out[190]:
   2011 - 01 - 05 0.779609
2011 - 01 - 06 0.588950
2011 - 01 - 07 - 2.505803
Freq: B

and count()

In[191]: s.ix[x: y].asfreq(BDay()).count()
Out[191]: 3

On top of this answer and xone, we can write a short function to return the trading days of US exchange:

from xone
import calendar

def business_dates(start, end):
   us_cal = calendar.USTradingCalendar()
kw = dict(start = start, end = end)
return pd.bdate_range( ** kw).drop(us_cal.holidays( ** kw))

In[1]: business_dates(start = '2018-12-20', end = '2018-12-31')
Out[1]: DatetimeIndex(['2018-12-20', '2018-12-21', '2018-12-24', '2018-12-26',
      '2018-12-27', '2018-12-28', '2018-12-31'
   ],
   dtype = 'datetime64[ns]', freq = None)

Example:

In[1]: pd.bdate_range("2020-01-01", "2020-01-06")
Out[1]: DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-06'], dtype = 'datetime64[ns]', freq = 'B')

If you also work on Saturdays or have an unusual working week, you also want to exclude public holidays in your country.

import pandas as pd
from datetime
import datetime

weekmask = 'Sun Mon Tue Wed Thu'
exclude = [pd.datetime(2020, 5, 1),
   pd.datetime(2020, 5, 2),
   pd.datetime(2020, 5, 3)
]

pd.bdate_range('2020/4/30', '2020/5/26',
   freq = 'C',
   weekmask = weekmask,
   holidays = exclude)

Suggestion : 2

Last Updated : 18 Jul, 2021,GATE CS 2021 Syllabus

Output:

The original range: 2015 - 06 - 03 00: 00: 00 2015 - 07 - 01 00: 00: 00
Total business days in range: 20

Output : 

The original range: 2015 - 06 - 03 00: 00: 00 2015 - 06 - 30 00: 00: 00
Total business days in range: 20

Suggestion : 3

Get better at data science interviews by solving a few questions per week,next, set the desired start date and end date to filter df with -- these can be in datetime (numpy and pandas), timestamp, or string format,assign mask to df to return the rows with birth_date between our specified start/end dates,We can perform this using a boolean mask First, lets ensure the 'birth_date' column is in date format

import pandas as pd
import numpy as np
raw_data = {
   'name': ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel'],
   'age': [20, 19, 22, 21],
   'favorite_color': ['blue', 'red', 'yellow', "green"],
   'grade': [88, 92, 95, 70],
   'birth_date': ['01-02-1996', '08-05-1997', '04-28-1996', '12-16-1995']
}
df = pd.DataFrame(raw_data, index = ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel'])
df
df['birth_date'] = pd.to_datetime(df['birth_date'])
start_date = '03-01-1996'
end_date = '06-01-1997'
mask = (df['birth_date'] > start_date) & (df['birth_date'] <= end_date)
df = df.loc[mask]
df

Suggestion : 4

Normalize start/end dates to midnight before generating date range.,Name of the resulting DatetimeIndex.,Time zone name for returning localized DatetimeIndex, for example Asia/Beijing.,Of the four parameters: start, end, periods, and freq, exactly three must be specified. Specifying freq is a requirement for bdate_range. Use date_range if specifying freq is not desired.

>>> pd.bdate_range(start = '1/1/2018', end = '1/08/2018')
DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04',
      '2018-01-05', '2018-01-08'
   ],
   dtype = 'datetime64[ns]', freq = 'B')

Suggestion : 5

Last modified: August 17, 2021

1._
public static List getDatesBetweenUsingJava7(Date startDate, Date endDate) {
   List datesInRange = new ArrayList < > ();
   Calendar calendar = getCalendarWithoutTime(startDate);
   Calendar endCalendar = getCalendarWithoutTime(endDate);

   while (calendar.before(endCalendar)) {
      Date result = calendar.getTime();
      datesInRange.add(result);
      calendar.add(Calendar.DATE, 1);
   }

   return datesInRange;
}

private static Calendar getCalendarWithoutTime(Date date) {
   Calendar calendar = new GregorianCalendar();
   calendar.setTime(date);
   calendar.set(Calendar.HOUR, 0);
   calendar.set(Calendar.HOUR_OF_DAY, 0);
   calendar.set(Calendar.MINUTE, 0);
   calendar.set(Calendar.SECOND, 0);
   calendar.set(Calendar.MILLISECOND, 0);
   return calendar;
}

In Java 8, we can now create a continuous infinite Stream of dates and take only the relevant part. Unfortunately, there is no way of terminating an infinite Stream when a predicate gets matched – this is why we need to calculate the number of days between those two days and then simply limit() the Stream:

public static List<LocalDate> getDatesBetweenUsingJava8(
  LocalDate startDate, LocalDate endDate) { 
 
    long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate); 
    return IntStream.iterate(0, i -> i + 1)
      .limit(numOfDaysBetween)
      .mapToObj(i -> startDate.plusDays(i))
      .collect(Collectors.toList()); 
}
3._
public static List<LocalDate> getDatesBetweenUsingJava9(
  LocalDate startDate, LocalDate endDate) {
 
    return startDate.datesUntil(endDate)
      .collect(Collectors.toList());
}