Skip to content

De-duplicate code for indexing with list-likes of keys #21503

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

Merged
merged 4 commits into from
Jun 19, 2018

Conversation

toobaz
Copy link
Member

@toobaz toobaz commented Jun 15, 2018

  • tests passed
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff

Just refactoring, removing duplicated code (I don't think the bug in Index._reindex_non_unique was actually appearing anywhere)

@toobaz toobaz added Refactor Internal refactoring of code Indexing Related to indexing on series/frames, not to indexes themselves Clean labels Jun 15, 2018
@toobaz
Copy link
Member Author

toobaz commented Jun 15, 2018

(there is still room for improvement, by the way, but I will need to look in the Index methods)

@codecov
Copy link

codecov bot commented Jun 16, 2018

Codecov Report

Merging #21503 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #21503      +/-   ##
==========================================
- Coverage   91.92%   91.92%   -0.01%     
==========================================
  Files         153      153              
  Lines       49594    49570      -24     
==========================================
- Hits        45590    45566      -24     
  Misses       4004     4004
Flag Coverage Δ
#multiple 90.32% <100%> (-0.01%) ⬇️
#single 41.82% <80.64%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexing.py 93.41% <100%> (-0.15%) ⬇️
pandas/core/frame.py 97.23% <100%> (ø) ⬆️
pandas/core/indexes/base.py 96.62% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 71c53f0...2b07ab0. Read the comment docs.

@toobaz
Copy link
Member Author

toobaz commented Jun 16, 2018

ASV run (of indexing.py):

       before           after         ratio
     [9e982e18]       [c08a39fa]
+       146±0.7ms        167±0.7ms     1.14  frame_methods.Iteration.time_iteritems_indexing
-         103±2μs         93.2±2μs     0.91  indexing.DataFrameNumericIndexing.time_loc
-         213±2μs          193±1μs     0.91  indexing.DataFrameNumericIndexing.time_iloc_dups
-         250±2μs          224±1μs     0.90  indexing.DataFrameStringIndexing.time_boolean_rows
-         162±1μs          144±1μs     0.89  indexing.IntervalIndexing.time_getitem_list
-      53.1±0.2μs       46.3±0.3μs     0.87  indexing.IntervalIndexing.time_getitem_scalar
-     2.33±0.01ms      2.00±0.02ms     0.86  indexing.DataFrameNumericIndexing.time_loc_dups
-         494±8ns          424±5ns     0.86  indexing.MethodLookup.time_lookup_iloc
-         727±4μs         623±10μs     0.86  indexing.NumericSeriesIndexing.time_loc_list_like(<class 'pandas.core.indexes.numeric.Int64Index'>)
-        88.6±2μs       74.9±0.6μs     0.85  indexing.IntervalIndexing.time_loc_scalar
-     1.67±0.07ms      1.34±0.02ms     0.80  indexing.NumericSeriesIndexing.time_loc_array(<class 'pandas.core.indexes.numeric.Int64Index'>)
-     1.90±0.09ms      1.44±0.01ms     0.76  indexing.NumericSeriesIndexing.time_getitem_array(<class 'pandas.core.indexes.numeric.Int64Index'>)
-     1.77±0.07ms      1.30±0.01ms     0.73  indexing.NumericSeriesIndexing.time_ix_array(<class 'pandas.core.indexes.numeric.Int64Index'>)
-         799±8μs          519±4μs     0.65  indexing.NumericSeriesIndexing.time_ix_list_like(<class 'pandas.core.indexes.numeric.Int64Index'>)
-         319±5ms          201±4ms     0.63  indexing.NumericSeriesIndexing.time_ix_array(<class 'pandas.core.indexes.numeric.Float64Index'>)
-       307±0.9ms          185±2ms     0.60  indexing.NumericSeriesIndexing.time_ix_list_like(<class 'pandas.core.indexes.numeric.Float64Index'>)

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.

@toobaz toobaz changed the title Dedup multi take De-duplicate code for indexing with list-likes of keys Jun 16, 2018
@toobaz
Copy link
Member Author

toobaz commented Jun 18, 2018

@jreback @jorisvandenbossche ping

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

assumed this to make some other of your PRs simpler if this is a precursor?

@toobaz
Copy link
Member Author

toobaz commented Jun 18, 2018

assumed this to make some other of your PRs simpler if this is a precursor?

Not sure I understand. This transforms three copies of the same code into one (and in doing so removes some superfluous calls, explaining the performance improvements). So yes, incidentally it will also make it easier to further rationalize the indexing code, and yes, I plan to work on it.

@jreback
Copy link
Contributor

jreback commented Jun 18, 2018

@toobaz no that's what I was asking. thanks.

@jreback jreback added this to the 0.24.0 milestone Jun 18, 2018
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

looks good on the cleanup, a few comments.

@@ -3627,7 +3627,7 @@ def _reindex_non_unique(self, target):
else:

# need to retake to have the same size as the indexer
indexer[~check] = 0
indexer[~check] = -1
Copy link
Contributor

Choose a reason for hiding this comment

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

what effect did this have? e.g. was this a bug before or just not used?

Copy link
Member Author

Choose a reason for hiding this comment

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

To the best of my understanding, it was not used: not in the sense that this line was not hit, but because the locations for missing keys were then taken from new_indexer (see three lines below), where they were correctly marked with -1.

o._get_axis_number(axis))
d[axis] = (keyarr, indexer)
d = {axis: self._get_listlike_indexer(key, axis)
for (key, axis) in zip(tup, o._AXIS_ORDERS)}
return o._reindex_with_indexers(d, copy=True, allow_dups=True)
except (KeyError, IndexingError) as detail:
Copy link
Contributor

Choose a reason for hiding this comment

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

This can only raise KeyError now, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

and in fact, maybe can just use self._exception() in _get_listlike_indexer? then can simply remove the try/except (here)? (and I think you do this below)

Copy link
Member Author

Choose a reason for hiding this comment

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

In fact, I think the entire block was just superfluous: if we get here, it means we have collections of keys, and if a key is not found, it will raise later.

values : array-like
An indexer for the return object; -1 denotes keys not found
"""
try:
Copy link
Contributor

Choose a reason for hiding this comment

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

can you move ops that won't fail outside the try

if new_indexer is not None:
result = self.obj._take(indexer[indexer != -1], axis=axis)
def _getitem_iterable(self, key, axis=None):
if axis is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a doc-string here


return result
if com.is_bool_indexer(key):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a comment on these cases (e.g. what is being checked)

@@ -1214,6 +1220,10 @@ def _validate_read_indexer(self, key, indexer, axis):
u"None of [{key}] are in the [{axis}]".format(
key=key, axis=self.obj._get_axis_name(axis)))

if not(self.name == 'loc' and not raise_missing):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a comment here

@toobaz toobaz force-pushed the dedup_multi_take branch from c08a39f to 2223bbe Compare June 19, 2018 09:56
@toobaz toobaz force-pushed the dedup_multi_take branch from 2223bbe to 2b07ab0 Compare June 19, 2018 10:00
@toobaz
Copy link
Member Author

toobaz commented Jun 19, 2018

@jreback ping

@jreback jreback merged commit 5fbb683 into pandas-dev:master Jun 19, 2018
@jreback
Copy link
Contributor

jreback commented Jun 19, 2018

very nice @toobaz

more like this very welcome!

@toobaz toobaz deleted the dedup_multi_take branch June 19, 2018 22:26
Sup3rGeo pushed a commit to Sup3rGeo/pandas that referenced this pull request Oct 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Clean Indexing Related to indexing on series/frames, not to indexes themselves Refactor Internal refactoring of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants