You're right, when you create a new frame and change the plot type, it will inherit the dataset from the frame behind it in the frame order. That said, the dataset is not "attached" to the new frame until you change the plot type. The act of changing the plot type attaches the dataset.
This is (in my mind) an unfortunate behavior and we should have required explicit attachment of a dataset. So, I would not rely on this behavior.
You can attach an explicit dataset via: TecUtilFrameSetDataSet().
If you must rely on the existing behavior, you will have to determine if the frame below the active frame in the frame order has an available dataset before changing the plot type.