1

This question was inspired by this question. I had the same problem, updating a MultiIndex DataFrame by selection. The drop_level=False solution in Pandas 0.13 will allow me to achieve the same result, but I am still wondering why I cannot get a view from the MultiIndex DataFrame. In other words, why does this not work?:

>>> sat = d.xs('sat', level='day', copy=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2248, in xs
    raise ValueError('Cannot retrieve view (copy=False)')
ValueError: Cannot retrieve view (copy=False)

Of course it could be only because it is not implemented, but is there a reason? Is it somehow ambiguous or impossible to implement? Returning a view is more intuitive to me than returning a copy then later updating the original. I looked through the source and it seems this situation is checked explicitly to raise an error.

Alternatively, is it possible to get the same sort of view from any of the other indexing methods? I've experimented but have not been successful.

[edit] Some potential implementations are discussed here. I guess with the last question above I'm wondering what the current best solution is to index into arbitrary multiindex slices and cross-sections.

Community
  • 1
  • 1
ontologist
  • 595
  • 4
  • 20

1 Answers1

0

on a multi-dtype frame this is not possible at all; even a single dtyped frame it is up 2 numpy, which makes it non-deterministic.

This is under consideration to basically always return a copy even if a view is possible using xs. the problem is that it is very easy to try to modify this thinking that you are actually modifying the underlying data when in fact you are working on a copy (or the reverse could be true).

(fyi this error is removed in 0.13 as well and will instead raise/warn if you try to modify it)

see this here for the change.

Jeff
  • 125,376
  • 21
  • 220
  • 187
  • Just to make sure I understand correctly: you're saying it's not possible because of low-level implementation details? While we're on the topic - is there any discussion to allow indexing by dict, with keys being the index label? My code is set up to take a dict from the user but I have to do some ugly transformations to turn into a tuple of indices and a list of labels. As is there doesn't seem to be an elegant way to work with subsets of MultiIndex DataFrames. – ontologist Nov 04 '13 at 02:05
  • a view is essentially a start/stop pointer to another data block. it's in general not possible to do this with arbitrary (fancy) indexing which is essentially what u r asking. as 2 your second question I am not sure what u r asking. – Jeff Nov 04 '13 at 02:45
  • Thanks for your answer, that makes sense. I've been searching on the topic and I think my second question is referring to the issues brought up in [this enhancement request](https://github.com/pydata/pandas/issues/4036). Since it seems to be a matter of implementation I'll be following pandas and hopefully someday I'll have the skills to contribute myself. – ontologist Nov 04 '13 at 03:47