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

Closed
sinhrks opened this Issue Sep 26, 2015 · 12 comments

Comments

Projects
None yet
5 participants
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 added this to the 0.17.1 milestone Sep 26, 2015

jreback referenced this issue Sep 26, 2015

Open

BUG: losing Index/Series names master issue #9862

8 of 12 tasks complete
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 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.

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 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.

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 milestone: Next Major Release, 0.17.1 Nov 13, 2015

Contributor

IamGianluca commented Nov 21, 2015

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

Contributor

IamGianluca commented Nov 23, 2015

@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?
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.

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).

Contributor

IamGianluca commented Nov 24, 2015

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

Member

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 milestone: 0.18.0, Next Major Release Nov 29, 2015

@jreback jreback added a commit that referenced this issue Dec 18, 2015

@IamGianluca @jreback IamGianluca + jreback BUG: Index does not copy existing Index or DatatetimeIndex object's n…
…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
fc40dcc
Contributor

jreback commented Dec 18, 2015

closed by #11695

jreback closed this Dec 18, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment