I have a numpy recarray, that has records of different data types or dtypes.
import numpy as np
a = np.array([1,2,3,4], dtype=int)
b = np.array([6,6,6,6], dtype=int)
c = np.array(['p', 'q', 'r', 's'], dtype=object)
d = np.array(['a', 'b', 'c', 'd'], dtype=object)
X = np.rec.fromarrays([a, b, c, d], names=['a', 'b', 'c', 'd'])
X
>>> rec.array([(1, 6, 'p', 'a'), (2, 6, 'q', 'b'), (3, 6, 'r', 'c'),
(4, 6, 's', 'd')],
dtype=[('a', '<i8'), ('b', '<i8'), ('c', 'O'), ('d', 'O')])
I tried to select records of object data type using select_dtypes, but I get a attribute error
X.select_dtypes(include='object')
>>>AttributeError: recarray has no attribute select_dtypes
Is there an equivalent of the select_dtype function for numpy recarrays where I can select columns of specific data type ?
recarray can access field as attribute or indexing:
testing the pandas approach:
Exploring the dtype:
Checking dtype by field:
So a list comprehension works:
df.select_dtypesis python code, but fairly complex, handling the include and exclude lists.