Skip to content

Commit 4fd68a7

Browse files
authored
Implement lazy Array.__iter__ (#7905)
This doesn't fix the original issue pandas-dev/pandas#38645, but hopefully it'll make it easier for pandas to know that it should sanitize dask.arrays.
1 parent 25227d5 commit 4fd68a7

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

dask/array/core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,10 @@ def name(self, val):
15021502
"please set ``._name``"
15031503
)
15041504

1505+
def __iter__(self):
1506+
for i in range(len(self)):
1507+
yield self[i]
1508+
15051509
__array_priority__ = 11 # higher than numpy.ndarray and numpy.matrix
15061510

15071511
def __array__(self, dtype=None, **kwargs):

dask/array/tests/test_array_core.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4210,6 +4210,21 @@ def test_normalize_chunks_nan():
42104210
assert "auto" in str(info.value)
42114211

42124212

4213+
def test_pandas_from_dask_array():
4214+
pd = pytest.importorskip("pandas")
4215+
from dask.dataframe._compat import PANDAS_GT_130, PANDAS_GT_131
4216+
4217+
a = da.ones((12,), chunks=4)
4218+
s = pd.Series(a, index=range(12))
4219+
4220+
if PANDAS_GT_130 and not PANDAS_GT_131:
4221+
# https://github.com/pandas-dev/pandas/issues/38645
4222+
assert s.dtype != a.dtype
4223+
else:
4224+
assert s.dtype == a.dtype
4225+
assert_eq(s.values, a)
4226+
4227+
42134228
def test_from_zarr_unique_name():
42144229
zarr = pytest.importorskip("zarr")
42154230
a = zarr.array([1, 2, 3])

dask/dataframe/_compat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
PANDAS_GT_120 = PANDAS_VERSION >= parse_version("1.2.0")
1212
PANDAS_GT_121 = PANDAS_VERSION >= parse_version("1.2.1")
1313
PANDAS_GT_130 = PANDAS_VERSION >= parse_version("1.3.0")
14+
PANDAS_GT_131 = PANDAS_VERSION >= parse_version("1.3.1")
1415

1516

1617
if PANDAS_GT_100:

0 commit comments

Comments
 (0)