Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use pd.to_timedelta with yfinacne download?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading