在 DatetimeIndex 上合并两个 DataFrame
Merging two DataFrames on DatetimeIndex
到目前为止,我还没有遇到过此类 DataFrame 操作的问题。
我想要实现的是获得一个 DataFrame,其中包含几年的每日数据和过去 5 天的每小时数据。我的代码(完全运行):
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)
当然,我正在尝试合并两个索引(首先从 fine 索引中删除 tz 数据)。我得到的结果是一个空的 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: []
如您所见,合并操作将相似的 DataFrame 列拆分为 _x 和 _y 列,然后,当然,没有共同的值,因此是空的 DataFrame。
我尝试过将时间分配给 粗略的 日期、重置索引并合并日期列、重命名索引和其他绝望的东西,但没有任何效果。
也许有人知道发生了什么,非常感谢你的帮助
我已经尝试了一些更绝望的东西,显然,我需要指定我想要合并的列。另外这里需要outerjoin方法
yfdata = pd.merge(coarse, fine, how='outer', on=coarse.columns.to_list(), left_index=True, right_index=True)
到目前为止,我还没有遇到过此类 DataFrame 操作的问题。
我想要实现的是获得一个 DataFrame,其中包含几年的每日数据和过去 5 天的每小时数据。我的代码(完全运行):
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)
当然,我正在尝试合并两个索引(首先从 fine 索引中删除 tz 数据)。我得到的结果是一个空的 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: []
如您所见,合并操作将相似的 DataFrame 列拆分为 _x 和 _y 列,然后,当然,没有共同的值,因此是空的 DataFrame。
我尝试过将时间分配给 粗略的 日期、重置索引并合并日期列、重命名索引和其他绝望的东西,但没有任何效果。
也许有人知道发生了什么,非常感谢你的帮助
我已经尝试了一些更绝望的东西,显然,我需要指定我想要合并的列。另外这里需要outerjoin方法
yfdata = pd.merge(coarse, fine, how='outer', on=coarse.columns.to_list(), left_index=True, right_index=True)