Skip to content

[data/preprocessors] feat: allow hasher to run on append mode - #50632

Merged
richardliaw merged 1 commit into
ray-project:masterfrom
martinbomio:martinbomio/hasher-append-mode
Feb 26, 2025
Merged

richardliaw merged 1 commit into
ray-project:masterfrom
martinbomio:martinbomio/hasher-append-mode

Conversation

@martinbomio

@martinbomio martinbomio commented Feb 15, 2025 •

Copy link
Copy Markdown
Contributor

Why are these changes needed?

This is part of #48133. Continuing the approach taken in #49426, make all the hashers work in append mode

Related issue number

#49426

Checks

Comment thread python/ray/data/preprocessors/hasher.py Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@richardliaw wondering what your thoughts are on these changes. The FeatureHasher already create new columns, but it drops the original columns from the result dataset.
How can we enable/disable the dropping of the columns?

In these changes I opted for having the output_columns, which should name the transformed columns (instead of naming them hash_{i}) and if the output columns are provided, it will also make it so that the hasher doesn't drop the original columns.

Not super happy with this, there are a few other options we could take, like having an append, that controls whether to drop the original or not, but that won't allow us to name the output columns differently (as we do on other preprocessors)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm, I think it makes sense to only have a single output_column here, and disable the dropping of columns altogether?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@richardliaw not sure I follow. Right now the hasher preprocessor outputs num_features new columns. Are you saying that you want to change it so it always returns a single column with a a list of num_features elements?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yes, this would save us from headache in situations with wider tables

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done in ec6f311

@martinbomio
martinbomio force-pushed the martinbomio/hasher-append-mode branch from 23ff71d to ec6f311 Compare February 21, 2025 19:00
@martinbomio
martinbomio marked this pull request as ready for review February 21, 2025 19:00
@martinbomio
martinbomio requested a review from a team as a code owner February 21, 2025 19:00
@martinbomio martinbomio changed the title feat: allow hasher to run on append mode [data/preprocessors] feat: allow hasher to run on append mode Feb 21, 2025
@martinbomio
martinbomio force-pushed the martinbomio/hasher-append-mode branch 2 times, most recently from 97c6a27 to 76b1834 Compare February 21, 2025 20:06
@martinbomio
martinbomio force-pushed the martinbomio/hasher-append-mode branch from 879239d to 63f94ab Compare February 24, 2025 03:03
@richardliaw
richardliaw enabled auto-merge (squash) February 24, 2025 18:52
@github-actions github-actions Bot added the go add ONLY when ready to merge, run all tests label Feb 24, 2025
auto-merge was automatically disabled February 24, 2025 21:04

Head branch was pushed to by a user without write access

@martinbomio
martinbomio force-pushed the martinbomio/hasher-append-mode branch from 63f94ab to f0fa89c Compare February 24, 2025 21:04
richardliaw pushed a commit that referenced this pull request Feb 24, 2025
… mode (#50847)

<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

This is part of #48133.
Continuing the approach taken in
#49426, make all the vectorizers
work in append mode.
Took similar approach to #50632
where the vectorizers also change to output a single column instead of
one column per token or per num_feature

## Related issue number

#49426

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [x] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

Signed-off-by: Martin Bomio <[email protected]>
@martinbomio
martinbomio force-pushed the martinbomio/hasher-append-mode branch from f0fa89c to a20798c Compare February 25, 2025 02:50
@martinbomio
martinbomio force-pushed the martinbomio/hasher-append-mode branch from a20798c to 89515e0 Compare February 25, 2025 14:05
@richardliaw
richardliaw merged commit 8aabebf into ray-project:master Feb 26, 2025
kevin85421 pushed a commit to kevin85421/ray that referenced this pull request Feb 28, 2025
… mode (ray-project#50847)

<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

This is part of ray-project#48133.
Continuing the approach taken in
ray-project#49426, make all the vectorizers
work in append mode.
Took similar approach to ray-project#50632
where the vectorizers also change to output a single column instead of
one column per token or per num_feature

## Related issue number

ray-project#49426

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [x] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

Signed-off-by: Martin Bomio <[email protected]>
Signed-off-by: kaihsun <[email protected]>
kevin85421 pushed a commit to kevin85421/ray that referenced this pull request Feb 28, 2025
…oject#50632)

<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

This is part of ray-project#48133.
Continuing the approach taken in
ray-project#49426, make all the hashers work
in append mode

## Related issue number

ray-project#49426

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [ ] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [x] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

Signed-off-by: Martin Bomio <[email protected]>
Signed-off-by: kaihsun <[email protected]>
xsuler pushed a commit to antgroup/ant-ray that referenced this pull request Mar 4, 2025
… mode (ray-project#50847)

<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

This is part of ray-project#48133.
Continuing the approach taken in
ray-project#49426, make all the vectorizers
work in append mode.
Took similar approach to ray-project#50632
where the vectorizers also change to output a single column instead of
one column per token or per num_feature

## Related issue number

ray-project#49426

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [x] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

Signed-off-by: Martin Bomio <[email protected]>
xsuler pushed a commit to antgroup/ant-ray that referenced this pull request Mar 4, 2025
…oject#50632)

<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

This is part of ray-project#48133.
Continuing the approach taken in
ray-project#49426, make all the hashers work
in append mode

## Related issue number

ray-project#49426

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [ ] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [x] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

Signed-off-by: Martin Bomio <[email protected]>
park12sj pushed a commit to park12sj/ray that referenced this pull request Mar 18, 2025
… mode (ray-project#50847)

<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

This is part of ray-project#48133.
Continuing the approach taken in
ray-project#49426, make all the vectorizers
work in append mode.
Took similar approach to ray-project#50632
where the vectorizers also change to output a single column instead of
one column per token or per num_feature

## Related issue number

ray-project#49426

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [x] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

Signed-off-by: Martin Bomio <[email protected]>
park12sj pushed a commit to park12sj/ray that referenced this pull request Mar 18, 2025
…oject#50632)

<!-- Thank you for your contribution! Please review
https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

This is part of ray-project#48133.
Continuing the approach taken in
ray-project#49426, make all the hashers work
in append mode

## Related issue number

ray-project#49426

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [ ] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [x] I've added any new APIs to the API Reference. For example, if I
added a
method in Tune, I've added it in `doc/source/tune/api/` under the
           corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
   - [x] Unit tests
   - [ ] Release tests
   - [ ] This PR is not tested :(

Signed-off-by: Martin Bomio <[email protected]>
richardliaw pushed a commit that referenced this pull request Nov 24, 2026
… Concatenator, and PowerTransformer (#67483)

## Why are these changes needed?

This PR fixes silent `NaN` corruption on non-default `pd.DataFrame`
indices in `Concatenator` and `FeatureHasher`, and exceptions on
zero-row batches in `FeatureHasher` and `PowerTransformer`:

1. **Preserve `df.index` in `Concatenator._transform_pandas` and
`FeatureHasher._transform_pandas`
(`TestConcatenator.test_non_default_index`, `test_feature_hasher`):**
Both preprocessors previously assigned `df.loc[:, output_col] =
pd.Series(list(concatenated))` without `index=df.index`. When
`transform_batch` is called on a sliced, filtered, or custom-indexed
`pd.DataFrame` whose index is not `RangeIndex(0, len(df))` (e.g.,
`index=[10, 20]`), pandas aligns the newly created `Series` index
(`0..n-1`) against `df.index` and silently replaces non-overlapping
index positions with `NaN`. Passing `index=df.index` preserves row
alignment.
2. **Handle zero-row batches and hoist column-name hashing in
`FeatureHasher._transform_pandas` (`test_feature_hasher`):**
- On an empty `pd.DataFrame` (`len(df) == 0`), `df.loc[:,
self._columns].apply(row_feature_hasher, axis=1, result_type="expand")`
returns a 0-row DataFrame with `self._columns` rather than `hash_0 ..
hash_{num_features - 1}`, causing `feature_columns[hash_columns]` to
raise `KeyError`. Returning an empty `object`-dtype `Series` indexed by
`df.index` when `df.empty` avoids the crash.
- Precomputes `simple_hash(column, self._num_features)` once per column
per batch rather than recomputing the SHA-256 column-name hash inside
`row_feature_hasher` on every row.
3. **Return `pd.Series(result, index=s.index)` in
`PowerTransformer._transform_pandas` (`yeo-johnson`,
`test_power_transformer_empty_batch`):**
Unlike the `box-cox` branch (which returns a `pd.Series`), the
`yeo-johnson` branch returned a raw `np.ndarray`. On an empty DataFrame
(`len(df) == 0`), `pandas.DataFrame.transform` treats a non-`Series`
return value in `apply_empty_result` as a scalar reduction and raises
`ValueError: Transform function failed`. Returning `pd.Series(result,
index=s.index)` makes `yeo-johnson` consistent with `box-cox` on empty
batches.

### Related work & acknowledgements
- Builds on the preprocessor null-propagation and batch-handling
hardening in #65272 by @AarryaSaraf and `FeatureHasher` append-mode
support in #50632 by @martinbomio.

## Related issue number

N/A

## Checks

- [x] I've signed off every commit(by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run `scripts/format.sh` to lint the changes in this PR.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API reference. For example, if I
added a method in Python, I've added it to `doc/source/tune/api/` under
the corresponding `.rst` file.
- [x] I've made sure the tests are passing. Note that there might be a
few flaky tests, see the recent failures at https://flakey-tests.ray.io/
- Testing Strategy
- [x] Unit tests
(`python/ray/data/tests/preprocessors/test_concatenator.py`,
`python/ray/data/tests/preprocessors/test_hasher.py`,
`python/ray/data/tests/preprocessors/test_transformer.py`)
   - [ ] Release tests
   - [ ] This PR is not tested :(

---------

Signed-off-by: Parth Vadera <[email protected]>
Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-backlog go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants