5

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?

1 Answer 1

5

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

print (dw.set_value(pd.datetime(2015, 1, 19), 1))
DateTime
2015-01-18   -2374
2015-01-19       1
dtype: int64

More standard way is use ix or iloc:

print (dw)
            Count  DTW
DateTime              
2015-01-16     10    0
2015-01-17     28    0

dw.ix[1, 'DTW'] = 10
#dw.DTW.iloc[1] = 10
print (dw)
            Count  DTW
DateTime              
2015-01-16     10    0
2015-01-17     28   10
Sign up to request clarification or add additional context in comments.

5 Comments

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
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?
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?
Do you think something like dw.DTW.iloc[1] = 10 ?
For some reason that escaped me, Thank you @jezrael that worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.