yfinance icon indicating copy to clipboard operation
yfinance copied to clipboard

Dataframe with incorrect index type returned when combining two tickers (one without data)

Open btodac opened this issue 2 years ago • 6 comments

import yfinance as yf
df = yf.download( tickers = ["^NDX", "^TWII"],       
                  start="2023-01-22", end="2023-01-29",
                  interval = "1m",
                  group_by = 'column',
                  auto_adjust = False,
                  prepost = True,
                  threads = True,
                  proxy = None
                  )
[*********************100%***********************]  2 of 2 completed

1 Failed download:
- ^TWII: No data found for this date range, symbol may be delisted

It's not delisted, it's just there as there is no data due to the lunar new year. The problem is the returned dataframe index is not a datetime index, but has type object)

yfinace version 0.2.9, python 3.10.8 on Linux ubuntu

btodac avatar Jan 30 '23 13:01 btodac

What happens with other dates?

asafravid avatar Feb 01 '23 22:02 asafravid

@asafravid Not helpful.

This appears to be a simple problem for someone to debug & fix, clue is one ticker doesn't return data so maybe messing up merge.

ValueRaider avatar Feb 01 '23 22:02 ValueRaider

I’ll check

asafravid avatar Feb 01 '23 23:02 asafravid

I had a look at this myself. Pandas concat now combines the empty dataframe and then fails to convert the timezone. I altered a try catch block in multi.py at line 155 to remove the empty dataframe and then everything seemed to work fine:

try:
        dfs = dict([(key, df) for key, df in shared._DFS.items() if not df.empty])
        data = _pd.concat(dfs.values(), axis=1, sort=True,
                          keys=dfs.keys())
    except Exception:
        _realign_dfs()
        data = _pd.concat(shared._DFS.values(), axis=1, sort=True,
                          keys=shared._DFS.keys())

btodac avatar Feb 02 '23 16:02 btodac

Can you create a PR?

asafravid avatar Feb 02 '23 16:02 asafravid

remove the empty dataframe

Is this best solution? My concern is users might think yfinance is failing to fetch and post Issues.

ValueRaider avatar Feb 02 '23 16:02 ValueRaider