Skip to content

BUG/ENH: should .ix allow partial setting? #2578

Closed
@jreback

Description

@jreback

related to GH #2033

In [27]: df = pd.DataFrame(np.random.rand(4,2),index=pd.date_range('2001/1/12',periods=4),columns=['A','B'])

In [28]: df
Out[28]: 
                   A         B
2001-01-12  0.861201  0.530351
2001-01-13  0.434518  0.692022
2001-01-14  0.794237  0.912073
2001-01-15  0.421282  0.930005

In [29]: df.ix[:,'B'] = df.ix[:,'A'].copy()

In [30]: df.ix[:,'C'] = df.ix[:,'A'].copy()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-30-2107107d0bd8> in <module>()
----> 1 df.ix[:,'C'] = df.ix[:,'A'].copy()


/usr/local/lib/python2.7/site-packages/pandas-0.10.0-py2.7-linux-x86_64.egg/pandas/core/index.pyc in get_loc(self, key)
    714         loc : int if unique index, possibly slice or mask if not
    715         """
--> 716         return self._engine.get_loc(key)
    717 
    718     def get_value(self, series, key):

KeyError: 'C'

of course in this case df['C'] = df.ix[:,'A'].copy() accomplishes this

Panel example: have to transpose, setitem, then transpose back to accomplish this

In [46]: p = pd.Panel(np.random.rand(2,4,2),items=['Item1','Item2'],major_axis=pd.date_range('2001/1/12',periods=4),minor_axis=['A','B'])

In [47]: p
Out[47]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 4 (major_axis) x 2 (minor_axis)
Items axis: Item1 to Item2
Major_axis axis: 2001-01-12 00:00:00 to 2001-01-15 00:00:00
Minor_axis axis: A to B

In [48]: pt = p.transpose(2,1,0)

In [49]: pt
Out[49]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 4 (major_axis) x 2 (minor_axis)
Items axis: A to B
Major_axis axis: 2001-01-12 00:00:00 to 2001-01-15 00:00:00
Minor_axis axis: Item1 to Item2

In [50]: pt['C'] = p.ix[:,:,'B'].copy()

In [51]: pt.transpose(2,1,0)
Out[51]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 4 (major_axis) x 3 (minor_axis)
Items axis: Item1 to Item2
Major_axis axis: 2001-01-12 00:00:00 to 2001-01-15 00:00:00
Minor_axis axis: A to C

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugEnhancementIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions