0

I have created a DataFrame for keeping energy logger data. Right now this DataFrame is only storing some random numbers. As index I use a pandas.core.indexes.datetimes.DatetimeIndex. With the weekday attribute I can figure out the corresponding weekday (0 for monday, 1 for tuesday and so on...).

I don't expect there to be any energy consumption on weekends. My correspondign code looks about:

# weekday > 4 => saturday and sunday
df.loc[df.index.weekday > 4, 'power'] = 0

This works fine. But let's say, there is no consumption on wednesday and thursday. I would expect the corresponding code to look like:

df.loc[(df.index.weekday == 2 or df.index.weekday == 3), 'power'] = 0

This doesn't work. I get the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Ok, pandas hints me at these methods any and all. But as far as I understand, they don't do what I need - or at least I don't know how to use them for my needs.

QUESTION:

Does anybody know, how to get such a DataFrame slice determined by some boolean condition?

Many thanks in advance!

P.S.:

I have figured out a solution - but it's very uncomfortable:

df.loc[df.index.weekday == 2, 'power'] = 0
df.loc[df.index.weekday == 3, 'power'] = 0

Just imagine, I want to do the same thing for a couple of hours or minutes instead of days. There has to be an easier way to do this.

1 Answer 1

1

Combinations of conditions in these cases have to be joined by & (AND) or | (OR) and the single conditions have to be put in parentheses.

df.loc[(df.index.weekday == 2) | (df.index.weekday == 3), 'power'] = 0

should work

Edit based on comment: This can be extended to more days by using the isin(list) method: df.loc[(df.index.weekday.isin([3,4,5])), 'power'] = 0

Sign up to request clarification or add additional context in comments.

3 Comments

But still a lot of typing, if I want to do this for more days. Is there another more elegant solution?
Glad to hear that. Feel free to upvote and accept my answer. Yes, you can extend this by using df.index.weekday.isin([list of days]) as your condition
PERFECT! :) Thank you!

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.