Skip to content

Series.reset_index not working with "inplace=True" #2277

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
dvic opened this issue Nov 17, 2012 · 4 comments
Closed

Series.reset_index not working with "inplace=True" #2277

dvic opened this issue Nov 17, 2012 · 4 comments
Labels
Milestone

Comments

@dvic
Copy link

dvic commented Nov 17, 2012

When I call reset_index on a Series object with arguments inplace=True, it does not work. See the code example below. Am I missing something here? (I am using "0.9.0", checked with pandas.version)

ts_a = pandas.Series([1]*3+[2]*3+[3]*4, range(10))
ts_b = pandas.Series([2]*3+[3]*3+[1]*4, range(10))
ts_c = pandas.Series(numpy.random.randn(10), range(10))
df = pandas.DataFrame({"A": ts_a, "B": ts_b, "C":ts_c})
df = df.set_index(['A', 'B'])['C'] # I select column C here to have a Series object
# print df
# A  B
#1  2    1.032
#    2    0.139
#    2   -0.708
#2  3   -1.268
#    3    0.138
#    3    0.497
#3  1    0.973
#    1   -0.634
#    1   -0.112
#    1   -0.997

# now let's reset index 'B': inplace does not work?
df.reset_index('B', inplace=True)
# print df
# A  B
#1  2    1.032
#    2    0.139
#    2   -0.708
#2  3   -1.268
#    3    0.138
#    3    0.497
#3  1    0.973
#    1   -0.634
#    1   -0.112
#    1   -0.997

# when copied, it does work
df = df.reset_index('B')
# print df
# Name: C
#    B      C
# A          
#1  2  1.032
#1  2  0.139
#1  2 -0.708
#2  3 -1.268
#2  3  0.138
#2  3  0.497
#3  1  0.973
#3  1 -0.634
#3  1 -0.112
#3  1 -0.997
@changhiskhan
Copy link
Contributor

Here df is a Series but df.reset_index('B') is a DataFrame.
There's no way to turn df from a Series into a DataFrame so inplace for Series only takes effect if "drop=True" and the result stays a Series.

@dvic
Copy link
Author

dvic commented Nov 17, 2012

Alright, I understand now. So I guess it is a choice not to convert a Series object to a DataFrame object.

But why does a DataFrame object get converted to a Series object when I just select 1 column? For example if df is a DataFrame object, then df["A"] would give a Series object, right? This made me think that reset_index would convert a Series object to a DataFrame object.

@changhiskhan
Copy link
Contributor

inplace modifies the internals of self.

df["A"] returns a Series but it doesn't convert df into a DataFrame.

reset_index can indeed return a DataFrame object, but it doesn't make sense to change its type from Series to DataFrame.

@wesm wesm closed this as completed in 7a76656 Nov 23, 2012
@wesm
Copy link
Member

wesm commented Nov 23, 2012

I added a more helpful error message

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

No branches or pull requests

3 participants