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

Expression iterator #1800

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions packages/vaex-core/vaex/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,21 @@ def to_pandas_series(self):
import pandas as pd
return pd.Series(self.values)

def __len__(self):
return len(self.df)

def __iter__(self):
n_samples = len(self)
chunk_size = 1000

def iterator():
for ndx in range(0, n_samples, chunk_size):
i1, i2 = ndx, min(ndx + chunk_size, n_samples)
values = self[i1:i2].values.tolist()
for value in values:
yield value
return iterator()

def __getitem__(self, slicer):
"""Provides row and optional field access (struct arrays) via bracket notation.

Expand Down
12 changes: 12 additions & 0 deletions tests/getattr_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ def test_expression(ds_local):
assert np.array(dss).T.tolist() == [(ds.y/10).values.tolist(), (ds.x/5).values.tolist()]


def test_expression_iterator(ds_local):
ds = ds_local

assert len(ds) == len(ds.x)
assert len(ds[ds.x > 1]) == len(ds[ds.x > 1].x)

ds = ds_local.head(10)
for (x_value, y_value), x_iter_value, y_iter_value in zip(ds[['x', 'y']].values, ds.x, ds.y):
assert x_value == x_iter_value
assert y_value == y_iter_value


@pytest.mark.skip(reason="Not implemented yet, should work, might need refactoring of copy")
def test_expression_virtual(ds_local):
ds = ds_local
Expand Down