2

I have a numpy array containing date (as datetime object), prices and a category as an integer:

array([[datetime.date(2013, 6, 5), 11.42, 1],
   [datetime.date(2013, 6, 7), 63.97, 1],
   [datetime.date(2013, 6, 19), 3.92, 1],
   [datetime.date(2013, 6, 19), 16.25, 2],
   [datetime.date(2013, 6, 20), 11.0, 2],
   [datetime.date(2013, 6, 22), 32.72, 2],
   [datetime.date(2013, 6, 25), 16.6, 3],
   [datetime.date(2013, 6, 26), 2.95, 2],
   [datetime.date(2013, 7, 1), 6.27, 1],
   [datetime.date(2013, 7, 1), 2.95, 1]], dtype=object)

Suming up the prices if a category is met via index_cat=(array==2).any(axis=1), followed by by np.sum(array[index_cat][:,1]) is obvious.

What I want to achieve now is basically the same, but instead of selecting a category, I want to take the datetime object's month or month-year combination as a criterion.

So I thought a index_june=(array==datetime.dateime(month='06').any(axis=1) should do it, but, even with lots of searching, I could not find a way to do this.

So, how do I express this to match the datetime object considering the absence of wildcards?

Thanks a lot in advance!

1 Answer 1

1

You can use numpy's vectorize function:

getmonth = np.vectorize(lambda d: getattr(d, "month"))
ind = getmonth(arr[:,0]) == 11

another way is list comprehension:

ind = np.array([a[0].month == 11 for a in arr])
Sign up to request clarification or add additional context in comments.

2 Comments

Great! Thank you for the quick and clear answer! I prefer the list comprehension way as it suits my python-knowledge better (for now)..
It also seems to be slightly faster.

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.