0

I have a Numpy array of objects Equity, i.e.:

array([ Equity(24, symbol='AAPL', asset_name='APPLE INC', exchange='NASDAQ GLOBAL SELECT MARKET'),
   Equity(26578, symbol='GOOG_L', asset_name='GOOGLE INC', exchange='NASDAQ GLOBAL SELECT MARKET'),
   Equity(5061, symbol='MSFT', asset_name='MICROSOFT CORP', exchange='NASDAQ GLOBAL SELECT MARKET'),
   ...,
   Equity(20513, symbol='LOOK', asset_name='LOOKSMART LTD', exchange='NASDAQ CAPITAL MARKET', ),
   Equity(27133, symbol='WPCS', asset_name='WPCS INTERNATIONAL INC', exchange='NASDAQ CAPITAL MARKET'),
   Equity(27917, symbol='FREE', asset_name='FREESEAS INC', exchange='NASDAQ CAPITAL MARKET')], dtype=object)

The object Equity has the attribute exchange.

Which is the most concise method to get a subarray containing only Equity objects where exchange == 'NEW YORK STOCK EXCHANGE'?

Thanks!

1
  • When used like this a numpy array is basically the same as a Python list. There are few 'vectorized' operations that work on object elements. Commented Aug 5, 2015 at 18:27

1 Answer 1

1

Suppose the numpy array is named equity_array.

Solution 1:

Use list comprehension

np.array([eqt for eqt in equity_array if eqt.exchange == 'NEW YORK STOCK EXCHANGE'])

Solution 2:

Use Python build-in function filter

np.array(filter(lambda x: x.exchange == 'NEW YORK STOCK EXCHANGE', equity_array))

Notice that in Python 3 filter returns a iterator so it should be

np.array(list(filter(lambda x: x.exchange == 'NEW YORK STOCK EXCHANGE', equity_array)))
Sign up to request clarification or add additional context in comments.

Comments

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.