Examples
import numpy as np
import pandas as pd
df = pd.DataFrame({'P': [1, 2], 'Q': [0.7, 0.85]},
index=['p', 'q'])
df
df.to_records()
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:
df.index = df.index.rename("I")
df.to_records()
The index can be excluded from the record array:
df.to_records(index=False)
Data types can be specified for the columns:
df.to_records(column_dtypes={"P": "int32"})
As well as for the index:
df.to_records(index_dtypes="<S2")
index_dtypes = "<S{}".format(df.index.str.len().max())
df.to_records(index_dtypes=index_dtypes)