Skip to content

ENH/API: GH2578, allow ix and friends to partially set when the key is not contained in the object #4515

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

Merged
merged 2 commits into from
Sep 5, 2013

Conversation

jreback
Copy link
Contributor

@jreback jreback commented Aug 8, 2013

closes #2578

On a series, this is kind of like an inplace append operation (note the dtype change - on purpose)

In [6]: s = Series([1,2,3])

In [7]: s[4] = 5.
In [3]: s
Out[3]: 
0    1
1    2
2    3
4    5
dtype: float64

This is now allowed: (previously would KeyError), you first have to create the column C, then set it

In [1]: df = DataFrame(np.arange(6).reshape(3,2),columns=['A','B'])

In [3]: df
Out[3]: 
   A  B
0  0  1
1  2  3
2  4  5

In [4]: df.ix[:,'C'] = df.ix[:,'A']

In [5]: df
Out[5]: 
   A  B  C
0  0  1  0
1  2  3  2
2  4  5  4

This would previously raise an IndexError

In [4]: df.loc[3] = 5

In [5]: df
Out[5]: 
   A  B  C
0  0  1  0
1  2  3  2
2  4  5  4
3  5  5  5

and on Panel:

In [6]: p = Panel(np.arange(16).reshape(2,4,2),
   items=['Item1','Item2'],
   major_axis=pd.date_range('2001/1/12',periods=4),
   minor_axis=['A','B'],dtype='float64')

In [7]: p
Out[7]: 
<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 [10]: p.loc[:,:,'C'] = Series([30,32],index=p_orig.items)

In [11]: p
Out[11]: 
<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

In [13]: p.loc[:,:,'C']
Out[13]: 
            Item1  Item2
2001-01-12     30     32
2001-01-13     30     32
2001-01-14     30     32
2001-01-15     30     32

@jreback
Copy link
Contributor Author

jreback commented Aug 24, 2013

@jtratner @hayd @cpcloud @wesm thoughts when you have a chance

@jtratner
Copy link
Contributor

I guess I wonder what should happen in these kinds of cases cases - any magic?

df.loc[5] = 3
s[6] = 5

@hayd
Copy link
Contributor

hayd commented Aug 24, 2013

looks good.

@jreback
Copy link
Contributor Author

jreback commented Aug 25, 2013

@jtratner

these are straightforward

In [1]: s = Series([1,2,3])

In [2]: s[6] = 5

In [3]: s
Out[3]: 
0    1
1    2
2    3
6    5
dtype: int64

In [4]: df = DataFrame(np.arange(6).reshape(3,2),columns=['A','B'])

In [5]: df.loc[5] = 3

In [6]: df
Out[6]: 
   A  B
0  0  1
1  2  3
2  4  5
5  3  3

I have disabled both iloc/iat from being able to enlarge, as would require filling in the intermediary
index with something; which potentially is ambiguous.

In [7]: df.iloc[5] = 3
IndexError: iloc cannot enlarge its target object

@jreback
Copy link
Contributor Author

jreback commented Aug 26, 2013

@wesm take a look? this is non-trivial but you were already going it bith set_value in any event, this just generalizes (and with series refactor this is able to be done in-place, which makes pretty natural)

@jreback
Copy link
Contributor Author

jreback commented Aug 31, 2013

any comments before merging?

CLN: set_value in Series/Frame now go thru indexing routings in core/indexing.py
jreback added a commit that referenced this pull request Sep 5, 2013
ENH/API:  GH2578, allow ix and friends to partially set when the key is not contained in the object
@jreback jreback merged commit 3a2fe0b into pandas-dev:master Sep 5, 2013
@@ -828,6 +990,9 @@ def _has_valid_type(self, key, axis):

return isinstance(key, slice) or com.is_integer(key) or _is_list_like(key)

def _has_valid_setitem_indexer(self, indexer):
self._has_valid_positional_setitem_indexer(indexer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback is this supposed to return None?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no...they will just raise if needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG/ENH: should .ix allow partial setting?
3 participants