Comments (1)
Well, that error sums it upp pretty good: "Period '2d' is invalid, must be one of ['1d', '5d', '1mo', '3mo', '6mo', '1y', '2y', '5y', '10y', 'ytd', 'max']"
A possible fix could be to get the data for five days and then truncate the data down to the last two. The following result appears to work. It should be noted that your code worked for me when I used 'symbol = "aapl"'...
import pandas as pd
import yfinance as yf
symbol = input("Enter the symbol: ")
equity = yf.Ticker(symbol)
try:
df = equity.history(period="5d", interval="1m")
if df.empty:
raise ValueError("No data available for the specified period and interval.")
except Exception as e:
print(f"An error occurred: {e}")
print("Attempting to fetch alternative data...")
# Current date (timezone aware)
now = pd.Timestamp.now(tz=df.index.tz)
# Calculate the date for yesterday
two_days_ago = now - pd.Timedelta(days=1)
# Truncate the DataFrame to include only data from the last two days
# Only include results that are from today, or yesterday
df_last_two_days = df[df.index >= two_days_ago]
df_last_two_days
from yfinance.
Related Issues (20)
- Retrieving Historical 'Financials' Data (e.g. quarterly earnings) with Yahoo Finance Premium Subscription?
- Revenue estimate? HOT 1
- option_chain() function is returning error while calling it.
- yf.download() not accepting custom period anymore → is this truly desired behavior? HOT 4
- fullTimeEmployees function not found
- No data returned when using start/end parameters (rather than 'period') for history and download methods HOT 3
- options data
- Currency data for statements HOT 3
- industry average HOT 2
- mutual fund holders for few tickers not able to fetch data. HOT 1
- Inconsistent results between two successive runs HOT 5
- yf.download(['CL=F'],progress=True) cannot get data HOT 1
- incorrect 52 week high and low (compare with Yahoo) HOT 4
- Data Period Varies With History and Download Functions HOT 3
- Error "cannot access local variable 'df_orig' where it is not associated with a value" when fetching MRO.L data
- [Volume] FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. HOT 1
- 'unit' keyword in TimedeltaIndex construction HOT 1
- YFTzMissingError('$%ticker%: possibly delisted; No timezone found') when downloading price data HOT 8
- Erroneous print statement in history scraper
- Inconsistet data between yf.download and yf.ticker.history HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yfinance.