Skip to content

BUG: Index does not inherit existing Index or DatatetimeIndex object … #11695

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
wants to merge 1 commit into from
Closed

BUG: Index does not inherit existing Index or DatatetimeIndex object … #11695

wants to merge 1 commit into from

Conversation

IamGianluca
Copy link

This PR is meant to fix issue #11193
The issue prevent, when creating a new Index object based on a passed Index or DatetimeIndex object, to inherit the existing Index name if a new name is not provided.

I've also created two unit test which test the new behaviour on both cases (passing an Index or DatetimeIndex object) and added a comment in the whatsnew documentation.

@max-sixty
Copy link
Contributor

Great!

  • This could test other classes (PeriodIndex, etc), both when they're called with their own constructor (pd.PeriodIndex(period_index), and with different constructors (pd.Index(period_index)).
  • This could also update all the fields in _attributes, rather than just name (although it'd have to protect against differently typed Indexes having different attributes. In many cases it would be identical; a standard Index only has name in _attributes.)

I wonder if you could just call _infer_new from this (unmerged) PR: #11497, and that would take care of all this.

@IamGianluca
Copy link
Author

I'll rebase on master and squash my commits. I should update my PR shortly.

@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Nov 25, 2015
@@ -105,3 +105,5 @@ Performance Improvements

Bug Fixes
~~~~~~~~~

- Bug in ``Index`` prevent inheriting, from the passed Index or DatetimeIndex object, the existing name when a new one is not provided (:issue:`11193`)
Copy link
Contributor

Choose a reason for hiding this comment

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

just say creation, don't use inheriting, which has a different meaning

@jreback jreback added this to the 0.18.0 milestone Nov 29, 2015
- Bug in ``.loc`` against ``CategoricalIndex`` may result in normal ``Index`` (:issue:`11586`)
- Bug groupby on tz-aware data where selection not returning ``Timestamp`` (:issue:`11616`)
- Bug in timezone info lost when broadcasting scalar datetime to ``DataFrame`` (:issue:`11682`)
- Bug in ``Index`` prevents creation of a new Index object which has the same name of the passed Index or DatetimeIndex object, when a new name is not provided (:issue:`11193`)
Copy link
Contributor

Choose a reason for hiding this comment

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

use double backticks around Index and DatetimeIndex

@jreback
Copy link
Contributor

jreback commented Nov 29, 2015

minor correction, pls rebase as I just merged #11497 which might impact this. ping when green.

@IamGianluca
Copy link
Author

I've just rebased @jreback I'll add a second comment when the Travis CI build is completed.

idx = self.create_index()
idx.name = "foo"
result = pd.Index(idx)
self.assertTrue("'foo'" in str(result))
Copy link
Contributor

Choose a reason for hiding this comment

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

just construct the expected index and compare

expected = self.create_index()
expected.name = 'foo'
tm.assert_index_equal(result, expected)

also check with a new passed name

Copy link
Author

Choose a reason for hiding this comment

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

I can work on this. Please ignore the last PR.

Copy link
Author

Choose a reason for hiding this comment

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

@jreback What about the case when the object passed to Index is a MultiIndex? I think this is a corner case and should be avoided in principle. As a workaround, I would write the unit test as follows, even though is not very elegant:

    expected = self.create_index()
    if not isinstance(expected, MultiIndex):
        expected.name = 'foo'
        result = pd.Index(expected)
        tm.assert_index_equal(result, expected)

        result = pd.Index(expected, name='bar')
        expected.name = 'bar'
        tm.assert_index_equal(result, expected)

Should Index raise an error when we try to pass a MultiIndex object?

@IamGianluca
Copy link
Author

@jreback let me know if this needs further changes.

@@ -192,6 +193,21 @@ Bug Fixes


- Bug in ``.loc`` result with duplicated key may have ``Index`` with incorrect dtype (:issue:`11497`)


Copy link
Contributor

Choose a reason for hiding this comment

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

need a doc-note, and you are changing things which are not relevant. e.g. pls rebase on master first.

@jreback
Copy link
Contributor

jreback commented Dec 16, 2015

@IamGianluca can you update

…ame, when a new name is not provided

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
@IamGianluca
Copy link
Author

I've updated the unit test @jreback

@jreback
Copy link
Contributor

jreback commented Dec 18, 2015

merged via fc40dcc

thanks!

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.

3 participants