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

Implement DataFrame.hash_values, deprecate DataFrame.hash_columns. #9458

Merged
merged 4 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
53 changes: 48 additions & 5 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4396,6 +4396,9 @@ def apply_chunks(
def hash_columns(self, columns=None, method="murmur3"):
"""Hash the given *columns* and return a new device array

This method is deprecated. Replace ``df.hash_columns(columns, method)``
with ``df[columns].hash_values(method)``.

Parameters
----------
columns : sequence of str; optional
Expand All @@ -4411,14 +4414,54 @@ def hash_columns(self, columns=None, method="murmur3"):
Series
Hash values for each row.
"""
table_to_hash = (
self
if columns is None
else Frame(data={k: self._data[k] for k in columns})
warnings.warn(
"The `hash_columns` method will be removed in a future cuDF "
"release. Replace `df.hash_columns(columns, method)` with "
"`df[columns].hash_values(method)`.",
FutureWarning,
)
if columns is None:
# Slice by [:] to keep all columns.
columns = slice(None, None, None)
return self[columns].hash_values(method=method)

def hash_values(self, method="murmur3"):
bdice marked this conversation as resolved.
Show resolved Hide resolved
"""Compute the hash of values in each row.

Parameters
----------
method : {'murmur3', 'md5'}, default 'murmur3'
Hash function to use:
* murmur3: MurmurHash3 hash function.
* md5: MD5 hash function.

Returns
-------
Series
A Series with hash values.

Examples
--------
>>> import cudf
>>> df = cudf.DataFrame({"a": [10, 120, 30], "b": [0.0, 0.25, 0.50]})
>>> df
a b
0 10 0.00
1 120 0.25
2 30 0.50
>>> df.hash_values(method="murmur3")
0 -330519225
1 -397962448
2 -1345834934
dtype: int32
>>> df.hash_values(method="md5")
0 57ce879751b5169c525907d5c563fae1
1 948d6221a7c4963d4be411bcead7e32b
2 fe061786ea286a515b772d91b0dfcd70
dtype: object
"""
return Series._from_data(
{None: table_to_hash._hash(method=method)}, index=self.index
{None: self._hash(method=method)}, index=self.index
)

def partition_by_hash(self, columns, nparts, keep_index=True):
Expand Down
10 changes: 9 additions & 1 deletion python/cudf/cudf/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3501,7 +3501,15 @@ def hash_values(self, method="murmur3"):
2 30
dtype: int64
>>> series.hash_values(method="murmur3")
array([-1930516747, 422619251, -941520876], dtype=int32)
0 -1930516747
1 422619251
2 -941520876
dtype: int32
>>> series.hash_values(method="md5")
0 7be4bbacbfdb05fb3044e36c22b41e8b
1 947ca8d2c5f0f27437f156cfbfab0969
2 d0580ef52d27c043c8e341fd5039b166
dtype: object
"""
return Series._from_data(
{None: self._hash(method=method)}, index=self.index
Expand Down