Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame({'P': [1, 2], 'Q': [0.7, 0.85]},
                  index=['p', 'q'])
df
Out[2]:
P Q
p 1 0.70
q 2 0.85
In [3]:
df.to_records()
Out[3]:
rec.array([('p', 1, 0.7 ), ('q', 2, 0.85)],
          dtype=[('index', 'O'), ('P', '<i8'), ('Q', '<f8')])

If the DataFrame index has no label then the recarray field name is set to ‘index’.
If the index has a label then this is used as the field name:

In [4]:
df.index = df.index.rename("I")
df.to_records()
Out[4]:
rec.array([('p', 1, 0.7 ), ('q', 2, 0.85)],
          dtype=[('I', 'O'), ('P', '<i8'), ('Q', '<f8')])

The index can be excluded from the record array:

In [5]:
df.to_records(index=False)
Out[5]:
rec.array([(1, 0.7 ), (2, 0.85)],
          dtype=[('P', '<i8'), ('Q', '<f8')])

Data types can be specified for the columns:

In [6]:
df.to_records(column_dtypes={"P": "int32"})
Out[6]:
rec.array([('p', 1, 0.7 ), ('q', 2, 0.85)],
          dtype=[('I', 'O'), ('P', '<i4'), ('Q', '<f8')])

As well as for the index:

In [7]:
df.to_records(index_dtypes="<S2")
Out[7]:
rec.array([(b'p', 1, 0.7 ), (b'q', 2, 0.85)],
          dtype=[('I', 'S2'), ('P', '<i8'), ('Q', '<f8')])
In [8]:
index_dtypes = "<S{}".format(df.index.str.len().max())
df.to_records(index_dtypes=index_dtypes)
Out[8]:
rec.array([(b'p', 1, 0.7 ), (b'q', 2, 0.85)],
          dtype=[('I', 'S1'), ('P', '<i8'), ('Q', '<f8')])