user1515250
user1515250

Reputation: 85

Propogation of _metadata from DataFrame to Series

I want to be able to store 'units' information for each column of a DataFrame and have the following code (uisng pandas v 0.14.1):

import pandas as pd
import numpy as np
import copy

# Monkey path Series:
pd.Series._metadata.append('units')

# Test it
df = pd.DataFrame(data=np.random.randn(5,2), columns=['A', 'B'])
df['A'].units = 's'
df['B'].units = 'm'

s = df['A']
print s.units # produces 's' as expected / desired

s = copy.copy(df['A'])
print s.units # crash, attribute does not exists
print s._metadata # produces ['name', 'units'] as expected

Apparently the metadata is not propagated properly. Is this a intentional or is this a bug? If intentional what could be done to make this work?

Upvotes: 1

Views: 205

Answers (1)

Jeff
Jeff

Reputation: 129018

copy.copy(...) currently works on pandas objects, but the mechanism doesn't preserve the metadata.

Here's an issue to implement this.

An easy workaround is simply to use s.copy() which will preserve the metadata.

Here is an issue to more fully document how to do this.

Upvotes: 1

Related Questions