How to handle meta data associated with a pandas dataframe?
<p>What is the best practice for saving meta information to a dataframe? I know of the following coding practice</p>

<pre><code>import pandas as pd
df = pd.DataFrame([])
df.currency = 'USD'
df.measure = 'Price'
df.frequency = 'daily'
</code></pre>

<p>But as stated in this post <a href="http://stackoverflow.com/questions/14688306/adding-meta-information-metadata-to-pandas-dataframe">Adding meta-information/metadata to pandas DataFrame</a> this is associated with the risk of losing the information by appling functions such as "groupby, pivot, join or loc" as they may return "a new DataFrame without the metadata attached". </p>

<p>Is this still valid or has there been an update to meta information processing in the meantime? What would be an alternative coding practice?</p>

<p>I do not think building a seperate object is very suitable. Also working with Multiindex does not convince me. Lets say I want to divide a dataframe with prices by a dataframe with earnings. Working with Multiindices would be very involved.</p>

<pre><code>#define price DataFrame
p_index = pd.MultiIndex.from_tuples([['Apple', 'price', 'daily'],['MSFT', 'price', 'daily']])
price = pd.DataFrame([[90, 20], [85, 30], [70, 25]], columns=p_index)

# define earnings dataframe
e_index = pd.MultiIndex.from_tuples(
    [['Apple', 'earnings', 'daily'], ['MSFT', 'earnings', 'daily']])
earnings=pd.DataFrame([[5000, 2000], [5800, 2200], [5100, 3000]], 
                columns=e_index)

price.divide(earnings.values, level=1, axis=0)
</code></pre>

<p>In the example above I do not even ensure that the company indices really match. I would probably need to invoke a pd.DataFrame.reindex() or similar. This cannot be a good coding practice in my point of view.</p>

<p>Is there a straightforward solution to the problem of handling meta information in that context that I don't see?</p>

<p>Thank you in advance</p>

<p>I think that MultiIndexes is the way to go, but this way:</p>

<pre><code>daily_price_data = pd.DataFrame({'Apple': [90, 85, 30], 'MSFT':[20, 30, 25]})
daily_earnings_data = pd.DataFrame({'Apple': [5000, 58000, 5100], 'MSFT':[2000, 2200, 3000]})
data = pd.concat({'price':daily_price_data, 'earnings': daily_earnings_data}, axis=1)
data


    earnings        price
    Apple   MSFT    Apple   MSFT
0   5000    2000    90      20
1   58000   2200    85      30
2   5100    3000    30      25
</code></pre>

<p>Then, to divide:</p>

<pre><code>data['price'] / data['earnings']
</code></pre>

<p>If you find that your workflow makes more sense to have companies listed on the first level of the index, then <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.xs.html" rel="nofollow">pandas.DataFrame.xs</a> will be very helpful:</p>

<pre><code>data2 = data.reorder_levels([1,0], axis=1).sort_index(axis=1)
data2.xs('price', axis=1, level=-1) / data2.xs('earnings', axis=1, level=-1)
</code></pre>

python pandas metadata finance divide