Skip to content

Setting with enlargement fails in presence of MultiIndex #9250

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
FedericoV opened this issue Jan 14, 2015 · 6 comments · Fixed by #9256
Closed

Setting with enlargement fails in presence of MultiIndex #9250

FedericoV opened this issue Jan 14, 2015 · 6 comments · Fixed by #9256
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Milestone

Comments

@FedericoV
Copy link

In [1]:

import pandas as pd
import numpy as np

print pd.__version__

a = np.linspace(0, 10, 11)
idx = [('test1', i) for i in range(5)]
idx.extend([('test2', i) for i in range(6)])

idx = pd.MultiIndex.from_tuples(idx)
0.15.2-80-ge69deae
In [2]:

a = pd.Series(a, idx)
a
Out[2]:
test1  0     0
       1     1
       2     2
       3     3
       4     4
test2  0     5
       1     6
       2     7
       3     8
       4     9
       5    10
dtype: float64
In [3]:

a.loc[('test1', 2)] = 13
a
Out[3]:
test1  0     0
       1     1
       2    13
       3     3
       4     4
test2  0     5
       1     6
       2     7
       3     8
       4     9
       5    10
dtype: float64
In [4]:

a.loc[('test', 17)] = 13
---------------------------------------------------------------------------
IndexingError                             Traceback (most recent call last)
<ipython-input-4-157acf13152f> in <module>()
----> 1 a.loc[('test', 17)] = 13

/home/federico/Python_Libraries/pandas/pandas/core/indexing.pyc in __setitem__(self, key, value)
    114                 if len(key) > self.ndim:
    115                     raise IndexingError('only tuples of length <= %d supported' %
--> 116                                         self.ndim)
    117                 indexer = self._convert_tuple(key, is_setter=True)
    118             else:

IndexingError: only tuples of length <= 1 supported

In [5]:

a.ix[('test', 17)] = 13
---------------------------------------------------------------------------
IndexingError                             Traceback (most recent call last)
<ipython-input-5-89467846f8af> in <module>()
----> 1 a.ix[('test', 17)] = 13

/home/federico/Python_Libraries/pandas/pandas/core/indexing.pyc in __setitem__(self, key, value)
    114                 if len(key) > self.ndim:
    115                     raise IndexingError('only tuples of length <= %d supported' %
--> 116                                         self.ndim)
    117                 indexer = self._convert_tuple(key, is_setter=True)
    118             else:

IndexingError: only tuples of length <= 1 supported

Somehow - the enlarging fails. Any clue how to get it to work? I have also noticed that the 'fast' indexing methods, iat and at fail with DataFrames that are multi indexed.

@jreback
Copy link
Contributor

jreback commented Jan 14, 2015

pls pd.show_versions()

@FedericoV
Copy link
Author

I will do that asap tomorrow, since it was on my machine at work and I
cannot SSH. However, it was the very last version, pulled from master and
built ~1 minute before posting that issue. I did print pd.version though - so it was: 0.15.2-80-ge69deae

On Wed, Jan 14, 2015 at 5:51 PM, jreback [email protected] wrote:

pls pd.show_versions()


Reply to this email directly or view it on GitHub
#9250 (comment).

@jreback
Copy link
Contributor

jreback commented Jan 15, 2015

hmm, this should work. I'll mark it as an enhancement. pull-requests are welcome. You can of course use append to achieve what you want.

iat/at should work. pls post an example if that is not the case.

@jreback jreback added this to the 0.16.0 milestone Jan 15, 2015
@jreback jreback added the Indexing Related to indexing on series/frames, not to indexes themselves label Jan 15, 2015
@FedericoV
Copy link
Author

INSTALLED VERSIONS

commit: None
python: 2.7.8.final.0
python-bits: 64
OS: Linux
OS-release: 3.13.0-44-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.15.2-80-ge69deae
nose: 1.3.4
Cython: 0.21
numpy: 1.10.0.dev-d98e57c
scipy: 0.15.0.dev-c3d8771
statsmodels: 0.5.0
IPython: 2.3.0
sphinx: 1.3a0
patsy: 0.2.1
dateutil: 2.2
pytz: 2014.4
bottleneck: None
tables: 3.1.0
numexpr: 2.3.1
matplotlib: 1.3.1
openpyxl: 1.8.2
xlrd: 0.9.3
xlwt: 0.7.5
xlsxwriter: 0.5.2
lxml: 3.3.1
bs4: 4.3.1
html5lib: None
httplib2: 0.9
apiclient: None
rpy2: 2.3.6
sqlalchemy: 0.9.2
pymysql: None
psycopg2: None

On Thu, Jan 15, 2015 at 1:50 AM, jreback [email protected] wrote:

hmm, this should work. I'll mark it as an enhancement. pull-requests are
welcome. You can of course use append to achieve what you want.

iat/at should work. pls post an example if that is not the case.


Reply to this email directly or view it on GitHub
#9250 (comment).

@FedericoV
Copy link
Author

I didn't want to use append because I was using the dataframe as a structure to hold some simulation within a loop, and I wanted to avoid extra copies if possible.

I'm happy to contribute tests, but I don't know enough about Pandas internals to fix it myself... I can try to take a look though.

@jreback
Copy link
Contributor

jreback commented Jan 15, 2015

it's going to copy with enlargement in either case - you never should do this in a loop

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

Successfully merging a pull request may close this issue.

2 participants