Python 按分钟YFin和Tweepy情绪分数合并得到的数据框为空
您好,我想合并两个数据帧(YFinance,DF1的分钟级价格和Tweepy,DF2的情绪分数),但以下代码给了我一个空数据帧: DF1的结构:Python 按分钟YFin和Tweepy情绪分数合并得到的数据框为空,python,pandas,dataframe,sentiment-analysis,yfinance,Python,Pandas,Dataframe,Sentiment Analysis,Yfinance,您好,我想合并两个数据帧(YFinance,DF1的分钟级价格和Tweepy,DF2的情绪分数),但以下代码给了我一个空数据帧: DF1的结构: Datetime 2021-03-30 12:00:00-04:00 331.290009 331.480011 331.290009 331.480011 2021-03-30 12:01:00-04:
Datetime
2021-03-30 12:00:00-04:00 331.290009 331.480011 331.290009 331.480011
2021-03-30 12:01:00-04:00 331.679993 331.709991 331.549988 331.609985
2021-03-30 12:02:00-04:00 331.640015 331.640015 331.209991 331.209991
2021-03-30 12:03:00-04:00 331.290009 331.459991 331.190002 331.428009
2021-03-30 12:04:00-04:00 331.480011 331.480011 331.339996 331.359985
... ... ... ... ...
2021-03-31 11:55:00-04:00 327.269989 327.359985 327.239990 327.359985
2021-03-31 11:56:00-04:00 327.299988 327.380005 327.184998 327.279999
2021-03-31 11:57:00-04:00 327.190002 327.234985 327.109985 327.234985
2021-03-31 11:58:00-04:00 327.230011 327.369995 327.200012 327.279999
2021-03-31 11:59:00-04:00 327.325012 327.630005 327.245087 327.245087
Adj Close Volume
Datetime
2021-03-30 12:00:00-04:00 331.480011 0
2021-03-30 12:01:00-04:00 331.609985 2260
2021-03-30 12:02:00-04:00 331.209991 1886
2021-03-30 12:03:00-04:00 331.428009 3569
2021-03-30 12:04:00-04:00 331.359985 2191
... ... ...
2021-03-31 11:55:00-04:00 327.359985 4419
2021-03-31 11:56:00-04:00 327.279999 4428
2021-03-31 11:57:00-04:00 327.234985 2098
2021-03-31 11:58:00-04:00 327.279999 3447
2021-03-31 11:59:00-04:00 327.245087 7598
DF2的结构
Time Date Compound Subjectivity Polarity \
0 2021-03-31 23:58:23-05:00 2021-03-31 0.6369 0.300000 1.000000
1 2021-03-31 23:57:25-05:00 2021-03-31 0.6369 0.300000 1.000000
2 2021-03-31 23:56:57-05:00 2021-03-31 0.5719 0.427273 0.068182
3 2021-03-31 23:56:52-05:00 2021-03-31 0.6369 0.300000 1.000000
4 2021-03-31 23:56:31-05:00 2021-03-31 0.5994 0.287500 -0.031250
5 2021-03-31 23:56:16-05:00 2021-03-31 0.5719 0.427273 0.068182
6 2021-03-31 23:55:45-05:00 2021-03-31 0.0000 0.700000 0.000000
7 2021-03-31 23:53:08-05:00 2021-03-31 0.5719 0.427273 0.068182
8 2021-03-31 23:45:36-05:00 2021-03-31 0.6369 0.300000 1.000000
9 2021-03-31 23:43:21-05:00 2021-03-31 0.6369 0.300000 1.000000
10 2021-03-31 23:39:37-05:00 2021-03-31 0.0000 0.700000 0.000000
11 2021-03-31 23:39:20-05:00 2021-03-31 0.3818 0.560000 0.140000
12 2021-03-31 23:38:38-05:00 2021-03-31 0.6369 0.300000 1.000000
我想根据时间将它们合并为一个DF,我运行了以下命令:
output = tweet_df.merge(price, left_index= True, right_index= True)
print(output)
print('Finish combining price and sentiment')
tweet_df.index = pd.to_datetime(tweet_df.index)
price.index = pd.to_datetime(price.index)
output = tweet_df.merge(price, left_index= True, right_index= True)
print(output)
返回一个空的DF
Finish creating price
Empty DataFrame
Columns: [Tweet_content, Time, Date, Compound, Subjectivity, Polarity, Compound_analysis, Polarity_analysis, Open, High, Low, Close, Adj Close, Volume]
Index: []
Finish combining price and sentiment
回想起来,我必须为两个DFs编制索引以使其对齐,我运行了以下操作:
output = tweet_df.merge(price, left_index= True, right_index= True)
print(output)
print('Finish combining price and sentiment')
tweet_df.index = pd.to_datetime(tweet_df.index)
price.index = pd.to_datetime(price.index)
output = tweet_df.merge(price, left_index= True, right_index= True)
print(output)
这给了我一个错误:
TypeError: Cannot join tz-naive with tz-aware DatetimeIndex
有人知道如何解决这个问题吗?提前感谢:D首先,您必须确保这两个数据帧都有日期/时间值作为索引。如果其中一个不是datetime格式,则会出现错误 从这两个DataFame的数据样本中可以看出,DF1的行索引名为“Datetime”,这是正确的Datetime格式,而DF2的行索引是正常范围索引,即(0,1,2,…),DF2中的列
“Time”
只是一个数据列,而不是索引。在执行其他任务之前,需要将列“时间”
作为行索引,如下所示:
tweet_df = tweet_df.set_index('Time')
如果这仍然不起作用,那么可能您的一个数据帧是tz naive,而另一个是tz aware。通过在代码中将索引转换为日期时间格式,您可以尝试将其中任何一个转换为tz aware。例如:
tweet_df.index = pd.to_datetime(tweet_df.index).tz_localize('UTC') # or any other tz suitable for you
或者
我认为您应该指定右上、左上的参数。在这里查看有关合并的文档。请参阅下面我对您的问题的提示,这可能会找到您的错误源。无论如何,即使纠正了错误,您可能仍然需要对程序进行微调,因为从示例数据来看,您没有两个数据帧的公共数据可以按您期望的方式合并。我所说的没有公共数据是因为第一个数据帧的时间缩短到分钟,第二个数据帧的时间缩短到秒。因此,使用默认的内部联接,合并结果也可能为空。