-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
BUG: Fix series rename called with str altering name rather index (GH17407) #17654
Conversation
Codecov Report
@@ Coverage Diff @@
## master #17654 +/- ##
==========================================
- Coverage 91.27% 91.22% -0.05%
==========================================
Files 163 163
Lines 49765 49766 +1
==========================================
- Hits 45421 45401 -20
- Misses 4344 4365 +21
Continue to review full report at Codecov.
|
pandas/core/dtypes/inference.py
Outdated
@@ -263,7 +263,8 @@ def is_list_like(obj): | |||
""" | |||
|
|||
return (hasattr(obj, '__iter__') and | |||
not isinstance(obj, string_and_binary_types)) |
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.
do this with an or between iter and collections....
return (hasattr(obj, '__iter__') or isinstance(obj, Iterable) and not isinstance(obj, string....)
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.
using or doesn't work, as hasattr(str, '__iter__')
returns True
and that caused the bug. I changed it to use and
pandas/core/dtypes/inference.py
Outdated
@@ -263,7 +263,8 @@ def is_list_like(obj): | |||
""" | |||
|
|||
return (hasattr(obj, '__iter__') and | |||
not isinstance(obj, string_and_binary_types)) | |||
not isinstance(obj, string_and_binary_types) and | |||
isinstance(obj, collections.Iterable)) |
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.
from collection import Iterable
directly to the name space at the top
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.
done
doc/source/whatsnew/v0.21.0.txt
Outdated
@@ -546,6 +546,7 @@ Indexing | |||
- Bug in intersection of ``RangeIndex`` with negative step (:issue:`17296`) | |||
- Bug in ``IntervalIndex`` where performing a scalar lookup fails for included right endpoints of non-overlapping monotonic decreasing indexes (:issue:`16417`, :issue:`17271`) | |||
- Bug in :meth:`DataFrame.first_valid_index` and :meth:`DataFrame.last_valid_index` when no valid entry (:issue:`17400`) | |||
- Bug in ``Series.rename`` when called with `str` alters name of series rather than index of series. (:issue:`17407`) |
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.
called with a callable
pandas/core/dtypes/inference.py
Outdated
@@ -263,7 +263,8 @@ def is_list_like(obj): | |||
""" | |||
|
|||
return (hasattr(obj, '__iter__') and | |||
not isinstance(obj, string_and_binary_types)) | |||
not isinstance(obj, string_and_binary_types) and | |||
isinstance(obj, collections.Iterable)) | |||
|
|||
|
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 run a sub-set of the perf tests (IOW something like this)
asv continuous master thisbranchname --bench indexing
asv continuous master thisbranchname --bench timeseries
and report any significant diffs.
This touches code from almost everywhere indirectly so want to make sure no regressions.
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.
Done. See comments below for what the performance tests reported.
For indexing performance test,
For timeseries performance test,
|
pandas/core/dtypes/inference.py
Outdated
@@ -262,7 +263,7 @@ def is_list_like(obj): | |||
False | |||
""" | |||
|
|||
return (hasattr(obj, '__iter__') and | |||
return (hasattr(obj, '__iter__') and isinstance(obj, Iterable) and |
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 should be an or
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.
I see your point, but that wont fix the issue. What about only check isinstance(obj, Iterable)
? I'm asking this because I see another function in inference.py
called _iterable_not_string
, it checks against collections.Iterable
rather check for __iter__
attribute.
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.
you can try but this is used pretty much everywhere and you will break things. Technically a type
satisfies this condition I think, but we DO want to exlude it. you could also add it to the not isinstance(obj, (string_types, type))
. might work.
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.
I've checked, type
actually doesn't satisfy the condition. Also all the tests have passed after the change.
doc/source/whatsnew/v0.21.0.txt
Outdated
@@ -565,6 +565,7 @@ Indexing | |||
- Bug in intersection of ``RangeIndex`` with negative step (:issue:`17296`) | |||
- Bug in ``IntervalIndex`` where performing a scalar lookup fails for included right endpoints of non-overlapping monotonic decreasing indexes (:issue:`16417`, :issue:`17271`) | |||
- Bug in :meth:`DataFrame.first_valid_index` and :meth:`DataFrame.last_valid_index` when no valid entry (:issue:`17400`) | |||
- Bug in ``Series.rename`` when called with a `callable` alters name of series rather than index of series. (:issue:`17407`) |
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.
Bug in :func:`Series.rename`
when called with a callable, incorrectly alters the name of the Series
, rather than the name of the Index
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.
Done.
can you do
then force push this will rebuild and make sure our CI passes. ping on green. |
a61c12b
to
5d8bbf9
Compare
ci failed with this error:
and I found this on stackoverflow, seems related to a new Anaconda release. Any idea how to fix this? |
If you rebase on top of master it should be fixed. |
Thanks, Tom! CI passes now. |
Thanks @TomAugspurger and @jreback for the reviews. This is my first PR, and I'm excited to do more. |
* 'master' of github.com:pandas-dev/pandas: (188 commits) Separate out _convert_datetime_to_tsobject (pandas-dev#17715) DOC: remove whatsnew note for xref pandas-dev#17131 BUG: Regression in .loc accepting a boolean Index as an indexer (pandas-dev#17738) DEPR: Deprecate cdate_range and merge into bdate_range (pandas-dev#17691) CLN: replace %s syntax with .format in pandas.core: categorical, common, config, config_init (pandas-dev#17735) Fixed the memory usage explanation of categorical in gotchas from O(nm) to O(n+m) (pandas-dev#17736) TST: add backward compat for offset testing for pickles (pandas-dev#17733) remove unused time conversion funcs (pandas-dev#17711) DEPR: Deprecate convert parameter in take (pandas-dev#17352) BUG:Time Grouper bug fix when applied for list groupers (pandas-dev#17587) BUG: Fix some PeriodIndex resampling issues (pandas-dev#16153) BUG: Fix unexpected sort in groupby (pandas-dev#17621) DOC: Fixed typo in documentation for 'pandas.DataFrame.replace' (pandas-dev#17731) BUG: Fix series rename called with str altering name rather index (GH17407) (pandas-dev#17654) DOC: Add examples for MultiIndex.get_locs + cleanups (pandas-dev#17675) Doc improvements for IntervalIndex and Interval (pandas-dev#17714) BUG: DataFrame sort_values and multiple "by" columns fails to order NaT correctly Last of the timezones funcs (pandas-dev#17669) Add missing file to _pyxfiles, delete commented-out (pandas-dev#17712) update imports of DateParseError, remove unused imports from tslib (pandas-dev#17713) ...
…17407) (pandas-dev#17654) * BUG: Fix series rename called with str altering the name. GH17407 * add whatsnew for the fix for pandas-dev#17407 * Fix typo in whatsnew * remove whitespace * Update code after @jreback's comments * Change `or` to `and` for checking iterable * Only check against Iterable in is_list_like and add test for `str` * Update v0.21.0.txt
…17407) (pandas-dev#17654) * BUG: Fix series rename called with str altering the name. GH17407 * add whatsnew for the fix for pandas-dev#17407 * Fix typo in whatsnew * remove whitespace * Update code after @jreback's comments * Change `or` to `and` for checking iterable * Only check against Iterable in is_list_like and add test for `str` * Update v0.21.0.txt
git diff upstream/master -u -- "*.py" | flake8 --diff