Ma Ming
Ma Ming

Reputation: 1895

how to query the memory layout of pandas.dataframe

I want to to query the memory layout of a pandas.dataframe. More explicitly, given a dataframe df (say, of the type np.float32), I would like to known if it is column-contiguous or row-contiguous.

Upvotes: 1

Views: 779

Answers (1)

Haleemur Ali
Haleemur Ali

Reputation: 28303

you can examine the flags attribute of the underlying numpy array. The underlying numpy array can be accessed through the pd.DataFrame.values

example:

import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.random(12).reshape(4,3), columns=list('abc'))
df.values.flags
#outputs:
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False

As you can see from the output, in this case the data is row-contiguous (C_CONTINUOUS). F_CONTINUOUS signifies that the data is column-contiguous

Upvotes: 1

Related Questions