2

I haven't had problems with such DataFrame operations until now.

What I'm trying to achieve, is to get a DataFrame, containing daily data for a few years period and hourly data for the last 5 days. My code (fully running):

import yfinance as yf
import pandas as pd

ticker = yf.Ticker('TSLA')
fine = ticker.history(period='5d', interval='60m')
fine.index = fine.index.tz_convert('UTC').tz_localize(None)
print(fine)
coarse_end_date = fine.index[0].date()
coarse = ticker.history(start='2019-01-01', end=coarse_end_date, interval='1d')
print(coarse)
yfdata = pd.merge(coarse, fine, how='inner', left_index=True, right_index=True)
print(yfdata)

Naturally, I'm trying to merge on both indexes (removing tz data from fine index first). What I get in result is an empty DataFrame:

Empty DataFrame
Columns: [Open_x, High_x, Low_x, Close_x, Volume_x, Dividends_x, Stock Splits_x, Open_y, High_y, Low_y, Close_y, Volume_y, Dividends_y, Stock Splits_y]
Index: []

As you can see, merge operation splits similar DataFrame columns into _x and _y columns, and then, of course, there are no common values, hence the empty DataFrame.

I've tried assigning time to coarse dates, resetting indexes and merging on date column, renaming indexes, and other desperate stuff, but nothing worked.

Maybe someone has an idea of what's happening, I would highly appreciate your help

mldc
  • 31
  • 3
  • The index of `fine` is like `2021-03-30 19:30:00` and the index of `coarse` is like `2018-12-31`. They are not the same structure, thus their comparison will be always false. – Ynjxsjmh Mar 31 '21 at 11:30
  • Both are `DatetimeIndex`, it's only that time is hidden in case of `coarse` – mldc Mar 31 '21 at 11:37

1 Answers1

1

I've tried some more desperate stuff, and apparently, I needed to specify columns I want to merge. Also, outer join method is required here.

yfdata = pd.merge(coarse, fine, how='outer', on=coarse.columns.to_list(), left_index=True, right_index=True)

mldc
  • 31
  • 3
  • 1
    after update to pandas version 1.2.3 this solution no longer works. I actually needed to use *concat* method: `yfdata = pd.concat([coarse, fine])` – mldc Apr 06 '21 at 18:08