添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to add the row-wise result from a function into my dataframe using df.set_Value .

df in the format :

    Count   DTW
DateTime        
2015-01-16  10  0
2015-01-17  28  0

Using df.setValue

dw.set_Value(idx, 'col', dtw) # idx and dtw are int values
TypeError: cannot insert DatetimeIndex with incompatible label

How do I solve this error or what alternative method with comparable efficiency is there?

I think you have Series, not DataFrame, so use Series.set_value with index converted to datetime

dw = pd.Series([-2374], index = [pd.to_datetime('2015-01-18')])
dw.index.name = 'DateTime'
print (dw)
DateTime
2015-01-18   -2374
dtype: int64
print (dw.set_value(pd.to_datetime('2015-01-19'), 1))
DateTime
2015-01-18   -2374
2015-01-19       1
dtype: int64
                I'm getting the idx value from a range so I wanted to allocate it directly using an int value, I think this created a problem with the index as it is not in numerical order but is instead a DateTime index. I hope this clarified my question
– SerialDev
                Aug 19, 2016 at 14:02
                So do you have first index with int and datetime values? Or need overwrite some value, e.g. 28 in sample? Or add new row?
– jezrael
                Aug 19, 2016 at 14:04
                I want to set a value on an existing row that has a datetime index, is there an iterable that I could use to get datetime index as opposed to int?
– SerialDev
                Aug 19, 2016 at 14:10
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.