Setting dataframe name attribute

461 views
Skip to first unread message

Adam Hughes

unread,
Oct 14, 2012, 8:17:27 PM10/14/12
to pyd...@googlegroups.com
Hi guys,

I'm still using pandas-0.8.1-1, so I hope this is a version issue.

I noticed that if I instantiate a DataFrame with the name attribute, I get an attribute error; however I can set the name after instantiation.

df1=DataFrame((np.random.randn(10,10)), name='josh')
Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
TypeError: __init__() got an unexpected keyword argument 'name'

df1=DataFrame((np.random.randn(10,10)))
df1.name='josh'
df1.name
'josh'

Is this intended behavior or a bug?

Thanks

Wouter Overmeire

unread,
Oct 15, 2012, 5:45:49 AM10/15/12
to pyd...@googlegroups.com


2012/10/15 Adam Hughes <hughes...@gmail.com>


--
 
 

Unlike Series, DataFrame does not have a name argument in its default constructor, hence the exception.
Due to the dynamic nature of python you can set it after construction, but it will not behave like 'name' on Series. You can add whatever after construction

In [80]: df1 = pd.DataFrame(np.random.randn(10, 10))

In [81]: df1.magificent_song = 'Magnificent by U2'

BTW there is an open issue for having (like Series) a name for DataFrame: https://github.com/pydata/pandas/issues/447

Alvaro Tejero Cantero

unread,
Oct 15, 2012, 6:05:09 AM10/15/12
to pyd...@googlegroups.com
This reminds me of two issues related to DataFrame attributes

a)
df = pd.DataFrame(random.randn(20,4), columns=list('ABCD'))
df['name'] = df['A'] #someone adds a column for names
df.name = 'bastar' #attach a name unsuspectingly

df['name'] is now a column, obliterated by the string 'bastar'
df.name is the same


b)
In [11]: df.metastuff = {1: 'a', 'X': [1,2,3,4]}

In [12]: df.metastuff
Out[12]: {1: 'a', 'X': [1, 2, 3, 4]}

In [13]: st = pd.HDFStore('/tmp/attributes_test.ph5', 'w')

In [14]: st['df'] = df

In [15]: st.close()

In [16]: st = pd.HDFStore('/tmp/attributes_test.ph5', 'r')

In [17]: st['df'].metastuff
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-17-be4933f58476> in <module>()
----> 1 st['df'].metastuff

/home/x/Local/Envs/test/lib/python2.7/site-packages/pandas/core/frame.pyc
in __getattr__(self, name)
1769 return self[name]
1770 raise AttributeError("'%s' object has no attribute '%s'" %
-> 1771 (type(self).__name__, name))
1772
1773 def __setattr__(self, name, value):

AttributeError: 'DataFrame' object has no attribute 'metastuff'


HDF5 attributes are limited to 64kb but there must be a way to keep
some pieces of valuable metadata together with the Pandas objects when
serializing.


-á.
> --
>
>

Adam Hughes

unread,
Oct 15, 2012, 12:37:56 PM10/15/12
to pyd...@googlegroups.com
Thanks for the heads up.  I'll try to be more up on the github issues before posting to the list, sorry.

I upvote for this one.

--



Reply all
Reply to author
Forward
0 new messages