Git Product home page Git Product logo

Comments (1)

ItzXyers avatar ItzXyers commented on July 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.