A bee in your bonnet
Preoccupied or obsessed with an idea.
This phrase clearly alludes to the state of agitation one would be in when finding a bee inside one's bonnet. It follows on from the earlier expression 'to have bees in one's head', which had much the same meaning. This is recorded from the 16th century, for example, in Alexander Douglas's Aeneis, 1513:
Quhat bern be thou in bed with heid full of beis?
Beekeepers have always worn protective headgear when working with bees and it is possible, although entirely speculative, that the bonnet refers to this.
The first citation of 'bee in his bonnet' in print that I have found is the Reverend Philip Doddridge's Letters, 1790:
"I suppose you have heard of Mr. Coward's pranks. He has, as the Scotch call it, a Bee in his Bonnet."
The reference to Scotland is significant and the expression may well be Scottish. Early bonnets were caps worn by men and boys and had gone out of use in England by the time the phrase emerged but continued to be used in Scotland.
A form of the phrase appears to have been used by Robert Herrick, in the poem The Mad Maid's Song, 1648. Robert Nares certainly thought so when he published A glossary; or, Collection of words in 1822. He wrote:
The phrase [bee in the bonnet] is clearly alluded to in the following passage:
For pity, sir, find out that bee
Which bore my love away.
I'll seek him in your bonnet brave,
which is an extract from Herrick's poem. On further reading of the full text, that case doesn't seem to be well made. Herrick's imagery is unusual, to say the least, and the 'him' that is referred to appears to be the mad maid's lost love, not an insect.