I have a largish DataFrame with a date index ['Date'] and several columns. One column is a string identifier ['Type'], with related data in the remaining columns. I need to add newData to the DataFrame, but only if the date-type pair (i.e. index-ColumnValue pair) is not already present in the DataFrame. Checking for the existing pairing takes up ~95% of my code computing time, so I really need to find a quicker way to do it.
Options already considered, with timings in increasing order of speed::
existing_pair = len(compiledData[(compiledData['Type'] == newData['Type'])
& (compiledData.index == newData['Date'])]) > 0
# average = 114 ms
existing_pair = newData['Date'] in compiledData[compiledData['Type'] ==
newData['Type']].index
# average = 68 ms
existing_pair = compiledData[compiledData.index == newData['Type']]['Type'].
isin([newData['Date']]).any()
# average = 44 ms
I am relatively new to Python so I am sure there are better (= faster) ways of checking for an index-colVal pair. Or, it may be that my entire data structure is wrong. Would appreciate any pointers anyone can offer.
Edit: Sample of the compiledData dataframe:
Type ColA ColB ColC ColD ColE ColF
2021-01-19 B 83.0 -122.15 0.0 11.0 11.000 11.0
2021-01-19 D 83.0 -1495.48 0.0 11.0 11.000 11.0
2021-03-25 D 83.0 432.00 0.0 11.0 11.000 11.0
2021-04-14 D 83.0 646.00 0.0 11.0 11.000 11.0
2021-04-16 A 20.0 11.00 0.0 30.0 11.000 11.0
2021-04-25 D 83.0 -26.82 0.0 11.0 11.000 11.0
2021-04-28 B 83.0 -651.00 0.0 11.0 11.000 11.0