Home>
I want to achieve

date.csv (date is recorded)

date
20200401
20200402
20200405
20200407
20200409


When I met
I want to store it as a number in the list like date_list = [20200401, 20200402, 20200405, ....]. Is there a way to store only the dates excluding Saturdays, Sundays, and holidays at this time?

April 5, 2020 will be Sunday, so the output will be
We expect the result date_list = [20200401, 20200402, 20200407, 20200409].

The current source code looks like the following, simply putting the date columns in order.

import csv
import pandas as pd
#Date information
date_data = pd.read_csv ('date.csv')
date_list = []
for i, date in enumerate (date_data ['date']):
date_list.append (date_data ['date'] [i])
print (date_list)
  • Answer # 1

    import pandas as pd
    import datetime
    import jpholiday
    def weekday (date):
        dt = datetime.date (int (date [0: 4]), int (date [4: 6]), int (date [6: 8]))
        d = dt.isoweekday ()
        if jpholiday.is_holiday (dt):
            return False
        if d>= 6:
            return False
        else: else:
            return True
    df = pd.read_csv ('date.csv')
    df ['date'] = df ['date'] .astype ('str')
    df ['weekday'] = df ['date'] .apply (weekday)
    df = df [df ['weekday']]
    date_list = df ['date']. to_list ()
    # ['20200401', '20200402', '20200407', '20200409']