TypeError: cannot concatenate a non-NDFrame object

江枫思渺然 提交于 2019-12-12 12:57:25

问题


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:

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

I get this error TypeError: cannot concatenate a non-NDFrame object.

UPDATE:

sample data of df:

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

回答1:


If length of df is same as DatetimeIndex and need create index:

df.index = dates 

If not try filter by length of index what is same as length of 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:

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

EDIT:

I think you need union for concatenate index to dates and then 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


来源:https://stackoverflow.com/questions/45167175/typeerror-cannot-concatenate-a-non-ndframe-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!