'If a date column doesn't have a certain date, then do something

I am trying to read from dataframe on Date column, if a certain date doesn't exist, then insert new data for that date.

I've tried to googled on how to know if a return is none, but I can't find any result.

So this is the way I handle it, if the return is KeyError, then I insert the date data. But my concern is, what if the KeyError is NOT because the data doesn't exist?

Or is there any better way than catching a KeyError like this?

My Code

def checkdata(ticker, date):
    try: 
        data = pd.read_sql(ticker, idxengine)
        data = data.set_index('Date')
        data.loc[date]
        print(data.loc[date])
    except KeyError as ke:
        print("Data not found, insert new date data" + ticker + str(date))

Update to simplify what I want to see as the end result

Let's say I have dataA and dataB

dataA = [['2022-02-01', 123], ['2022-02-02', 120]]
dataB = [['2022-02-01', 123], ['2022-02-03', 125]]

I want to have

dataC = [['2022-02-01', 123], ['2022-02-02', 120], ['2022-02-03', 125]]
dfC = pd.DataFrame(dataC, columns = ['date', 'price'])
print(dfC)

Expected output

         name  price
0  2022-02-01    123
1  2022-02-02    120
2  2022-02-02    125

What should I do?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source