Skip to content

BUG: support for "level=" when reset_index() is called with a flat Index #16266

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 3 commits into from
May 6, 2017

Conversation

toobaz
Copy link
Member

@toobaz toobaz commented May 6, 2017

@jorisvandenbossche jorisvandenbossche added this to the 0.20.2 milestone May 6, 2017
Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Looks good

@@ -77,6 +77,7 @@ Conversion
Indexing
^^^^^^^^

- Bug in ``DataFrame.reset_index(level=)`` with flat index (:issue:`16263`)
Copy link
Member

Choose a reason for hiding this comment

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

Can you move this to the 0.20.2 file?

columns=['A', 'B', 'C', 'D'])

# With MultiIndex
for levels in ['A', 'B', 'E'], [0, 1, 2]:
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove the 'E' and 2 from the lists? I founded it a bit confusing as you don't use it

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, they were refactoring leftovers

@codecov
Copy link

codecov bot commented May 6, 2017

Codecov Report

Merging #16266 into master will decrease coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #16266      +/-   ##
==========================================
- Coverage   90.33%    90.3%   -0.04%     
==========================================
  Files         167      167              
  Lines       50907    50908       +1     
==========================================
- Hits        45989    45974      -15     
- Misses       4918     4934      +16
Flag Coverage Δ
#multiple 88.09% <100%> (-0.02%) ⬇️
#single 40.29% <88.88%> (-0.1%) ⬇️
Impacted Files Coverage Δ
pandas/core/frame.py 97.59% <100%> (-0.1%) ⬇️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/plotting/_converter.py 63.23% <0%> (-1.82%) ⬇️
pandas/_version.py 44.65% <0%> (+1.9%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e513430...d27ae91. Read the comment docs.

@codecov
Copy link

codecov bot commented May 6, 2017

Codecov Report

Merging #16266 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #16266      +/-   ##
==========================================
- Coverage   90.32%    90.3%   -0.02%     
==========================================
  Files         167      167              
  Lines       50907    50908       +1     
==========================================
- Hits        45982    45973       -9     
- Misses       4925     4935      +10
Flag Coverage Δ
#multiple 88.09% <100%> (-0.01%) ⬇️
#single 40.29% <88.88%> (-0.11%) ⬇️
Impacted Files Coverage Δ
pandas/core/frame.py 97.59% <100%> (-0.1%) ⬇️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/core/common.py 90.68% <0%> (-0.35%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ba60321...c301473. Read the comment docs.

@toobaz toobaz force-pushed the fix_flat_reset_level branch 2 times, most recently from fd83502 to f4c3121 Compare May 6, 2017 11:16
@@ -42,7 +42,7 @@ Conversion
Indexing
^^^^^^^^


- Bug in ``DataFrame.reset_index(level=)`` with flat index (:issue:`16263`)
Copy link
Contributor

Choose a reason for hiding this comment

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

single level index

@@ -641,6 +641,35 @@ def test_reset_index(self):
xp = xp.set_index(['B'], append=True)
assert_frame_equal(rs, xp, check_names=False)

def test_reset_index_level(self):
df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]],
columns=['A', 'B', 'C', 'D'])
Copy link
Contributor

Choose a reason for hiding this comment

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

issue number

Copy link
Member Author

Choose a reason for hiding this comment

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

the number is few lines after, it only applies to the flat index

result = df.set_index(['A', 'B']).reset_index(level=levels[0])
tm.assert_frame_equal(result, df.set_index('B'))

result = df.set_index(['A', 'B']).reset_index(level=levels[:1])
Copy link
Contributor

Choose a reason for hiding this comment

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

can u add similar tests for Series (in series test hierarchy)

Copy link
Member Author

Choose a reason for hiding this comment

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

done


result = df.set_index('A').reset_index(level=levels[:1])
tm.assert_frame_equal(result, df)

Copy link
Contributor

Choose a reason for hiding this comment

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

add with drop as well

Copy link
Member Author

Choose a reason for hiding this comment

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

done

with tm.assert_raises_regex(KeyError, 'Level E '):
df.set_index(idx_lev).reset_index(level=['A', 'E'])
with tm.assert_raises_regex(IndexError, 'Too many levels'):
df.set_index(idx_lev).reset_index(level=[0, 1, 2])
Copy link
Contributor

Choose a reason for hiding this comment

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

it might be easier to parameteize these tests (snd make the errors a separate test)

Copy link
Member Author

Choose a reason for hiding this comment

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

can you clarify?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this is fine.

@toobaz toobaz force-pushed the fix_flat_reset_level branch from f4c3121 to 7cc21c2 Compare May 6, 2017 11:34
@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels May 6, 2017
with tm.assert_raises_regex(KeyError, 'Level E '):
s.reset_index(level=['A', 'E'])

# With flat Index
Copy link
Contributor

Choose a reason for hiding this comment

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

don't use word flat, rather single level

@jreback
Copy link
Contributor

jreback commented May 6, 2017

minor comment, otherwise lgtm.

@jorisvandenbossche jorisvandenbossche merged commit 8809b04 into pandas-dev:master May 6, 2017
@jorisvandenbossche
Copy link
Member

Thanks!

@toobaz toobaz deleted the fix_flat_reset_level branch May 6, 2017 22:50
pawroman pushed a commit to pawroman/pandas that referenced this pull request May 9, 2017
pcluo pushed a commit to pcluo/pandas that referenced this pull request May 22, 2017
TomAugspurger pushed a commit to TomAugspurger/pandas that referenced this pull request May 29, 2017
TomAugspurger pushed a commit that referenced this pull request May 30, 2017
stangirala pushed a commit to stangirala/pandas that referenced this pull request Jun 11, 2017
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DataFrame.reset_index deletes index, does not all for ints as level arg
4 participants