0

Let's say we have a dataframe with a 10 second interval timeseries. We want to assign a value to a new column when:

  1. the minutes are 1 and 5 ( 00:01:00, 00:05:00)
  2. the seconds are 0, so that only the first row with minute 1 and 5 are considered

I'm already failing by filtering only two conditions, minute 1 and second 0:

df.something = np.where(df.index.second == 0 and df.index.minute == 1, 'some_value', np.nan)

this leads to an error:

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

If I use any() or all() in all combination it still doesn't work. Either nothing will be assigned to the column or all rows have 'some_value'

How to achieve this?

my desired output is:

datetime               something
2022-02-01 00:00:50    NaN
2022-02-01 00:01:00    some_value
2022-02-01 00:01:10    NaN
2022-02-01 00:01:20    NaN
...
2022-02-01 00:04:50    NaN
2022-02-01 00:05:00    some_value
2022-02-01 00:05:10    NaN
....
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
stanvooz
  • 522
  • 3
  • 19
  • 3
    use `&` instead of `and` in the conditions, and don't forget proper parenthesis. – Quang Hoang May 12 '22 at 15:34
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – Ynjxsjmh May 12 '22 at 15:47

0 Answers0