-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
Conversation
Not sure about where the whatsnew entry will go? |
@@ -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)) |
There was a problem hiding this comment.
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
.
Thanks! whatsnew locates on |
Also, I think it is an API change, rather than a bug (as original issue is tagged). |
Thanks @sinhrks for the friendly review! This is my first PR, so it's a good opportunity to get a start! Also, the Travis failures seem to be unrelated? |
@jrings you need to rebase on master. Things changed a bit today. |
Ugh rebase doesn't like me. Never has never will. Have rebased, will try to squash if changes are ok? (or cry for help) |
http://pandas.pydata.org/pandas-docs/stable/contributing.html#contributing-your-changes-to-pandas
then |
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
After properly rebased, pls fix datetimelike Also, adding more dtype tests (you can find some examples here). |
1ce3348
to
678a6b7
Compare
Ok, cleaned up the rebase mess, will react to the rest later or tomorrow. Thanks! |
@sinhrks Oh I see, by different dtypes you meant testing map on the different index flavors, not a type conversion on the Index? |
@jrings thx for the fix. yes i meant different classes. |
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 |
@@ -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`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dupe.
The |
Ok, yeah sorry that |
Also, should I do anything for empty and tuples Index? |
#12532 has been merged, can you update? |
""" | ||
Apply mapper function to an index | ||
|
||
Parameters |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected!
fab8205
to
2351ec8
Compare
Ok categorical works now! Rebased on master and squashed commits. |
@@ -1557,6 +1557,41 @@ def test_string_index_repr(self): | |||
|
|||
self.assertEqual(coerce(idx), expected) | |||
|
|||
def test_map(self): |
There was a problem hiding this comment.
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.
Will work on it this weekend! |
Sorry for being slow, am traveling a lot at the moment. But one question @jreback, would a very simple function for |
@@ -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`) |
There was a problem hiding this comment.
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
can you rebase/update |
2351ec8
to
5c8c57e
Compare
can you rebase/update |
can you rebase / update? |
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
5c8c57e
to
c30c862
Compare
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? |
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. |
Continued in #14506 |
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)
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)
Continued in #14506
git diff upstream/master | flake8 --diff