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

feat: Add column format option to iter rows #3681

Merged
merged 7 commits into from
Jan 21, 2025

Conversation

colin-ho
Copy link
Contributor

@colin-ho colin-ho commented Jan 14, 2025

Addresses: #3634

Add an option to iter_rows to decude the format of columns during iteration, either Python (default), Arrow, or Numpy.

@github-actions github-actions bot added the feat label Jan 14, 2025
Copy link

codspeed-hq bot commented Jan 14, 2025

CodSpeed Performance Report

Merging #3681 will not alter performance

Comparing colin/iter-rows-with-col-format (85317a2) with main (03fea9c)

Summary

✅ 27 untouched benchmarks

@colin-ho colin-ho marked this pull request as ready for review January 15, 2025 00:44
@colin-ho colin-ho requested a review from jaychia January 15, 2025 01:01
Copy link

codecov bot commented Jan 15, 2025

Codecov Report

Attention: Patch coverage is 89.47368% with 2 lines in your changes missing coverage. Please review.

Project coverage is 77.91%. Comparing base (c932ec9) to head (85317a2).
Report is 26 commits behind head on main.

Files with missing lines Patch % Lines
daft/dataframe/dataframe.py 89.47% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3681      +/-   ##
==========================================
- Coverage   78.06%   77.91%   -0.16%     
==========================================
  Files         728      727       -1     
  Lines       89967    91219    +1252     
==========================================
+ Hits        70236    71074     +838     
- Misses      19731    20145     +414     
Files with missing lines Coverage Δ
daft/dataframe/dataframe.py 85.39% <89.47%> (-0.13%) ⬇️

... and 125 files with indirect coverage changes

@colin-ho
Copy link
Contributor Author

Offline: allow python and arrow column format since numpy is only for numeric. If user wants numpy they should be able to convert themselves

@colin-ho colin-ho force-pushed the colin/iter-rows-with-col-format branch from 43ea5cc to 35adce9 Compare January 17, 2025 04:35
@colin-ho
Copy link
Contributor Author

Example from the linked issue, which is much faster now with iter_rows(format="arrow"). The nested scalar can be converted to numpy via to_numpy()

import numpy as np
import daft

np.random.seed(0)
n_rows = 1_000
list_size = 100_000
data = {"list": np.random.randint(0, 256, (n_rows, list_size), dtype=np.uint8)}
df = daft.from_pydict(data)

for row in df.iter_rows(column_format="arrow"):
    v = row["list"]
    print(v.values.to_numpy())

@jaychia jaychia removed their request for review January 17, 2025 18:35
@jaychia
Copy link
Contributor

jaychia commented Jan 17, 2025

Feel free to re-add me as reviewer once we work in the offline discussions yesterday around using arrow!

@colin-ho colin-ho requested a review from jaychia January 17, 2025 19:21
@colin-ho
Copy link
Contributor Author

Feel free to re-add me as reviewer once we work in the offline discussions yesterday around using arrow!

Ready for another pass, thanks!

Copy link
Contributor

@jaychia jaychia left a comment

Choose a reason for hiding this comment

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

Any quick numbers for speedup to expect here?

) -> Iterator[Dict[str, Any]]:
"""Return an iterator of rows for this dataframe.

Each row will be a Python dictionary of the form { "key" : value, ... }. If you are instead looking to iterate over
entire partitions of data, see: :meth:`df.iter_partitions() <daft.DataFrame.iter_partitions>`.

By default, Daft will convert the columns to Python lists for easy consumption. However, for nested data such as List or Struct arrays, this can be expensive.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add some comments here also about how we determine the appropriate Python types?

I think for example tensor type gets converted to numpy arrays. Not sure if there is special handling for other logical types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good!

@colin-ho
Copy link
Contributor Author

import numpy as np
import daft
import time

np.random.seed(0)
n_rows = 1_000
list_size = 100_000
data = {"list": np.random.randint(0, 256, (n_rows, list_size), dtype=np.uint8)}
df = daft.from_pydict(data)

start_time = time.time()
for row in df.iter_rows(column_format="arrow"):
    v = row["list"]
    np_values = v.values.to_numpy()

print(f"Arrow to numpy: {time.time() - start_time} seconds")

start_time = time.time()
for row in df.iter_rows(column_format="python"):
    pass

print(f"Python: {time.time() - start_time} seconds")

Arrow to numpy: 0.0013179779052734375 seconds
Python: 12.269848108291626 seconds

@colin-ho colin-ho merged commit 1d18308 into main Jan 21, 2025
43 checks passed
@colin-ho colin-ho deleted the colin/iter-rows-with-col-format branch January 21, 2025 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants