Git Product home page Git Product logo

Comments (2)

hadley avatar hadley commented on August 26, 2024

Comments from Wes:

one relevant section is in pandas/core/groupby.py. here's the code
with a few comments. as you can see I haven't completely solved the
problem, there's still a 64-bit overflow possibility that I'm punting
on (this doesn't occur that much in practice except with very large
number of group keys)

def _get_compressed_labels(self):

    # These are the "factorized" integer numbers for each group key
    all_labels = [ping.labels for ping in self.groupings]

    if self._overflow_possible:

        # We're punting and doing an object hash if we're going
        # to overflow a 64-bit integer. Lame, I know

        # Convert the label arrays to Python tuples
        tups = lib.fast_zip(all_labels)

        # Pass the tuples through a hash table to compute integer labels
        labs, uniques = algos.factorize(tups)

        # Sort them if so desired
        if self.sort:
            uniques, labs = _reorder_by_uniques(uniques, labs)

        return labs, uniques
    else:
        if len(all_labels) > 1:
            # This is the "cartesian product number"
            group_index = get_group_index(all_labels, self.shape)

            # Pass the cartesian product number through the hash
            # table again to remove sparsity
            comp_ids, obs_group_ids = _compress_group_index(group_index)
        else:
            # Just use the integer labels from the only group key

            ping = self.groupings[0]
            comp_ids = ping.labels
            obs_group_ids = np.arange(len(ping.group_index))
            self.compressed = False
            self._filter_empty_groups = False

        return comp_ids, obs_group_ids

My recommendation: use a fast hash table impl, either std::hashmap or
https://github.com/attractivechaos/klib (I don't think the one used in
factor(...) is very good, and there's some wasted performance there),
to convert basic R data types into integers. Write the above helper
functions, and then you should be in business.

I haven't looked at the data.table implementation for GPL reasons but
there might be some interesting algorithmic details in there to learn
from perhaps-- though when your data is indexed and therefore
presorted this all becomes much easier

from dplyr.

hadley avatar hadley commented on August 26, 2024

Moved to dplyrRcpp

from dplyr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.