Skip to content

BUG: hash_pandas_object ignores optional arguments when the input is a DataFrame. #42049

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 3 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,7 @@ Other
- Bug in :class:`Series` backed by :class:`DatetimeArray` or :class:`TimedeltaArray` sometimes failing to set the array's ``freq`` to ``None`` (:issue:`41425`)
- Bug in creating a :class:`Series` from a ``range`` object that does not fit in the bounds of ``int64`` dtype (:issue:`30173`)
- Bug in creating a :class:`Series` from a ``dict`` with all-tuple keys and an :class:`Index` that requires reindexing (:issue:`41707`)
- Bug in :func:`pandas.util.hash_pandas_object` not recognizing ``hash_key``, ``encoding`` and ``categorize`` when the input object type is a :class:`DataFrame` (:issue:`41404`)

.. ---------------------------------------------------------------------------

Expand Down
5 changes: 4 additions & 1 deletion pandas/core/util/hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ def hash_pandas_object(
ser = Series(h, index=obj.index, dtype="uint64", copy=False)

elif isinstance(obj, ABCDataFrame):
hashes = (hash_array(series._values) for _, series in obj.items())
hashes = (
hash_array(series._values, encoding, hash_key, categorize)
Copy link
Member

Choose a reason for hiding this comment

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

Since encoding also wasn't passed before, can you add a test that ensures we respect encoding too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a test_df_encoding to confirm that hash values can change when the different encodings are specified.

for _, series in obj.items()
)
num_items = len(obj.columns)
if index:
index_hash_generator = (
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/util/test_hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,32 @@ def test_hash_keys():
assert (a != b).all()


def test_df_hash_keys():
# DataFrame version of the test_hash_keys.
# https://github.com/pandas-dev/pandas/issues/41404
obj = DataFrame({"x": np.arange(3), "y": list("abc")})

a = hash_pandas_object(obj, hash_key="9876543210123456")
b = hash_pandas_object(obj, hash_key="9876543210123465")

assert (a != b).all()


def test_df_encoding():
# Check that DataFrame recognizes optional encoding.
# https://github.com/pandas-dev/pandas/issues/41404
# https://github.com/pandas-dev/pandas/pull/42049
obj = DataFrame({"x": np.arange(3), "y": list("a+c")})

a = hash_pandas_object(obj, encoding="utf8")
b = hash_pandas_object(obj, encoding="utf7")

# Note that the "+" is encoded as "+-" in utf-7.
assert a[0] == b[0]
assert a[1] != b[1]
assert a[2] == b[2]


def test_invalid_key():
# This only matters for object dtypes.
msg = "key should be a 16-byte string encoded"
Expand Down