REF: Store metadata in an attrs dict by TomAugspurger · Pull Request #29062 · pandas-dev/pandas (original) (raw)
Also, what happens if you try to delete name from the attrs dict?
From a quick test, it seems this deletes the name (s.name
becomes None
); which I suppose is the expected behaviour?
But you can indeed set it to a non-hashable value that way, which doesn't directly raise an error, but can confusingly give errors in later operations:
In [9]: s = pd.Series([1, 2, 3], name='a')
In [10]: s.attrs
Out[10]: {'name': 'a'}
In [12]: s.attrs['name'] = [1, 2]
In [13]: s
Out[13]:
0 1
1 2
2 3
Name: [1, 2], dtype: int64
In [14]: s.to_frame()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-14-78ea8628cc3c> in <module>
----> 1 s.to_frame()
~/scipy/pandas/pandas/core/series.py in to_frame(self, name)
1533 """
1534 if name is None:
-> 1535 df = self._constructor_expanddim(self)
1536 else:
1537 df = self._constructor_expanddim({name: self})
~/scipy/pandas/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
456 data_columns = list(data.dtype.names)
457 data = {k: data[k] for k in data_columns}
--> 458 if columns is None:
459 columns = data_columns
460 mgr = init_dict(data, index, columns, dtype=dtype)
TypeError: unhashable type: 'list'