Skip to content

BUG: Column indexing does not work in this case #5744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
socheon opened this issue Dec 19, 2013 · 2 comments · Fixed by #5745
Closed

BUG: Column indexing does not work in this case #5744

socheon opened this issue Dec 19, 2013 · 2 comments · Fixed by #5745
Labels
API Design Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@socheon
Copy link

socheon commented Dec 19, 2013

I am using Pandas 0.13.0rc1-23-g286811a, Python 2.7 and Windows XP.

The space in column names is apparently causing a problem. On IPython,

df = pd.DataFrame(columns=['a', 'b', 'c c'])
df['d'] = 3
df['c c']

Error

C:\python_envs\prod2\lib\site-packages\pandas\core\frame.pyc in __getitem__(self, key)
   1626             return self._getitem_multilevel(key)
   1627         else:
-> 1628             return self._getitem_column(key)
   1629
   1630     def _getitem_column(self, key):

C:\python_envs\prod2\lib\site-packages\pandas\core\frame.pyc in _getitem_column(self, key)
   1633         # get column
   1634         if self.columns.is_unique:
-> 1635             return self._get_item_cache(key)
   1636
   1637         # duplicate columns & possible reduce dimensionaility

C:\python_envs\prod2\lib\site-packages\pandas\core\generic.pyc in _get_item_cache(self, item)
    977         if res is None:
    978             values = self._data.get(item)
--> 979             res = self._box_item_values(item, values)
    980             cache[item] = res
    981             res._cacher = (item, weakref.ref(self))

C:\python_envs\prod2\lib\site-packages\pandas\core\frame.pyc in _box_item_values(self, key, values)
   1834             return self._constructor(values.T, columns=items, index=self.index)
   1835         else:
-> 1836             return self._box_col_values(values, items)
   1837
   1838     def _box_col_values(self, values, items):

C:\python_envs\prod2\lib\site-packages\pandas\core\frame.pyc in _box_col_values(self, values, items)
   1839         """ provide boxed values for a column """
   1840         return self._constructor_sliced.from_array(values, index=self.index,
-> 1841                                                    name=items, fastpath=True)
   1842
   1843     def __setitem__(self, key, value):

C:\python_envs\prod2\lib\site-packages\pandas\core\series.pyc in from_array(cls, arr, index, name, copy, fastpath)
    233             cls = SparseSeries
    234
--> 235         return cls(arr, index=index, name=name, copy=copy, fastpath=fastpath)
    236
    237     @property

C:\python_envs\prod2\lib\site-packages\pandas\core\series.pyc in __init__(self, data, index, dtype, name, copy, fastpath
)
    130             # data is an ndarray, index is defined
    131             if not isinstance(data, SingleBlockManager):
--> 132                 data = SingleBlockManager(data, index, fastpath=True)
    133             if copy:
    134                 data = data.copy()

C:\python_envs\prod2\lib\site-packages\pandas\core\internals.pyc in __init__(self, block, axis, do_integrity_check, fast
path)
   3413                 block = block[0]
   3414             if not isinstance(block, Block):
-> 3415                 block = make_block(block, axis, axis, ndim=1, fastpath=True)
   3416
   3417         else:

C:\python_envs\prod2\lib\site-packages\pandas\core\internals.pyc in make_block(values, items, ref_items, klass, ndim, dt
ype, fastpath, placement)
   1893
   1894     return klass(values, items, ref_items, ndim=ndim, fastpath=fastpath,
-> 1895                  placement=placement)
   1896
   1897

C:\python_envs\prod2\lib\site-packages\pandas\core\internals.pyc in __init__(self, values, items, ref_items, ndim, fastp
ath, placement)
   1297         super(ObjectBlock, self).__init__(values, items, ref_items, ndim=ndim,
   1298                                           fastpath=fastpath,
-> 1299                                           placement=placement)
   1300
   1301     @property

C:\python_envs\prod2\lib\site-packages\pandas\core\internals.pyc in __init__(self, values, items, ref_items, ndim, fastp
ath, placement)
     63         if len(items) != len(values):
     64             raise ValueError('Wrong number of items passed %d, indices imply '
---> 65                              '%d' % (len(items), len(values)))
     66
     67         self.set_ref_locs(placement)

ValueError: Wrong number of items passed 1, indices imply 0
@jreback
Copy link
Contributor

jreback commented Dec 19, 2013

this may not be doing what you expect, with current master
(this was just fixed very recently via #5723)

You setting with a scalar when their is no index, does not work (hmm...maybe I should raise on this)

In [1]: df = pd.DataFrame(columns=['a', 'b', 'c c'])

In [2]: df
Out[2]: 
Empty DataFrame
Columns: [a, b, c c]
Index: []

[0 rows x 3 columns]

This will now raise

In [3]: df['d'] = 3

Selection works fine

In [4]: df
Out[4]: 
Empty DataFrame
Columns: [a, b, c c]
Index: []

[0 rows x 4 columns]

In [5]: df['c c']
Out[5]: Series([], name: c c, dtype: object)

In [6]: pd.__version__
Out[6]: '0.13.0rc1-99-gbac7c6e'

@socheon
Copy link
Author

socheon commented Dec 19, 2013

Thanks for the fix! After upgrading, it works fine now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants