Python 类型错误:无法连接非 NDFrame 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45167175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: cannot concatenate a non-NDFrame object
提问by user7379562
I have this DatetimeIndex:
我有这个日期时间索引:
dates = DatetimeIndex(['2017-06-09', '2017-06-10', '2017-06-11', '2017-06-12',
'2017-06-13', '2017-06-14'],
dtype='datetime64[ns]', freq='<DateOffset>')
I want to take dates and append them to my DataFrame df:
我想获取日期并将它们附加到我的 DataFrame 中df:
for i in xrange(0,5):
df.append(dates[i],ignore_index=True)
I get this error TypeError: cannot concatenate a non-NDFrame object.
我收到此错误TypeError: cannot concatenate a non-NDFrame object。
UPDATE:
更新:
sample data of df:
样本数据df:
Out[85]:
2017-06-05 -0.944868
2017-06-06 0.073623
2017-06-07 -0.687232
Freq: <DateOffset>, dtype: float64
采纳答案by jezrael
If length of dfis same as DatetimeIndexand need create index:
如果长度df相同DatetimeIndex并且需要创建index:
df.index = dates
If not try filter by length of indexwhat is same as length of df:
如果不尝试按与长度index相同的长度过滤df:
df.index = dates[:len(df.index)]
If need new column:
如果需要新列:
df['a'] = dates
If not:
如果不:
df['a'] = dates[:len(df.index)]
If need use only first 5 values:
如果只需要使用前 5 个值:
df['a'] = dates[:5]
EDIT:
编辑:
I think you need unionfor concatenate index to datesand then reindex:
我认为您需要union将索引连接到dates然后reindex:
df = df.reindex(df.index.union(dates), fill_value=-0.944868)
print (df)
2017-06-05 -0.944868
2017-06-06 0.073623
2017-06-07 -0.687232
2017-06-09 -0.944868
2017-06-10 -0.944868
2017-06-11 -0.944868
2017-06-12 -0.944868
2017-06-13 -0.944868
2017-06-14 -0.944868
Name: <DateOffset>, dtype: float64

