You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Dataframe index is not sorted after .set_index() #3376
From the docs+source it appears that the output of ddf.set_index() should be sorted by index, but this doesn't seem to be the case.
import dask, dask.dataframe as dd, distributed
In [1]: c = distributed.Client('localhost:8786')
In [2]: ddf = dd.read_csv('gs://<bucket>/2018-01-*.csv.gz', compression='gzip').set_index('uid').compute()
In [3]: ddf.index.is_monotonic
Out[3]: False
In [4]: ddf.head().index
Out[4]:
Index(['00004cd2-8a03-4dc7-85a9-f32ded88d6f6',
'00004cd2-8a03-4dc7-85a9-f32ded88d6f6',
'00009051-c962-460b-8090-c1c9d45df180',
'00009051-c962-460b-8090-c1c9d45df180',
'00009051-c962-460b-8090-c1c9d45df180'],
dtype='object', name='uid')
The inputs are randomly ordered, and the output appears to be mostly-but-not-quite sorted; maybe sorted within each partition but not reassembled correctly?
I suspect that it is the reverse, data has been sorted between partitions
but not within. My guess is that this does the minimum work to ensure that
partitions/divisions are consistent. Sorting the index on set_index seems
sensible to me. Any interest in submitting a PR?
If memory serves this was discussed before, I'm a little surprised to see
that it was not done. It may be that there were decent counter-arguments
at the time.
On Thu, Apr 5, 2018 at 12:29 PM, Brett Naul ***@***.***> wrote:
From the docs+source it appears that the output of ddf.set_index() should
be sorted by index, but this doesn't seem to be the case.
import dask, dask.dataframe as dd, distributed
In [1]: c = distributed.Client('localhost:8786')
In [2]: ddf = dd.read_csv('gs://<bucket>/2018-01-*.csv.gz', compression='gzip').set_index('uid').compute()
In [3]: ddf.index.is_monotonic
Out[3]: False
In [4]: ddf.head().index
Out[4]:
Index(['00004cd2-8a03-4dc7-85a9-f32ded88d6f6',
'00004cd2-8a03-4dc7-85a9-f32ded88d6f6',
'00009051-c962-460b-8090-c1c9d45df180',
'00009051-c962-460b-8090-c1c9d45df180',
'00009051-c962-460b-8090-c1c9d45df180'],
dtype='object', name='uid')
The inputs are randomly ordered, and the output appears to be
mostly-but-not-quite sorted; maybe sorted within each partition but not
reassembled correctly?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#3376>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AASszAE7AjPYYrmeARuD0VCBBsCiuYXjks5tlkaHgaJpZM4TIxlW>
.
Interesting; do you think things like .groupby().apply() would be affected by this level of non-sortedness, or will everything behave OK as long as the partitions/divisions are consistent? The fact that the index isn't sorted within partitions might not be a problem per se, I was mostly just surprised since I saw several references to .set_index performing a sort.
Most pandas operations don't assume a sorted index, some do though. It
could be that operations like loc would misbehave?
Dask dataframe logic only assumes that divisions are accurate and that
pandas operations work. Sortedness probably isn't required.
The right person to ask about correct behavior here is probably
@TomAugspurger
On Thu, Apr 5, 2018 at 1:11 PM, Brett Naul ***@***.***> wrote:
Interesting; do you think things like .groupby().apply() would be
affected by this level of non-sortedness, or will everything behave OK as
long as the partitions/divisions are consistent? The fact that the index
isn't sorted within partitions might not be a problem per se, I was mostly
just surprised since I saw several references to .set_index performing a
sort.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3376 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AASszPAFnHPA0k7cban_1-C2GsxHDOq9ks5tllAsgaJpZM4TIxlW>
.
A full sort seems reasonable, though I haven't thought about it too deeply.
loc & other indexing takes some shortcuts when the index is sorted.
Tom
On Thu, Apr 5, 2018 at 12:12 PM, Matthew Rocklin <[email protected]>
wrote:
Most pandas operations don't assume a sorted index, some do though. It
could be that operations like loc would misbehave?
Dask dataframe logic only assumes that divisions are accurate and that
pandas operations work. Sortedness probably isn't required.
The right person to ask about correct behavior here is probably
@TomAugspurger
On Thu, Apr 5, 2018 at 1:11 PM, Brett Naul ***@***.***>
wrote:
> Interesting; do you think things like .groupby().apply() would be
> affected by this level of non-sortedness, or will everything behave OK as
> long as the partitions/divisions are consistent? The fact that the index
> isn't sorted within partitions might not be a problem per se, I was
mostly
> just surprised since I saw several references to .set_index performing a
> sort.
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <#3376 (comment)>, or
mute
> the thread
> <https://github.com/notifications/unsubscribe-
auth/AASszPAFnHPA0k7cban_1-C2GsxHDOq9ks5tllAsgaJpZM4TIxlW>
> .
>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3376 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABQHIvZowlfoNex-LtEpcrne84WgZtpyks5tllCVgaJpZM4TIxlW>
.
@mrocklin I poked around a bit more: I have only been able to reproduce this behavior w/ the distributed scheduler and lot of data, so unfortunately I don't have a simple test case to share (nor can I use the single-threaded scheduler for debugging).
It seems like sorting is not the issue per se, but rather the shuffle/partition itself:
ddf = dd.read_csv('gs://<bucket>/2018-01-*.csv.gz', compression='gzip').set_index('uid').persist()
# found a partition where one order violation occurred
In [90]: ddf.get_partition(255)
Out[90]:
Dask DataFrame Structure:
lat lng
npartitions=1
135c2fc8-8861-4dfd-9663-4b79438d311f float64 float64
137266de-0abd-473c-9915-1f1330d58d71 ... ...
Dask Name: get-partition, 3457 tasks
In [91]: ddf.get_partition(255).tail()
Out[91]:
lat lng
uid
f66de7bc-49c9-4a8d-8090-df3a0d61fac6 ... ...
f66de7bc-49c9-4a8d-8090-df3a0d61fac6 ... ...
f66de7bc-49c9-4a8d-8090-df3a0d61fac6 ... ...
f66eb47d-b3a3-42bc-8e76-ac739ef8d7a0 ... ...
f66eb47d-b3a3-42bc-8e76-ac739ef8d7a0 ... ...
In [92]: ddf.map_partitions(lambda df: df.index.is_monotonic).compute().all()
Out[92]:
True
The divisions themselves are in sorted order, and each partition individually is sorted, but a few pieces (in this case 6 index values, out of a 4M row dataframe) just end up in the wrong place. Any ideas how this might come to be?
On Sun, Apr 22, 2018 at 3:46 PM, Brett Naul ***@***.***> wrote:
@mrocklin <https://github.com/mrocklin> I poked around a bit more: I have
only been able to reproduce this behavior w/ the distributed scheduler and
lot of data, so unfortunately I don't have a simple test case to share (nor
can I use the single-threaded scheduler for debugging).
It seems like sorting is not the issue per se, but rather the
shuffle/partition itself:
ddf = dd.read_csv('gs://<bucket>/2018-01-*.csv.gz', compression='gzip').set_index('uid').persist()
# found a partition where one order violation occurred
In [90]: ddf.get_partition(255)
Out[90]:
Dask DataFrame Structure:
lat lng
npartitions=1
135c2fc8-8861-4dfd-9663-4b79438d311f float64 float64
137266de-0abd-473c-9915-1f1330d58d71 ... ...
Dask Name: get-partition, 3457 tasks
In [91]: ddf.get_partition(255).tail()
Out[91]:
lat lng
uid
f66de7bc-49c9-4a8d-8090-df3a0d61fac6 ... ...
f66de7bc-49c9-4a8d-8090-df3a0d61fac6 ... ...
f66de7bc-49c9-4a8d-8090-df3a0d61fac6 ... ...
f66eb47d-b3a3-42bc-8e76-ac739ef8d7a0 ... ...
f66eb47d-b3a3-42bc-8e76-ac739ef8d7a0 ... ...
In [92]: ddf.map_partitions(lambda df: df.index.is_monotonic).compute().all()
Out [93]:
True
The divisions themselves are in sorted order, and each partition
individually is sorted, but a few (in this case 6, out of a 4M row
dataframe) just end up in the wrong place. Any ideas how this might come to
be?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3376 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AASszCbaeqJ9hJrki3aqepVvsg8yWm76ks5trN4GgaJpZM4TIxlW>
.
Can you verify that you're on the latest release or master?
On Sun, Apr 22, 2018 at 3:46 PM, Brett Naul ***@***.***>
wrote:
> @mrocklin <https://github.com/mrocklin> I poked around a bit more: I
> have only been able to reproduce this behavior w/ the distributed scheduler
> and lot of data, so unfortunately I don't have a simple test case to share
> (nor can I use the single-threaded scheduler for debugging).
>
> It seems like sorting is not the issue per se, but rather the
> shuffle/partition itself:
>
> ddf = dd.read_csv('gs://<bucket>/2018-01-*.csv.gz', compression='gzip').set_index('uid').persist()
>
> # found a partition where one order violation occurred
> In [90]: ddf.get_partition(255)
> Out[90]:
> Dask DataFrame Structure:
> lat lng
> npartitions=1
> 135c2fc8-8861-4dfd-9663-4b79438d311f float64 float64
> 137266de-0abd-473c-9915-1f1330d58d71 ... ...
> Dask Name: get-partition, 3457 tasks
>
> In [91]: ddf.get_partition(255).tail()
> Out[91]:
> lat lng
> uid
> f66de7bc-49c9-4a8d-8090-df3a0d61fac6 ... ...
> f66de7bc-49c9-4a8d-8090-df3a0d61fac6 ... ...
> f66de7bc-49c9-4a8d-8090-df3a0d61fac6 ... ...
> f66eb47d-b3a3-42bc-8e76-ac739ef8d7a0 ... ...
> f66eb47d-b3a3-42bc-8e76-ac739ef8d7a0 ... ...
>
> In [92]: ddf.map_partitions(lambda df: df.index.is_monotonic).compute().all()
> Out [93]:
> True
>
> The divisions themselves are in sorted order, and each partition
> individually is sorted, but a few (in this case 6, out of a 4M row
> dataframe) just end up in the wrong place. Any ideas how this might come to
> be?
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#3376 (comment)>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AASszCbaeqJ9hJrki3aqepVvsg8yWm76ks5trN4GgaJpZM4TIxlW>
> .
>
Yep this is 0c443fd49cd63ce23f803706c7f9762f0b8bb165. I think I could come up with a reproducible example if it would help, but like I said it seems like a rare problem and I can't reproduce it on anything smaller than millions of rows. I have managed to get the same behavior w/ a subset of columns: now I have a few thousand files that look like
On Mon, Apr 23, 2018 at 11:35 AM, Brett Naul ***@***.***> wrote:
Yep this is 0c443fd. I think I could
come up with a reproducible example if it would help, but like I said it
seems like a rare problem and I can't reproduce it on anything smaller than
millions of rows. I have managed to get the same behavior w/ a subset of
columns: now I have a few thousand files that look like
i,uid
0,002862bc-67d5-43a4-bbb7-da659a987f0c
1,002862bc-67d5-43a4-bbb7-da659a987f0c
2,002862bc-67d5-43a4-bbb7-da659a987f0c
3,002862bc-67d5-43a4-bbb7-da659a987f0c
4,002862bc-67d5-43a4-bbb7-da659a987f0c
5,03b076de-9038-48d3-9609-178690ca808e
6,03b076de-9038-48d3-9609-178690ca808e
7,03b076de-9038-48d3-9609-178690ca808e
8,03b076de-9038-48d3-9609-178690ca808e
9,03b076de-9038-48d3-9609-178690ca808e
Loading these, setting the index, and computing gives the above behavior.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3376 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AASszIOdH4GnZ_KdYRi_Z7vl3khbWVsqks5trfStgaJpZM4TIxlW>
.
The following runs in about 5 minutes on my local machine:
import numpy as np, pandas as pd, dask.dataframe as dd, distributed, uuid
c = distributed.Client('localhost:8786')
ids = [uuid.uuid4() for i in range(100_000)]
df = pd.DataFrame({'uid': np.concatenate([np.repeat(uid, np.random.randint(1, 750)) for uid in ids]),
'x': 0})
dd.from_pandas(df, npartitions=4000).to_csv('/tmp/test_*.csv', index=False)
del df
ddf = dd.read_csv('/tmp/test_*.csv').set_index('uid').persist()
print(ddf.index.compute().is_monotonic) # False
For me the very first partition was incorrect:
In [35]: ddf.get_partition(0)
Out[35]:
Dask DataFrame Structure:
x
npartitions=1
00001f4f-7f25-43d0-a939-cc485a359e7e int64
00143d26-016c-427a-84ff-8d1fca4bfa08 ...
Dask Name: get-partition, 4001 tasks
In [36]: ddf.get_partition(0).tail()
Out[36]:
x
uid
f5f3f922-b22c-4537-90fe-f8a311d26f14 0
f5f3f922-b22c-4537-90fe-f8a311d26f14 0
f5f3f922-b22c-4537-90fe-f8a311d26f14 0
f5f3f922-b22c-4537-90fe-f8a311d26f14 0
f5f3f922-b22c-4537-90fe-f8a311d26f14 0
On Mon, Apr 23, 2018 at 1:53 PM, Brett Naul ***@***.***> wrote:
The following runs in about 5 minutes on my local machine:
import numpy as np, pandas as pd, dask.dataframe as dd, distributed, uuid
c = distributed.Client('localhost:8786')
ids = [uuid.uuid4() for i in range(100_000)]
df = pd.DataFrame({'uid': np.concatenate([np.repeat(uid, np.random.randint(1, 750)) for uid in ids]),
'x': 0})
dd.from_pandas(df, npartitions=4000).to_csv('/tmp/test_*.csv', index=False)
del df
ddf = dd.read_csv('/tmp/test_*.csv').set_index('uid').persist()
print(ddf.index.compute().is_monotonic) # False
For me the very first partition was incorrect:
In [35]: ddf.get_partition(0)
Out[35]:
Dask DataFrame Structure:
x
npartitions=1
00001f4f-7f25-43d0-a939-cc485a359e7e int64
00143d26-016c-427a-84ff-8d1fca4bfa08 ...
Dask Name: get-partition, 4001 tasks
In [36]: ddf.get_partition(0).tail()
Out[36]:
x
uid
f5f3f922-b22c-4537-90fe-f8a311d26f14 0
f5f3f922-b22c-4537-90fe-f8a311d26f14 0
f5f3f922-b22c-4537-90fe-f8a311d26f14 0
f5f3f922-b22c-4537-90fe-f8a311d26f14 0
f5f3f922-b22c-4537-90fe-f8a311d26f14 0
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3376 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AASszA0Wnbk00N25ZXoT50hFVHn0SX8Pks5trhT8gaJpZM4TIxlW>
.
c = distributed.Client('localhost:8786')
ids = [uuid.uuid4() for i in range(100_000)]
df = pd.DataFrame({'uid': np.concatenate([np.repeat(uid, np.random.randint(1, 750)) for uid in ids]),
'x': 0})
ddf = dd.from_pandas(df, npartitions=4000).set_index('uid').persist()
print(ddf.index.compute().is_monotonic) # False
But running w/ the default dask scheduler works fine:
ids = [uuid.uuid4() for i in range(100_000)]
df = pd.DataFrame({'uid': np.concatenate([np.repeat(uid, np.random.randint(1, 750)) for uid in ids]),
'x': 0})
ddf = dd.from_pandas(df, npartitions=4000).set_index('uid')
print(ddf.index.compute().is_monotonic) # True
On Mon, Apr 23, 2018 at 2:43 PM, Brett Naul ***@***.***> wrote:
Same behavior w/o read_csv:
c = distributed.Client('localhost:8786')
ids = [uuid.uuid4() for i in range(100_000)]
df = pd.DataFrame({'uid': np.concatenate([np.repeat(uid, np.random.randint(1, 750)) for uid in ids]),
'x': 0})
ddf = dd.from_pandas(df, npartitions=4000).set_index('uid').persist()
print(ddf.index.compute().is_monotonic) # False
But running w/ the default dask scheduler works fine:
ids = [uuid.uuid4() for i in range(100_000)]
df = pd.DataFrame({'uid': np.concatenate([np.repeat(uid, np.random.randint(1, 750)) for uid in ids]),
'x': 0})
ddf = dd.from_pandas(df, npartitions=4000).set_index('uid')
print(ddf.index.compute().is_monotonic) # False
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3376 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AASszAvukPlOzKACSCvnkf4RnaT9xajAks5triDUgaJpZM4TIxlW>
.
I recommend trying dask.set_options(tasks='shuffle') and seeing if that
has an effect with the local scheduler
On Mon, Apr 23, 2018 at 2:43 PM, Brett Naul ***@***.***>
wrote:
> Same behavior w/o read_csv:
>
> c = distributed.Client('localhost:8786')
> ids = [uuid.uuid4() for i in range(100_000)]
> df = pd.DataFrame({'uid': np.concatenate([np.repeat(uid, np.random.randint(1, 750)) for uid in ids]),
> 'x': 0})
> ddf = dd.from_pandas(df, npartitions=4000).set_index('uid').persist()
> print(ddf.index.compute().is_monotonic) # False
>
> But running w/ the default dask scheduler works fine:
>
> ids = [uuid.uuid4() for i in range(100_000)]
> df = pd.DataFrame({'uid': np.concatenate([np.repeat(uid, np.random.randint(1, 750)) for uid in ids]),
> 'x': 0})
> ddf = dd.from_pandas(df, npartitions=4000).set_index('uid')
> print(ddf.index.compute().is_monotonic) # False
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#3376 (comment)>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AASszAvukPlOzKACSCvnkf4RnaT9xajAks5triDUgaJpZM4TIxlW>
> .
>
Yep, shuffle='tasks' gives the same incorrect behavior. I tried decreasing the size of the dataframe but with fewer partitions it seems to always end up sorted (4000 breaks, 2000 is OK). Not sure what to make of that 🙃
Activity
mrocklin commentedon Apr 5, 2018
bnaul commentedon Apr 5, 2018
Interesting; do you think things like
.groupby().apply()would be affected by this level of non-sortedness, or will everything behave OK as long as the partitions/divisions are consistent? The fact that the index isn't sorted within partitions might not be a problem per se, I was mostly just surprised since I saw several references to .set_index performing a sort.mrocklin commentedon Apr 5, 2018
TomAugspurger commentedon Apr 5, 2018
mrocklin commentedon Apr 19, 2018
@bnaul do you have any interest in fixing this? It seems like a well isolated contribution.
bnaul commentedon Apr 20, 2018
I am interested; will try to take a look within the next few days
bnaul commentedon Apr 22, 2018
@mrocklin I poked around a bit more: I have only been able to reproduce this behavior w/ the distributed scheduler and lot of data, so unfortunately I don't have a simple test case to share (nor can I use the single-threaded scheduler for debugging).
It seems like sorting is not the issue per se, but rather the shuffle/partition itself:
The divisions themselves are in sorted order, and each partition individually is sorted, but a few pieces (in this case 6 index values, out of a 4M row dataframe) just end up in the wrong place. Any ideas how this might come to be?
mrocklin commentedon Apr 22, 2018
mrocklin commentedon Apr 22, 2018
bnaul commentedon Apr 23, 2018
Yep this is
0c443fd49cd63ce23f803706c7f9762f0b8bb165. I think I could come up with a reproducible example if it would help, but like I said it seems like a rare problem and I can't reproduce it on anything smaller than millions of rows. I have managed to get the same behavior w/ a subset of columns: now I have a few thousand files that look likeLoading these, setting the index, and computing gives the above behavior.
mrocklin commentedon Apr 23, 2018
bnaul commentedon Apr 23, 2018
The following runs in about 5 minutes on my local machine:
For me the very first partition was incorrect:
mrocklin commentedon Apr 23, 2018
bnaul commentedon Apr 23, 2018
Same behavior w/o
read_csv:But running w/ the default dask scheduler works fine:
mrocklin commentedon Apr 23, 2018
mrocklin commentedon Apr 23, 2018
bnaul commentedon Apr 23, 2018
Yep,
shuffle='tasks'gives the same incorrect behavior. I tried decreasing the size of the dataframe but with fewer partitions it seems to always end up sorted (4000 breaks, 2000 is OK). Not sure what to make of that 🙃bnaul commentedon May 16, 2019
Not sure what changed but when I run the above example now the index is sorted. Guess I'll close...?