How to generate a list with every monday, between two dates, and exclude that some a specific list using pandas

Question:

I want to generate a dataframe with pandas where one of the columns is filled with all mondays between to dates. But I need to exclude some mondays that are in a specific list. I could generate the column with the mondays, but I could find how to remove that mondays in the given list.

I generate the mondays using:

import pandas as pd

st=pd.to_datetime(‘8/22/2022′)
ed=pd.to_datetime(’12/22/2022’)

a1=pd.date_range(start=st,end=ed, freq=’W-MON’)

But I would like to exclude the mondays that are in this list

fer=pd.to_datetime([’09/07/2022′,’10/12/2022′,’10/15/2022′,’10/28/2022′,’11/01/2022′,’11/14/2022′,’11/15/2022′,’11/20/2022′])

I was not able to find the solution online.

Answers:

IIUC, you can use a negative pandas.Index.isin :

a1= a1[~a1.isin(fer)]

# Output :

print(a1)
​
DatetimeIndex(['2022-08-22', '2022-08-29', '2022-09-05', '2022-09-12',
               '2022-09-19', '2022-09-26', '2022-10-03', '2022-10-10',
               '2022-10-17', '2022-10-24', '2022-10-31', '2022-11-07',
               '2022-11-21', '2022-11-28', '2022-12-05', '2022-12-12',
               '2022-12-19'],
              dtype='datetime64[ns]', freq=None)
Answered By: abokey

Here’s a code snippet that hopefully answers your question. The code snippet uses the pandas package to remove a list of blacklisted dates from a list of dates. It does this by first generating a list of every Monday between a specified start and end date using the generate_mondays function. It then defines a list of blacklisted Mondays and converts both lists to pandas Series objects.

Next, the code uses the Series.isin() method to create a Boolean mask indicating which dates in the mondays Series are not in the blacklisted_mondays Series. This mask is then used to filter the mondays Series, and the resulting Series is converted to a list using the tolist() method.

The resulting list of non-blacklisted Mondays can be accessed by calling the non_blacklisted_mondays variable, which is the final line of the code snippet. This variable contains a list of all the Mondays between the start and end dates, with the blacklisted Mondays removed.

# Import the date and timedelta classes from the datetime module
from datetime import date, timedelta

# Import the pandas package
import pandas as pd

# Function to generate a list of every Monday between two dates
def generate_mondays(start_date, end_date):
  # Create a variable to hold the list of Mondays
  mondays = []

  # Create a variable to hold the current date, starting with the start date
  current_date = start_date

  # Calculate the number of days between the start date and the first Monday
  # We use (7 - start_date.weekday()) % 7 to find the number of days to the
  # next Monday, and then subtract one to get the number of days to the first
  # Monday
  days_to_first_monday = (7 - start_date.weekday()) % 7 - 1

  # Add the number of days to the first Monday to the current date to move to
  # the first Monday
  current_date += timedelta(days=days_to_first_monday)

  # Loop until we reach the end date
  while current_date <= end_date:
    # Append the current date to the list of Mondays
    mondays.append(current_date)

    # Move to the next Monday by adding 7 days
    current_date += timedelta(days=7)

  # Return the list of Mondays
  return mondays

# Set the start and end dates
start_date = date(2022, 1, 1)
end_date = date(2022, 12, 31)

# Generate a list of every Monday between the start and end dates
mondays = generate_mondays(start_date, end_date)

# Define a list of blacklisted Mondays
blacklisted_mondays = [
  date(2022, 1, 10),
  date(2022, 2, 14),
  date(2022, 3, 21),
]

# Convert the list of mondays and the list of blacklisted mondays to pandas
# Series objects
mondays_series = pd.Series(mondays)
blacklisted_mondays_series = pd.Series(blacklisted_mondays)

# Use the pandas Series.isin() method to create a Boolean mask indicating
# which dates in the mondays Series are not in the blacklisted_mondays Series
mask = ~mondays_series.isin(blacklisted_mondays_series)

# Use the mask to filter the mondays Series and convert the resulting Series
# to a list
non_blacklisted_mondays = mondays_series[mask].tolist()

# Print the resulting list of non-blacklisted Mondays
print(non_blacklisted_mondays)
Answered By: Jonas
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.