Skip to content
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

API: map() on Index returns an Index, not array #12798

Closed
wants to merge 2 commits into from

Conversation

jrings
Copy link

@jrings jrings commented Apr 5, 2016

Continued in #14506

@jrings
Copy link
Author

jrings commented Apr 5, 2016

Not sure about where the whatsnew entry will go?

@jrings jrings changed the title map() on Index returns an Index, not array BUG: map() on Index returns an Index, not array Apr 5, 2016
@@ -2198,7 +2198,7 @@ def groupby(self, to_groupby):
return self._groupby(self.values, _values_from_object(to_groupby))

def map(self, mapper):
return self._arrmap(self.values, mapper)
return Index(self._arrmap(self.values, mapper))
Copy link
Member

Choose a reason for hiding this comment

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

This impl losts metadata. Pls use _shallow_copy_with_infer.

@sinhrks
Copy link
Member

sinhrks commented Apr 5, 2016

Thanks! whatsnew locates on doc/source/whatsnew.

@sinhrks sinhrks added API Design Compat pandas objects compatability with Numpy or Python functions labels Apr 5, 2016
@sinhrks
Copy link
Member

sinhrks commented Apr 5, 2016

Also, I think it is an API change, rather than a bug (as original issue is tagged).

@jrings jrings changed the title BUG: map() on Index returns an Index, not array API: map() on Index returns an Index, not array Apr 5, 2016
@jrings
Copy link
Author

jrings commented Apr 5, 2016

Thanks @sinhrks for the friendly review! This is my first PR, so it's a good opportunity to get a start!
I made those changes, let me know if this is what you had in mind!

Also, the Travis failures seem to be unrelated?

@jreback
Copy link
Contributor

jreback commented Apr 5, 2016

@jrings you need to rebase on master. Things changed a bit today.

@jrings
Copy link
Author

jrings commented Apr 5, 2016

Ugh rebase doesn't like me. Never has never will. Have rebased, will try to squash if changes are ok? (or cry for help)

@jreback
Copy link
Contributor

jreback commented Apr 6, 2016

http://pandas.pydata.org/pandas-docs/stable/contributing.html#contributing-your-changes-to-pandas

git checkout yourbranch
git rebase -i origin/master

then
git push yourremote yourbranch -f

@jrings
Copy link
Author

jrings commented Apr 6, 2016

Yeah I merged on the master to my master, then to my branch. Will work on fixing it, I finally need to figure this out :D

@@ -1,5 +1,5 @@
pytz
numpy
numpy=1.10.4
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 revert unnecessary changes?

Copy link
Author

Choose a reason for hiding this comment

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

git blame tells me this comes from @jreback jrings@fded942

Copy link
Member

Choose a reason for hiding this comment

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

Pls only include changes you've made(see other PRs). You need rebase:

git checkout master
git pull upstream
git checkout <your branch>
git rebase -i master

@sinhrks
Copy link
Member

sinhrks commented Apr 6, 2016

After properly rebased, pls fix datetimelike map consistently.

Also, adding more dtype tests (you can find some examples here).

@jrings jrings force-pushed the index_map_return_index branch 3 times, most recently from 1ce3348 to 678a6b7 Compare April 6, 2016 22:04
@jrings
Copy link
Author

jrings commented Apr 6, 2016

Ok, cleaned up the rebase mess, will react to the rest later or tomorrow. Thanks!

@jrings
Copy link
Author

jrings commented Apr 6, 2016

@sinhrks Oh I see, by different dtypes you meant testing map on the different index flavors, not a type conversion on the Index?

@sinhrks
Copy link
Member

sinhrks commented Apr 6, 2016

@jrings thx for the fix. yes i meant different classes.

@jrings
Copy link
Author

jrings commented Apr 7, 2016

Ok, need some advice. Added almost all the index types successfully (pushed what I have so far), but failed on the categorical:

======================================================================
ERROR: test_map (pandas.tests.indexes.test_base.TestIndex)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/joerg/src/pandas/pandas/tests/indexes/test_base.py", line 1593, in test_map
    testIdx = self.catIndex.map(lambda x: int(x))
  File "/home/joerg/src/pandas/pandas/indexes/base.py", line 2201, in map
    return self._shallow_copy_with_infer(self._arrmap(self.values, mapper))
TypeError: Argument 'index' has incorrect type (expected numpy.ndarray, got Categorical)

Can you give me a pointer to how to fix this? i guess since the .c mapper doesn't cover this, I'd check for a CategoricalIndex and handle it in Python instead? Or, rather, implement a separate map in CategoricalIndex?

@@ -105,6 +105,10 @@ API changes
- ``pd.show_versions()`` now includes ``pandas_datareader`` version (:issue:`12740`)
- Provide a proper ``__name__`` and ``__qualname__`` attributes for generic functions (:issue:`12021`)

- ``map`` on an ``Index`` now returns an ``Index``, not an array (:issue:`12766`)

- ``map`` on an ``Index`` now returns an ``Index``, not an array (:issue:`12766`)
Copy link
Member

Choose a reason for hiding this comment

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

dupe.

@sinhrks
Copy link
Member

sinhrks commented Apr 8, 2016

The CategoricalIndex error should be handled in #12532. Let me update it.

@jrings
Copy link
Author

jrings commented Apr 8, 2016

Ok, yeah sorry that print was left over from figuring out the catIndex, and I'll dedupe the whatsnew entry as well. Should I wait for #12532 to be merged then?

@jrings
Copy link
Author

jrings commented Apr 8, 2016

Also, should I do anything for empty and tuples Index?

@sinhrks
Copy link
Member

sinhrks commented Apr 18, 2016

#12532 has been merged, can you update?

"""
Apply mapper function to an index

Parameters
Copy link
Member

Choose a reason for hiding this comment

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

The indentation is one space off here (as well as all lines below)

Copy link
Author

Choose a reason for hiding this comment

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

Corrected!

@jrings jrings force-pushed the index_map_return_index branch 2 times, most recently from fab8205 to 2351ec8 Compare April 23, 2016 17:23
@jrings
Copy link
Author

jrings commented Apr 23, 2016

Ok categorical works now! Rebased on master and squashed commits.
@sinhrks

@@ -1557,6 +1557,41 @@ def test_string_index_repr(self):

self.assertEqual(coerce(idx), expected)

def test_map(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

put all of this in pandas/tests/index/common.py/Base it will then get tested on each index type. see the structure of other methods. If you need to override it then put it specifically in a pandas/test/index/test_* file.

@jrings
Copy link
Author

jrings commented Apr 29, 2016

Will work on it this weekend!

@jrings
Copy link
Author

jrings commented May 4, 2016

Sorry for being slow, am traveling a lot at the moment. But one question @jreback, would a very simple function for map be enough, just to test that a map still produces an Index, because then putting it all in the loop of the various indexes would be fine, but if I would try to transfer my more varied tests that would lead to a lot of isinstance for each type that would sort of defeat the purpose - so then I'd distribute them over all the different test classes for each type of index?

@@ -149,6 +149,8 @@ API changes
- Provide a proper ``__name__`` and ``__qualname__`` attributes for generic functions (:issue:`12021`)
- ``pd.concat(ignore_index=True)`` now uses ``RangeIndex`` as default (:issue:`12695`)

- ``map`` on an ``Index`` now returns an ``Index``, not an array (:issue:`12766`)
Copy link
Contributor

Choose a reason for hiding this comment

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

move to 0.18.2

@jreback
Copy link
Contributor

jreback commented May 14, 2016

can you rebase/update

@jreback
Copy link
Contributor

jreback commented May 31, 2016

can you rebase/update

@jreback
Copy link
Contributor

jreback commented Sep 9, 2016

can you rebase / update?

@jreback
Copy link
Contributor

jreback commented Oct 6, 2016

pls rebase / update

[ENH] Using _shallow_copy_with_infer, additional test for type change and attribute conservation

TST: Tests for various index types

REF: Move to tm.assert_index_equal

DOC: docstring for the map function
@nateyoder
Copy link
Contributor

If this has stalled I'd be more than happy to take it over as I'd really like to see and #12756 make it into a release soon. Is the easiest way to do this by creating a new PR or can I use this one somehow?

@jreback
Copy link
Contributor

jreback commented Oct 25, 2016

create a new PR. you can incorporate this commit (if you'd like). you can add a closes (this PR number) as well as the issue number.

@jorisvandenbossche jorisvandenbossche added this to the No action milestone Nov 7, 2016
@jorisvandenbossche
Copy link
Member

Continued in #14506

jreback pushed a commit that referenced this pull request Dec 16, 2016
closes #12766
closes #12798

This is a follow on to #12798.

Author: Nate Yoder <nate@whistle.com>

Closes #14506 from nateyoder/index_map_index and squashes the following commits:

95e4440 [Nate Yoder] fix typo and add ref tag in whatsnew
b36e83c [Nate Yoder] update whatsnew, fix documentation
4635e6a [Nate Yoder] compare as index
a17ddab [Nate Yoder] Fix unused import and docstrings per pep8radius docformatter; change other uses of assert_index_equal to testing instead os self
ab168e7 [Nate Yoder] Update whatsnew and add git PR to tests to denote changes
504c2a2 [Nate Yoder] Fix tests that weren't run by PyCharm
23c133d [Nate Yoder] Update tests to match dtype int64
07b772a [Nate Yoder] use the numpy results if we can to avoid repeating the computation just to create the object
a110be9 [Nate Yoder] make map on time tseries indices return index if dtype of output is not a tseries; sphinx changes; fix docstring
a596744 [Nate Yoder] introspect results from map so that if the output array has tuples we create a multiindex instead of an index
5fc66c3 [Nate Yoder] make map return an index if it operates on an index, multi index, or categorical index; map on a categorical will either return a categorical or an index (rather than a numpy array)
@jreback jreback modified the milestones: 0.20.0, No action Dec 16, 2016
ischurov pushed a commit to ischurov/pandas that referenced this pull request Dec 19, 2016
closes pandas-dev#12766
closes pandas-dev#12798

This is a follow on to pandas-dev#12798.

Author: Nate Yoder <nate@whistle.com>

Closes pandas-dev#14506 from nateyoder/index_map_index and squashes the following commits:

95e4440 [Nate Yoder] fix typo and add ref tag in whatsnew
b36e83c [Nate Yoder] update whatsnew, fix documentation
4635e6a [Nate Yoder] compare as index
a17ddab [Nate Yoder] Fix unused import and docstrings per pep8radius docformatter; change other uses of assert_index_equal to testing instead os self
ab168e7 [Nate Yoder] Update whatsnew and add git PR to tests to denote changes
504c2a2 [Nate Yoder] Fix tests that weren't run by PyCharm
23c133d [Nate Yoder] Update tests to match dtype int64
07b772a [Nate Yoder] use the numpy results if we can to avoid repeating the computation just to create the object
a110be9 [Nate Yoder] make map on time tseries indices return index if dtype of output is not a tseries; sphinx changes; fix docstring
a596744 [Nate Yoder] introspect results from map so that if the output array has tuples we create a multiindex instead of an index
5fc66c3 [Nate Yoder] make map return an index if it operates on an index, multi index, or categorical index; map on a categorical will either return a categorical or an index (rather than a numpy array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Compat pandas objects compatability with Numpy or Python functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API: Index.map should return Index rather than array
5 participants