我试图通过日期列将python pandas中的两个数据帧连接在一起。
如何加入两个数据帧:df和时间戳?
下面给出了这个错误。我试图将df的日期列和dt数据帧命名为“Date”,以便我可以加入它们。
File "C:\Program Files\Python36-32\lib\site-packages\pandas\core\frame.py", line 4767, in join
rsuffix=rsuffix, sort=sort)
File "C:\Program Files\Python36-32\lib\site-packages\pandas\core\frame.py", line 4797, in _join_compat
can_concat = all(df.index.is_unique for df in frames)
File "C:\Program Files\Python36-32\lib\site-packages\pandas\core\frame.py", line 4797, in <genexpr>
can_concat = all(df.index.is_unique for df in frames)
AttributeError: 'Timestamp' object has no attribute 'index'
我尝试了另一种连接方式来更改时间戳数据帧 到非时间戳日期框架。它不起作用。
这是我的代码:
import pandas as pd
# vars section
fields = ['Date','Adj Close']
# setup the dataframe
df = pd.read_csv("SRS.csv", na_values=["0"],
parse_dates=["Date"])
df.to_csv('new.csv', index=True, columns=fields)
#data cleaner
df = df.fillna(0)
df = df[df.Date != "1970-01-01"]
# set the dataframes
dt = pd.date_range("08-11-2014","08-27-2014", index=True)
idx = pd.DatetimeIndex(dt, index=True)
# print the dataframes
print(df)
print(idx)
print(dt)
# join the dataframes
df2 = df.join(dt, how='inner')