What happened

Shares of offshore drilling rig operator Transocean (NYSE:RIG) soared 25.8% during the first six months of 2018, according to data provided by S&P Global Market Intelligence. This was similar to rival Ensco's (NYSE:ESV) 22.8% gain. Both stocks handily beat the S&P 500's 1.7% gain over the same period.

Things didn't look so good for either stock early in the year, as both experienced a gut-wrenching drop in January and February. But in April and May, both stocks came roaring back.

Yellow pipes and support structures atop blue water

The stock market punished the offshore rig industry after the oil price slump in 2014. Now the recovery has begun. Image source: Getty Images.

So what

Transocean's performance boils down to one primary factor: oil prices. Offshore drilling has much higher up-front expenses than onshore shale drilling. That's why when oil prices tumbled in 2014, oil companies severely cut back on new rig contracts, which in turn decimated the offshore industry. Some companies, like Seadrill, filed for bankruptcy. Others, like Atwood Oceanics, were gobbled up by competitors (in Atwood's case, Ensco). Everyone else saw share prices tumble, in many cases losing more than three-quarters of their value.

Finally in 2018, though, oil prices began what appears to be a sustained recovery. After hovering around the $50-per-barrel mark for much of 2017, both Brent Crude and West Texas Intermediate crude -- the key international and domestic crude oil benchmarks -- have risen to above $70 per barrel. That's given investors confidence that the offshore rig industry might take off again, as the economics start to allow oil companies to recoup their investments in offshore drilling more quickly.

The data bears out this theory: Transocean's January-February stock drop corresponded with a drop in crude prices, while its April-May recovery occurred as crude prices reached three-year highs.

Now what

The sustained rise in crude prices has now hit the one-year mark, which has been good for the overall oil and gas sector and fantastic for shares of offshore drillers like Transocean. However, it's worth noting that even with the excellent first half of 2018 factored in, both oil prices and offshore drillers' stocks are well off their five-year highs:

RIG Chart

RIG data by YCharts.

If oil prices continue to rise -- or even stabilize at current levels -- Transocean's stock should continue to rise in turn. And even though that appears likely -- at least in the near term -- investors should remember that oil prices can turn on a dime, as they did in 2014. While the climate seems generally good for oil and gas stocks right now, they should be included only as part of a diversified portfolio.

John Bromels has no position in any of the stocks mentioned. The Motley Fool has no position in any of the stocks mentioned. The Motley Fool has a disclosure policy.