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

REF: remove block access in groupby libreduction Series(Bin)Grouper #40199

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,20 @@ cdef class _BaseGrouper:
cached_typ = self.typ(
vslider.buf, dtype=vslider.buf.dtype, index=cached_ityp, name=self.name
)
self.has_block = hasattr(cached_typ._mgr, "_block")
else:
# See the comment in indexes/base.py about _index_data.
# We need this for EA-backed indexes that have a reference
# to a 1-d ndarray like datetime / timedelta / period.
object.__setattr__(cached_ityp, '_index_data', islider.buf)
cached_ityp._engine.clear_mapping()
cached_ityp._cache.clear() # e.g. inferred_freq must go
object.__setattr__(cached_typ._mgr._block, 'values', vslider.buf)
object.__setattr__(cached_typ._mgr._block, 'mgr_locs',
Copy link
Member

Choose a reason for hiding this comment

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

might get a small boost by setting _mgr_locs to BlockPlacement(slice(...)) instead of going through the mgr_locs property

slice(len(vslider.buf)))
if self.has_block:
object.__setattr__(cached_typ._mgr._block, 'values', vslider.buf)
object.__setattr__(cached_typ._mgr._block, 'mgr_locs',
slice(len(vslider.buf)))
else:
cached_typ._mgr.arrays[0] = vslider.buf
Copy link
Member Author

Choose a reason for hiding this comment

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

I could probably replace this full if/else block with a single cached_typ._mgr.set_values(vslider.buf) (if I add the setting of mgr_locs to SingleBlockManager.set_values).

But I assume that, currently, we don't use a plain python attribute setting but object.__setattr__ for performance? Using _mgr.set_values(..) might defeat that purpose?

Copy link
Member Author

Choose a reason for hiding this comment

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

So the newer commit I pushed does this change, and is thus now using _mgr.set_values.

I did some timings with a specific function that uses the SeriesGrouper with many labels + a cheap dummy function (adapted from the same benchmark case as I have been using for the other groupby PRs: #40178 (comment)):

ncols = 1000
N = 1000
data = np.random.randn(N, ncols)
labels = np.random.randint(0, 100, size=N)
df = pd.DataFrame(data)

%timeit df.groupby(labels)[0].agg(lambda x: 1)

And repeating this several times switching back and forth between this version and master, I don't see any difference. A representative timing was:

In [15]: %timeit df.groupby(labels)[0].agg(lambda x: 1)
1.18 ms ± 111 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)  <-- master
1.17 ms ± 88.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)  <-- PR

object.__setattr__(cached_typ, '_index', cached_ityp)
object.__setattr__(cached_typ, 'name', self.name)

Expand Down Expand Up @@ -108,6 +112,7 @@ cdef class SeriesBinGrouper(_BaseGrouper):
cdef public:
ndarray arr, index, dummy_arr, dummy_index
object values, f, bins, typ, ityp, name
bint has_block

def __init__(self, object series, object f, object bins):

Expand Down Expand Up @@ -201,6 +206,7 @@ cdef class SeriesGrouper(_BaseGrouper):
cdef public:
ndarray arr, index, dummy_arr, dummy_index
object f, labels, values, typ, ityp, name
bint has_block

def __init__(self, object series, object f, object labels,
Py_ssize_t ngroups):
Expand Down