Repository navigation
Description
What happened:
dask.dataframe.partitionquantiles.partition_quantiles finds an incorrect minimum and maximum value with large integer inputs.
What you expected to happen:
For partition_quantiles to find correct minimum and maximum.
Minimal Complete Verifiable Example:
In [1]: import numpy as np
...: import pandas as pd
...: import dask.dataframe as dd
...: from dask.dataframe.partitionquantiles import partition_quantiles
In [2]: pandas_df = pd.DataFrame({
...: 'a': np.array([612509347682975743, 616762138058293247], dtype=np.uint64)
...: })
...: dask_df = dd.from_pandas(pandas_df, npartitions=1)
In [3]: partition_quantiles(dask_df.a, npartitions=1).compute()
Out[3]:
0.0 612509347682975744
1.0 616762138058293248
Name: a, dtype: uint64
In [4]: dask_df.a.min().compute()
Out[4]: 612509347682975743Anything else we need to know?:
As more commentary on this bug: this is the simplest example I could come up with, but I've been having related issues with set_index on a uint64 column. Specifically, if I load data from an unsorted CSV, and the minimum value is towards the end of the dataset, then calling set_index leaves the minimum value in the last partition!
I can't share the full dataset but this is really interesting:
df = dd.read_csv(path, dtype={'uint_col': np.uint64})
df = df.set_index('uint_col')
df_min_value = df.index.min().compute()
# 612509347682975743
partition_min_value = df.partitions[-1].compute().index.min()
# 612509347682975743
divisions_min_value = df.divisions[0]
# 612509347682975744
df_min_value < divisions_min_value
# True
partition_min_value < divisions_min_value
# False
partition_min_value < np.uint64(divisions_min_value)
# True
df_min_value == partition_min_value
# True
type(df_min_value)
# numpy.int64
type(partition_min_value)
# numpy.uint64
type(divisions_min_value)
# int🤯
So I'm guessing that there are some buffer overflow issues with uints?
Edit: It seems like it might be helpful to store more things in the same datatype as the index, and prevent unnecessary coercion? For example, this seems like a bug:
df.index.min().dtype
# dtype('uint64')
df.index.min().compute().dtype
# dtype('int64')I don't understand why the dtype should change there.
Environment:
- Dask version: 2021.2.0
- Python version: 3.8.3
- Operating System: Mac OSX 11.1
- Install method (conda, pip, source): pip
Activity
eriknw commentedon Mar 2, 2021
Thanks for the report @kylebarron!
I can reproduce. I believe the issue arises from using linear interpolation when operating on integer dtypes. Observe:
This could be solved by a few different ways:
(1) is simple and probably good enough, but may change behavior for existing code in unexpected ways.
(2) more complicated and introduces an extra min/max computation, but computing the min/max should be very fast.
(3) is my least favorite choice.
I lean towards (2).
eriknw commentedon Mar 2, 2021
Always using
interpolation="nearest"for integer dtypes is probably a very reasonable thing to do. It will help the compression step when combining quantiles from blocks. See relevant comment here: #1261 (comment) (if only we considered domain more carefully back then!).Another option is to see if the min/max value from the percentile calculation isn't safe to cast to float, and to move the the next closest floating point value. This may have the least impact to existing code:
kylebarron commentedon Mar 2, 2021
Glad it's reproducible! It looks like the relevant code is here?
dask/dask/dataframe/partitionquantiles.py
Lines 411 to 418 in 8663c6b
So it looks like the float output is being rounded and coerced back to integer as you mentioned above.
I don't know Dask super well so I don't have a strong opinion on the best fix. Using
interpolation="nearest"for integer types seems fine to me.One small comment here is that it can be beneficial to know tight bounds on your partition, and it seems like this approach necessarily creates larger bounds than necessary? But maybe that's better to prevent a backwards-incompatible change?
eriknw commentedon Mar 2, 2021
Yup, you're understanding of all of the above is correct.
If you want to make a PR to use nearest interpolation for integer dtypes, @kylebarron, that would be great! We're here to help if you need it.
But just to be clear, you don't need to make a PR if you don't want to. I can take care of it if you don't. The reproducible bug report is already super-valuable.