I've been aware that the phrase originated with Thomas Carlyle, the 19th-century writer best known for his history of the French Revolution - he memorably and unflatteringly described Robespierre, known to his contemporaries as l'Incorruptible, and remembered today for his enthusiasm for political executions, as "the sea-green incorruptible". But recently I learnt from Robert Dixon, via Derek Thompson and Chris Dillow, the context in which Carlyle came up with the phrase. He introduced it, in contrast to the 'gay science'* of poetry, in his 1849 essay Occasional Discourse on the Negro Question (original here, more readably here, historical background here).
...the social science - not a 'gay science,' but a rueful - which finds the secret of this universe in 'supply-and-demand,' and reduces the duty of human governors to that of letting men alone, is also wonderful. Not a "gay science," I should say, like some we have heard of; no, a dreary, desolate and, indeed, quite abject and distressing one; what we might call, by way of eminence, the dismal science.Carlyle's subject matter was not economics but the use of slave labour on sugar plantations in the Caribbean, which had been banned in 1833. He was being intentionally provocative - the essay was originally published anonymously, purported transcribed in an elaborate fiction by a Dr Phelim M'Quirk, and evoked a vigorous riposte, The Negro Question, from John Stuart Mill (original here, more readably here). (None of this old-fashioned debate is ancient history: Mill lived long enough to be briefly godfather to Bertrand Russell, who was the grandson of the Lord John Russell, then prime minister, upbraided by Carlyle in his essay.)
By 1849 British sugar plantations in the Caribbean were struggling: slave labour was no longer legal, and the 1846 Sugar Duties Act had started the process of removing the preferential tariffs protecting the plantations from competition from countries where slavery was still legal, and from European sugar beet. (At the end of his essay, Carlyle tacitly acknowledges the problem of competition by advocating the sending of gunships to stop shipments of African slaves to Cuba and Brazil.) The former slaves, Carlyle complains, were unwilling to cut sugar cane for the wages on offer, preferring to work growing pumpkins for themselves.
The economists' solution to the problem of insufficient labour was to increase supply. Plantation owners tried bringing in indentured labour from India - a practice strongly opposed by the anti-slavery movement - but Carlyle says the economists had recommended importing more (free) African labour. He points out that newly arrived Africans preferred growing pumpkins just as much as did the original freed slaves, and that the only way to make them work on the sugar plantations would be to bring in so many Africans that they would otherwise starve - he makes a comparison with the Great Famine then causing mass emigration from Ireland. But he doesn't admit the implication that the only thing that could make a man choose to work under the pay and conditions of a sugar plantation was starvation.
Carlyle's proposal was the Africans should be compelled to work on the plantations "with beneficient whip". The economist on the other hand would say that sugar should be grown on the plantations only if it were profitable enough that an attractive wage could be paid. The plantations might be protected by tariffs from competition from slave countries, but if they were unable to compete with European sugar production from beet, it would best to let the Europeans grow sugar for us and grow other crops in the Caribbean. Pumpkins, if nothing else, had shown themselves capable of paying their way. Whereas Carlyle seemed to be speaking for the owners of sugar plantations, the economist would have in mind the interests of humanity in general.
The true dies mali - evil days - were those of slave labour enforced by men with whips. Economics might well follow Carlyle in calling itself the 'rueful science', but it's Carlyle's racism which was dismal.
*The Consistori del Gay Saber - "Consistory of the Gay Science" was an academy of poetry founded in Toulouse in 1323 to perpetuate the lyric school of the troubadours. In the early 1840's Ralph Waldo Emerson described himself as "a Professor of the Joyous Science", and the term was revisited explicitly by E.S Dallas in his The Gay Science (1866). Nietzsche adopted it for his Die fröhliche Wissenschaft in 1882.