Walmart (WMT  ) shares were 6% higher as the company beat analysts' estimates on the top and bottom line and affirmed its guidance for the second half of the year. Walmart shares took a major hit in the first half of the year as the retailer dealt with an inventory overhang and consumers shifted their spending from higher margins items to lower-margin ones.

However, the stock seems to have bottomed and is now nearly 20% higher from its lows. One factor is that the inventory issues seem to be in the rearview mirror, and as a defensive stock, it benefits from inflows as recession odds increase. Overall, the stock is down 5% YTD, and it's 15% off its all-time highs from April of this year.

Inside the Numbers

In Q2, Walmart reported $1.77 in earnings per share, beating analysts' estimates of $1.62 per share in earnings. Revenue also beat at $152.9 billion vs. $150.8 billion. Overall, this was a 24% improvement from last year in earnings and a 4% increase in revenues.

Same-store sales were up 6.5%, beating expectations of 5.9% growth. Digital sales were up 12% compared to last year and 18% over the last 2 years.

However, the company did reiterate the continued weakness in profit margins due to consumers buying less higher-margin items. It expects this to continue for the rest of the year. It reiterated its forecast of 3% same-store sales growth, excluding fuel, for the next quarter and 4% for the year.

The company said that foot traffic was up as consumers prioritized value. It's seeing particular strength among households with an annual income of $100,000 or more. It also noted more signs of stress among shoppers including more use of credit cards, buying smaller packages of food, and increasing sales of canned tuna and beans.

The company's inventory situation is slowly improving. Compared to last year, inventories are up 25%. As a result, the company has been marking down merchandise and has canceled 'billions of dollars in orders to help align inventory levels with expected demand'.