My dataframe has content when obtaining Close data, but is empty when receiving any other data

74 views Asked by At

I'm new to Python and stock trading, and using multiple Pandas dataframes to store various stock data. I already have a Close dataframe and am now trying to create separate dataframes for Open, High, Low etc.

I used data = yf.download(tickers=data, period='1y', interval='1d')['Close'] to get just the Close data for all tickers in the nasdaq_100_tickers.txt file. Then I saved it into a CSV file, then use the CSV file to make a Pandas dataframe. Works perfectly and as intended.

However if I try and do the same thing with Open, High, Low or Volume, the dataframe is entirely empty. Why does this happen? The code is nearly exactly the same apart from swapping 'Close' for 'Open', 'High' etc. If I was meant to make the dataframe differently, why are these other columns different to the Close data?

There appears to be no issue obtaining the data from Yahoo Finance.

This is my code:

def get_tickers():
    # Get list of tickers
    tickers = open("dataset/nasdaq_100_tickers.txt", "r")
    data = tickers.read().splitlines()

    return data


def get_open_data(data):
    # Check if the data has already been downloaded
    if os.path.exists('open.csv'):
        dataframe = pd.read_csv('open.csv', index_col="Date", parse_dates=True).dropna()
    else:
        # Download Open data from Yahoo Finance
        data = yf.download(tickers=data, period='1y', interval='1d')['Open']
        data.to_csv('open.csv')
        # Convert array to pandas dataframe, remove NaN values
        complete_data = data.dropna()
        dataframe = pd.DataFrame(complete_data)

    return dataframe

def get_close_data(data):
    # Check if the data has already been downloaded
    if os.path.exists('close.csv'):
        dataframe = pd.read_csv('close.csv', index_col="Date", parse_dates=True).dropna()
    else:
        # Download Close data from Yahoo Finance
        data = yf.download(tickers=data, period='1y', interval='1d')['Close']
        data.to_csv('close.csv')
        # Convert array to pandas dataframe, remove NaN values
        complete_data = data.dropna()
        dataframe = pd.DataFrame(complete_data)

    return dataframe

The open.csv file has data - Opening prices of NASDAQ-100 companies from 2023-01-11 to 2022-12-22.

                  AAPL        ABNB  ...         ZM          ZS
Date                                ...                       
2023-01-11  131.250000   89.000000  ...  70.980003  105.559998
2023-01-12  133.880005   95.309998  ...  69.830002  103.730003
2023-01-13  132.029999   97.500000  ...  69.089996  102.610001
2023-01-17  134.830002   99.915001  ...  69.500000  105.190002
2023-01-18  136.820007  101.410004  ...  71.000000  115.410004
...                ...         ...  ...        ...         ...
2023-12-18  196.089996  147.160004  ...  70.860001  221.660004
2023-12-19  196.160004  147.500000  ...  71.570000  221.490005
2023-12-20  196.899994  146.500000  ...  71.330002  222.550003
2023-12-21  196.100006  142.369995  ...  70.580002  221.679993
2023-12-22  195.179993  141.880005  ...  72.120003  221.669998

Snapshot of ticker names from nasdaq_100_tickers.txt file:

AAPL
MSFT
AMZN
NVDA
META
AVGO
GOOGL
GOOG
TSLA
ADBE
COST

Any help is appreciated, thank you.

0

There are 0 answers