gpt4 book ai didi

python - 类型错误 : cannot concatenate a non-NDFrame object

转载 作者:太空宇宙 更新时间:2023-11-03 14:51:48 28 4
gpt4 key购买 nike

我有这个 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>')

我想获取日期并将它们附加到我的 DataFrame df:

for i in xrange(0,5):
df.append(dates[i],ignore_index=True)

我收到此错误 TypeError: cannot concatenate a non-NDFrame object

更新:

df的示例数据:

Out[85]:
2017-06-05 -0.944868
2017-06-06 0.073623
2017-06-07 -0.687232
Freq: <DateOffset>, dtype: float64

最佳答案

如果df的长度与DatetimeIndex相同,需要创建index:

df.index = dates 

如果不尝试按 index 的长度与 df 的长度相同的长度进行过滤:

df.index = dates[:len(df.index)]

如果需要新列:

df['a'] = dates 

如果不是:

df['a'] = dates[:len(df.index)]

如果只需要使用前 5 个值:

df['a'] = dates[:5]

编辑:

我想你需要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

关于python - 类型错误 : cannot concatenate a non-NDFrame object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45167175/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com