2

I would like to transform the date level in my multi index but I am not sure how to access that one level without affecting the other level. This is what I am doing:

pipeline_output.index=pipeline_output.index.get_level_values(0).tz_convert('US/Eastern')

This removes the other index obviously so I am left only with date. Is there a way to change this, so that this transformation only applies to level=0?

Another approach I took was this:

 pipeline_output.index.get_level_values(0)=pipeline_output.index.get_level_values(0).tz_convert('US/Eastern')

Which produces an error, but it seems to work on the right hand side but not on the left hand side.

EDIT: The approach below has worked (thanks Peter for the suggestion):

pipeline_output.index=pipeline_output.index.set_levels(pipeline_output.index.levels[0].tz_convert('US/Eastern'),level=0)

Tartaglia
  • 949
  • 14
  • 20
  • You might try messing around with the `set_levels` or `set_labels` methods. – lmo Jun 09 '19 at 15:33
  • those seem to be more for changing the name of the index column. I am trying to indicate that I would like to change only level 0, which works on the right hand side but not on the left hand side. – Tartaglia Jun 09 '19 at 15:52
  • might it be possible just to use reset_index() > transform > set_index() – Raphael Jun 09 '19 at 15:58
  • @Tartaglia, looks like `set_levels` is one way: https://stackoverflow.com/questions/26629095/multiindex-and-timezone-frozen-list-error/26629643#26629643 Or you can rebuild the MultiIndex and apply your changes in a list comprehension: https://stackoverflow.com/questions/29150346/pandas-modify-a-particular-level-of-multiindex/41829331 – Peter Leimbigler Jun 09 '19 at 15:59
  • Raphael, I thought about that too, but that seems cumbersome, shouldn't it be possible to do that directly? – Tartaglia Jun 09 '19 at 16:03
  • Besides, the transform does only work on indexes it seems. – Tartaglia Jun 09 '19 at 16:09
  • Peter, thanks for the suggestion. I took the set_levels approach and worked!!! I am posting it below my original post for future reference. – Tartaglia Jun 10 '19 at 02:49
  • Tartaglia or @Peter either of you want to answer this? I had a similar problem, solved it through the links Peter gave, went back to answer and saw the EDIT at bottom of question. So now I feel cheeky answering and claiming my points, feels like stealing but happy to do it if neither of you want to... – scatter Jul 31 '19 at 16:22

0 Answers0