3

I have a dataframe indexed using a 12hr frequency datetime:

                    id  mm ls
date            
2007-09-27 00:00:00 1   0   0
2007-09-27 12:00:00 1   0   0
2007-09-28 00:00:00 1   15  0
2007-09-28 12:00:00 NaN NaN 0
2007-09-29 00:00:00 NaN NaN 0
Timestamp('2007-09-27 00:00:00', offset='12H')

I use column 'ls' as a binary variable with default value '0' using:

data['ls'] = 0

I have a list of days in the form '2007-09-28' from which I wish to update all 'ls' values from 0 to 1.

                    id  mm ls
date            
2007-09-27 00:00:00 1   0   0
2007-09-27 12:00:00 1   0   0
2007-09-28 00:00:00 1   15  1
2007-09-28 12:00:00 NaN NaN 1
2007-09-29 00:00:00 NaN NaN 0
Timestamp('2007-09-27 00:00:00', offset='12H')

I understand how this can be done using another column variable ie:

data.ix[data.id == '1'], ['ls'] = 1

yet this does not work using datetime index. Could you let me know what the method for datetime index is?

1
  • 1
    IIUC doesn't data.loc['2007-09-28','ls'] = 1 work? Commented Nov 13, 2015 at 15:07

1 Answer 1

2

You have a list of days in the form '2007-09-28':

days = ['2007-09-28', ...]

then you can modify your df using:

df['ls'][pd.DatetimeIndex(df.index.date).isin(days)] = 1
Sign up to request clarification or add additional context in comments.

1 Comment

This works, as does "EdChum" comment, yet this answer creates a copy of the df, rather than updating the existing df. I didn't specify in the question however.

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.