Skip to content

API/BUG: Index creation with named Index resets name #11193

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
sinhrks opened this issue Sep 26, 2015 · 12 comments
Closed

API/BUG: Index creation with named Index resets name #11193

sinhrks opened this issue Sep 26, 2015 · 12 comments
Labels
API Design Bug Needs Discussion Requires discussion from core team before further action
Milestone

Comments

@sinhrks
Copy link
Member

sinhrks commented Sep 26, 2015

import pandas as pd
idx = pd.Index([1, 2 ,3], name='x')
pd.Index(idx)
# Int64Index([1, 2, 3], dtype='int64')

A point is whether to reset existing Index name in below case. Current impl can't distinguish whether name is not passed, or passed None explicitly.

pd.Index(idx, name=None)
# -> should reset name?
@sinhrks sinhrks added this to the 0.17.1 milestone Sep 26, 2015
@sinhrks sinhrks added the Needs Discussion Requires discussion from core team before further action label Sep 26, 2015
@jreback
Copy link
Contributor

jreback commented Sep 26, 2015

Same for DatetimeIndex. smells like a buggy. Should preserve meta-data in the constructor (for another passed Index)

In [18]: pd.DatetimeIndex(pd.date_range('20130101',periods=3,name='foo'))
Out[18]: DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03'], dtype='datetime64[ns]', freq='D')

@mbirdi
Copy link

mbirdi commented Oct 28, 2015

I tried to reproduce the bug and failed. Here is a link the code examples I ran on my laptop.

http://nbviewer.ipython.org/gist/mbirdi/7586ebc290bd90a73ed3

So in my examples I was able to reset the name of the index from 'x' to None.

@jreback
Copy link
Contributor

jreback commented Oct 28, 2015

@mbirdi this reproduces on master

In [58]: idx = pd.Index([1, 2 ,3], name='x')

In [59]: pd.Index(idx)
Out[59]: Int64Index([1, 2, 3], dtype='int64')

In [60]: pd.Index(idx).name

you need another index creation (e.g. with just a pd.Index)

@mbirdi
Copy link

mbirdi commented Oct 29, 2015

@jreback
@sinhrks

Thanks for the feedback.

I see the question up for discussion now. I have never really had to create an index from an index. Is that a common thing to do in pandas? I am trying to picture why I would do that with the datasets I work with and I am coming up blank.

@jreback
Copy link
Contributor

jreback commented Oct 29, 2015

well if I have something that could be a list or an Index then I want to be able to wrap it and preserve meta data. The bottom line is that we want to have API consistency.

@jreback jreback modified the milestones: Next Major Release, 0.17.1 Nov 13, 2015
@IamGianluca
Copy link

I can work on this. I'll send a PR shortly.

@IamGianluca
Copy link

@jreback What should be the desired behaviour when the user passes None as the value for the argument name and the existing index we are passing had already a name?

idx = pd.Index([1, 2 ,3], name='x')
pd.Index(idx, name=None)
# -> should reset name?

@shoyer
Copy link
Member

shoyer commented Nov 24, 2015

Well, one option is to use a sentinel object for the default instead, e.g., _default = object(), and then use def Index(..., name=_object):. Then you can tell the difference between the user passing name=None and not passing it at all. That said, I don't know if this is worth worrying about it. It's probably OK to keep the current name if name=None is passed.

@jreback
Copy link
Contributor

jreback commented Nov 24, 2015

I agree with @shoyer here. The point of this is to preserve the name, so name=None is not passing one (yes could do something like @shoyer suggest, but not worth it IMHO).

@IamGianluca
Copy link

Thanks for the clarification @jreback and @shoyer . I will send a pull request this evening.

@sinhrks
Copy link
Member Author

sinhrks commented Nov 25, 2015

The last question (name=None) is derived from test failures when I tried to fix it by simply retrieving the given Index name. @shoyer 's suggestion should work and be consistent.

@jreback jreback modified the milestones: 0.18.0, Next Major Release Nov 29, 2015
jreback pushed a commit that referenced this issue Dec 18, 2015
…ame, when a new name is not provided, #11193

fix missing lines in whatsnew

cosmetic change in whatsnew

refactor unit test, failing for MultiIndex

short sentence in whatsnew doc

fix unit test

remove commented code

fix typo

add doc string

add unit test for multiindex case
@jreback
Copy link
Contributor

jreback commented Dec 18, 2015

closed by #11695

@jreback jreback closed this as completed Dec 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Bug Needs Discussion Requires discussion from core team before further action
Projects
None yet
Development

No branches or pull requests

5 participants