Sweden Finally Chose a Prime Minister. She Lasted About 7 Hours.

It seemed like a new era was dawning in Sweden on Wednesday when Magdalena Andersson, the leader of the center-left Social Democratic Party, became the country’s first female prime minister.

Her historic term lasted about seven hours.

She resigned after a painful budget defeat in parliament. She had only just formed a two-party minority government with the Green Party. But after their budget was rejected in favor of one proposed by the opposition, which included the far-right Sweden Democrats party, the Green Party quit the coalition out of frustration. That left Ms. Andersson without a partner.

Her resignation plunged Sweden into political uncertainty. The country’s political landscape was already frayed by fragile coalition governments and a vote of no confidence in June against the former prime minister, Stefan Lofven. Ms. Andersson later succeeded Mr. Lofven as leader of the Social Democrats.

Sweden, at one point, accepted more refugees per capita than any other European nation. But its progressive image has gradually been eroded by far-right populist sentiment that has taken hold, led by the Sweden Democrats party. The political spectrum has shifted to the right, with increasing anti-immigrant and anti-European voices.

Ms. Andersson, 54, announced that she was the new prime minister in a Facebook statement a little before 10 a.m. local time on Wednesday. A little after 4:30 p.m. the same day, she released a second Facebook statement announcing her resignation.

“According to constitutional practice, a coalition government should resign if one party leaves the government,” she said. “For me, it is about respect, but I also do not want to lead a government where there may be grounds to question its legitimacy.”

Per Bolund, a Green Party spokesman, said his faction left the government because Parliament had approved a state budget negotiated by the Sweden Democrats. The party, with neo-Nazi roots and known for its anti-immigrant politics, has seen a steady rise in popularity over the last few years.

Until a new government is elected, the current one will remain on. Ms. Andersson, who served as Sweden’s finance minister since 2014, has said she is still ready to serve as prime minister, but only in a one-party government.