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

ENH: Improve performance for df.__setitem__ with list-like indexers #38148

Merged
merged 4 commits into from
Nov 29, 2020

Conversation

phofl
Copy link
Member

@phofl phofl commented Nov 29, 2020

Reindexing the Block Manager improves the performance significantly. I hope I have not missed anything, concerning the reindexing of the blocks.
Time spent in _ensure_listlike_indexer is pretty low now.
timeit result for the ops methods:

In [20]: %timeit setitem(x, x_col, df)
1.09 ms ± 9.65 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [21]: %timeit concat(x, x_col, df)
293 µs ± 4.29 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Should we add tests here? I have added an asv to capture this case.

cc @jbrockmendel

@phofl phofl added Indexing Related to indexing on series/frames, not to indexes themselves Performance Memory or execution speed performance labels Nov 29, 2020
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.

wow, simplification and pref boost

asv_bench/benchmarks/indexing.py Show resolved Hide resolved
self.obj[k] = value[i]
else:
self.obj[k] = value
keys = self.obj.columns.tolist()
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 create the keys in the same order whether they are in the obj or not (e.g. combine L666 and 667)

Copy link
Contributor

Choose a reason for hiding this comment

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

do we have a test for this ordering?

Copy link
Member Author

Choose a reason for hiding this comment

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

Union should do the trick.

At least the test added with #37964 covers this

@jreback jreback added this to the 1.2 milestone Nov 29, 2020
doc/source/whatsnew/v1.2.0.rst Outdated Show resolved Hide resolved
@jreback jreback modified the milestones: 1.2, 1.1.5 Nov 29, 2020
@jreback
Copy link
Contributor

jreback commented Nov 29, 2020

lgtm can merge on green.

@phofl
Copy link
Member Author

phofl commented Nov 29, 2020

@jreback greenish. Failure unrelated

@jreback jreback merged commit 2f41109 into pandas-dev:master Nov 29, 2020
@jreback
Copy link
Contributor

jreback commented Nov 29, 2020

thanks @phofl

@phofl phofl deleted the 37954 branch November 29, 2020 21:54
@jbrockmendel
Copy link
Member

@phofl did you determine that this isn't making a copy?

@phofl
Copy link
Member Author

phofl commented Nov 30, 2020

Not with a test, but I tested this in code.

x = self.obj
self.obj._mgr = self.obj._mgr.reindex_axis(keys, 0)

This also changes x, so that is not a copy, isn't it?

Should we add a test for this?

@jbrockmendel
Copy link
Member

This also changes x, so that is not a copy, isn't it?

Assuming homogeneous dtype for now, what im asking for is:

values = self.obj.values
self.obj._mgr = self.obj._mgr.reindex_axis(keys, 0)
new_values = self.obj.values

assert new_values is values   # <-- will raise if we made a copy

@jreback
Copy link
Contributor

jreback commented Nov 30, 2020

i am not sure this is possible w/o a copy

@phofl
Copy link
Member Author

phofl commented Nov 30, 2020

This does make a copy. Did not know, that we have to compare values instead of the object itself.

@phofl
Copy link
Member Author

phofl commented Nov 30, 2020

Actually this raised previously too, so at least not a regression

@jbrockmendel
Copy link
Member

Did not know, that we have to compare values instead of the object itself.

Note: the example above I assumed homogeneous dtypes. More generally, we need to have obj._mgr.blocks[n].values be unchanged for each pre-existing n.

i am not sure this is possible w/o a copy

A few things need to happen for it to be possible:

  1. add consolidate: bool = True keyword to BlockManager.reindex_axis that then passes consolidate=consolidate to reindex_indexer
  2. add only_slice: bool = False to reindex_axis and reindex_indexer so it can be passed to _slice_take_blocks_ax0
  3. the reindex_axis call in this PR passes consolidate=False, only_slice=True

@simonjayhawkins
Copy link
Member

@meeseeksdev backport 1.1.x

@lumberbot-app

This comment has been minimized.

@simonjayhawkins
Copy link
Member

#38181 (comment)

@phofl #38148 (comment) was this necessary? do we know what commit caused the performance regression. the existing code on 1.1.x looks much simpler to what was replaced on master.

If we don't backport this, we will need to move the release note on master

@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Nov 30, 2020

As @jbrockmendel noted, this can change the copy/view semantics in certain cases.

One example:

df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 4, 6]})
# get one column as a view of df
s = df['a']
# add columns with list-like indexer
df[['c', 'd']] = np.array([[.1, .2], [.3, .4], [.4, .5]])
# edit in place the first column to check view semantics
df.iloc[0, 0] = 100

on master this gives:

In [8]: df
Out[8]: 
     a  b    c    d
0  100  4  0.1  0.2
1    2  4  0.3  0.4
2    3  6  0.4  0.5

In [9]: s
Out[9]: 
0    1
1    2
2    3
Name: a, dtype: int64

where "a" and "b" columns were now copied in the df[['c', 'd']] = ... operation (shown by s not being updated).

While on pandas 1.1.4, this copy didn't happen, and the series was actually updated:

In [6]: df
Out[6]: 
     a  b    c    d
0  100  4  0.1  0.2
1    2  4  0.3  0.4
2    3  6  0.4  0.5

In [7]: s
Out[7]: 
0    100
1      2
2      3
Name: a, dtype: int64

This of course closely relates to the recent discussions about improving the copy/view semantics (as currently those semantics are both not clear and largely untested).

(now the above is certainly a specific example to trigger the issue. In many cases we would also do a copy, eg if there would already have been float columns present in the example df)

@jbrockmendel
Copy link
Member

what if instead of calling reindex_axis we called reindex_indexer and passed indexer=slice(len(self.obj.columns))?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: df.__setitem__ can be 10x slower than pd.concat(..., axis=1)
5 participants