dimanche 10 octobre 2021

datetime.date is not matching other datetime.date in if statement within a for loop

For my first self directed python project I have chosen to try to get some stock data into a Series. I have created a method that creates days when the market is not open(nonbizday) and another method that creates datetime.dates going backward from the day the script is run(date_maker). Within the method that goes back one day, it is supposed to check if that day is on the list created by .nonbizday(). It will not remove any days and I cant figure out why.

Heres the two methods: (the nobizday is saved as an attribute to the class the methods are within... if that makes sense)

def nonbizday(self):
    newnew = datetime.date(year=2021, day=2, month=1)
    nobiz = [datetime.date(year=2021, day=2, month=1)]

    while newnew.year == 2021:
        newyears = datetime.date(year=2021, day=1, month=1)
        newnew = newnew + datetime.timedelta(days=1)
        nobiz.append(newnew)
        newnew = newnew + datetime.timedelta(days=6)
        nobiz.append(newnew)
    return nobiz

def date_maker(self):
    btime = self.data['Meta Data']['3. Last Refreshed']
    btime = datetime.datetime.strptime(btime, dtnotime)
    btime = btime.date()
    benchlist = []

    for i in range(10):
        motime = i + 1
        benchie = btime - (datetime.timedelta(days=motime))
        # benchie = benchie.date()
        adding = False
        print(benchie)
        for offdays in self.nobiz:
            print(offdays)
            if benchie == offdays:
                adding = False
            else:
                adding = True
        print(adding)
        print(isinstance(benchie, datetime.date))
        print(isinstance(offdays, datetime.date))
        if adding == True:
            benchlist.append(benchie)
    return benchlist

I know this is probably very crude to the more fluent pythonista, any other suggestions are also helpful

Aucun commentaire:

Enregistrer un commentaire