How to use pd.to_timedelta with yfinacne download?

82 Views Asked by At

I'm running a bit of code to download stock information about various stocks using yfinance. I've been running this simplified bit of code for months now and have recently gotten "FutureWarning" errors when I run the following block of code

import pandas as pd
import yfinance as yf
print(yf.download('MMM', period='6h'))

The Error that I receive is the following: The 'unit' keyword in TimedeltaIndex construction is deprecated and will be removed in a future version. Use pd.to_timedelta instead. df.index+= _pd.TimedeltaIndex(dst-error_hours,'h')

I've tried using yfinance's start and end parameters to try and fix the issue, and have tried changing the period length and type, (period='10h', period='12y',etc). Everything I've done has given me the same error.

1

There are 1 best solutions below

1
TheHungryCub On BEST ANSWER

The warning is originating from the yfinance library itself, and it's related to the internal implementation of the library. Unfortunately, there's not much you can do directly within your code to suppress this warning, as it is being triggered by the library's own implementation details.

However, you can temporarily suppress all FutureWarnings using Python's warnings module. Try this:

import warnings
import pandas as pd
import yfinance as yf

# Suppress FutureWarnings
warnings.simplefilter(action='ignore', category=FutureWarning)

# Download stock data
data = yf.download('MMM', period='6h')

print(data)

Output:

            Open       High        Low      Close  Adj Close   Volume
Date                                                                 
2024-02-28  92.0  92.629997  91.434998  91.434998  91.434998  1347032