Repository navigation
Description
I'm having a problem in filtering and loading a partitioned parquet file using dask, which I show below.
I'm including full code for reproducibility.
Versions
dask==0.17.0
pandas==0.22.0
fastparquet==0.1.4
Init code
Imports
import numpy as np
import pandas as pd
import dask.dataframe as dd
import random
import fastparquetGeneration and plotting functions
#generate dummy df
def generate_df(size):
d = {
'dates':
pd.date_range('1980-01-01', periods=size, freq='1T'),
'signal1':
np.random.normal(0, 0.3, size=size).cumsum() + 50,
'signal2':
np.random.normal(0, 0.3, size=size).cumsum() + 50,
'signal3':
np.random.normal(0, 0.3, size=size).cumsum() + 50,
'fake_categorical1':
np.random.choice(['A', 'B', 'C'], size=size),
'fake_categorical2':
np.random.choice(['D', 'E', 'F'], size=size)
}
df = pd.DataFrame(d)
return df.melt(id_vars=['dates', 'fake_categorical1', 'fake_categorical2'])Generate df
Let's generate a small df:
small_df = generate_df(10)
small_df| dates | fake_categorical1 | fake_categorical2 | variable | value | |
|---|---|---|---|---|---|
| 0 | 1980-01-01 00:00:00 | C | D | signal1 | 50.106769 |
| 1 | 1980-01-01 00:01:00 | C | F | signal1 | 50.134314 |
| 2 | 1980-01-01 00:02:00 | B | F | signal1 | 50.049454 |
| 3 | 1980-01-01 00:03:00 | C | E | signal1 | 50.204364 |
| 4 | 1980-01-01 00:04:00 | A | D | signal1 | 50.337799 |
| 5 | 1980-01-01 00:05:00 | A | D | signal1 | 50.716083 |
| 6 | 1980-01-01 00:06:00 | B | E | signal1 | 50.389717 |
| 7 | 1980-01-01 00:07:00 | C | E | signal1 | 50.295345 |
| 8 | 1980-01-01 00:08:00 | C | E | signal1 | 50.221086 |
| 9 | 1980-01-01 00:09:00 | C | E | signal1 | 50.074217 |
| 10 | 1980-01-01 00:00:00 | C | D | signal2 | 50.475801 |
| 11 | 1980-01-01 00:01:00 | C | F | signal2 | 49.874271 |
| 12 | 1980-01-01 00:02:00 | B | F | signal2 | 49.745380 |
| 13 | 1980-01-01 00:03:00 | C | E | signal2 | 49.967490 |
| 14 | 1980-01-01 00:04:00 | A | D | signal2 | 49.822340 |
| 15 | 1980-01-01 00:05:00 | A | D | signal2 | 49.740567 |
| 16 | 1980-01-01 00:06:00 | B | E | signal2 | 49.393413 |
| 17 | 1980-01-01 00:07:00 | C | E | signal2 | 49.151365 |
| 18 | 1980-01-01 00:08:00 | C | E | signal2 | 49.381807 |
| 19 | 1980-01-01 00:09:00 | C | E | signal2 | 48.647324 |
| 20 | 1980-01-01 00:00:00 | C | D | signal3 | 49.837980 |
| 21 | 1980-01-01 00:01:00 | C | F | signal3 | 49.787703 |
| 22 | 1980-01-01 00:02:00 | B | F | signal3 | 49.616496 |
| 23 | 1980-01-01 00:03:00 | C | E | signal3 | 49.674050 |
| 24 | 1980-01-01 00:04:00 | A | D | signal3 | 49.987540 |
| 25 | 1980-01-01 00:05:00 | A | D | signal3 | 49.667552 |
| 26 | 1980-01-01 00:06:00 | B | E | signal3 | 49.553683 |
| 27 | 1980-01-01 00:07:00 | C | E | signal3 | 50.074661 |
| 28 | 1980-01-01 00:08:00 | C | E | signal3 | 50.526340 |
| 29 | 1980-01-01 00:09:00 | C | E | signal3 | 51.246639 |
Here is where the problem shows
Write and read non partitioned parquet:
Save a non partitioned parquet:
fastparquet.write('file_non_partitioned', small_df, compression='snappy',
file_scheme='hive', write_index=False)Read it with dask:
df_non_partitioned = dd.read_parquet('file_non_partitioned')No problems showing head():
df_non_partitioned.head()| dates | fake_categorical1 | fake_categorical2 | variable | value | |
|---|---|---|---|---|---|
| 0 | 1980-01-01 00:00:00 | C | D | signal1 | 50.106769 |
| 1 | 1980-01-01 00:01:00 | C | F | signal1 | 50.134314 |
| 2 | 1980-01-01 00:02:00 | B | F | signal1 | 50.049454 |
| 3 | 1980-01-01 00:03:00 | C | E | signal1 | 50.204364 |
| 4 | 1980-01-01 00:04:00 | A | D | signal1 | 50.337799 |
And no problems filtering on a value of a column and computing... cool! The problem comes with partitioned parquet (see below).
df_non_partitioned[df_non_partitioned.fake_categorical1=='A'].compute()| dates | fake_categorical1 | fake_categorical2 | variable | value | |
|---|---|---|---|---|---|
| 4 | 1980-01-01 00:04:00 | A | D | signal1 | 50.337799 |
| 5 | 1980-01-01 00:05:00 | A | D | signal1 | 50.716083 |
| 14 | 1980-01-01 00:04:00 | A | D | signal2 | 49.822340 |
| 15 | 1980-01-01 00:05:00 | A | D | signal2 | 49.740567 |
| 24 | 1980-01-01 00:04:00 | A | D | signal3 | 49.987540 |
| 25 | 1980-01-01 00:05:00 | A | D | signal3 | 49.667552 |
# Write and read partitioned parquet
Save a partitioned parquet:
fastparquet.write('file_partitioned', small_df, compression='snappy',
file_scheme='hive', write_index=False,
partition_on=['fake_categorical1','fake_categorical2'])Read it with dask:
df_partitioned = dd.read_parquet('file_partitioned')No problems showing head():
df_partitioned.head()| dates | variable | value | fake_categorical1 | fake_categorical2 | |
|---|---|---|---|---|---|
| 0 | 1980-01-01 00:04:00 | signal1 | 50.337799 | A | D |
| 1 | 1980-01-01 00:05:00 | signal1 | 50.716083 | A | D |
| 2 | 1980-01-01 00:04:00 | signal2 | 49.822340 | A | D |
| 3 | 1980-01-01 00:05:00 | signal2 | 49.740567 | A | D |
| 4 | 1980-01-01 00:04:00 | signal3 | 49.987540 | A | D |
No problems running compute() on the full dataframe:
df_partitioned.compute().head()| dates | variable | value | fake_categorical1 | fake_categorical2 | |
|---|---|---|---|---|---|
| 0 | 1980-01-01 00:04:00 | signal1 | 50.337799 | A | D |
| 1 | 1980-01-01 00:05:00 | signal1 | 50.716083 | A | D |
| 2 | 1980-01-01 00:04:00 | signal2 | 49.822340 | A | D |
| 3 | 1980-01-01 00:05:00 | signal2 | 49.740567 | A | D |
| 4 | 1980-01-01 00:04:00 | signal3 | 49.987540 | A | D |
But when trying to filtering on a column before computing it doesnt work:
df_partitioned[df_partitioned.fake_categorical1=='A'].compute()Error and more examples
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-33-3a07acb2728f> in <module>()
----> 1 df_partitioned[df_partitioned.fake_categorical1=='A'].compute()
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/base.py in compute(self, **kwargs)
141 dask.base.compute
142 """
--> 143 (result,) = compute(self, traverse=False, **kwargs)
144 return result
145
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/base.py in compute(*args, **kwargs)
390 postcomputes = [a.__dask_postcompute__() if is_dask_collection(a)
391 else (None, a) for a in args]
--> 392 results = get(dsk, keys, **kwargs)
393 results_iter = iter(results)
394 return tuple(a if f is None else f(next(results_iter), *a)
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/threaded.py in get(dsk, result, cache, num_workers, **kwargs)
73 results = get_async(pool.apply_async, len(pool._pool), dsk, result,
74 cache=cache, get_id=_thread_get_id,
---> 75 pack_exception=pack_exception, **kwargs)
76
77 # Cleanup pools associated to dead threads
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/local.py in get_async(apply_async, num_workers, dsk, result, cache, get_id, rerun_exceptions_locally, pack_exception, raise_exception, callbacks, dumps, loads, **kwargs)
519 _execute_task(task, data) # Re-execute locally
520 else:
--> 521 raise_exception(exc, tb)
522 res, worker_id = loads(res_info)
523 state['cache'][key] = res
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/compatibility.py in reraise(exc, tb)
65 if exc.__traceback__ is not tb:
66 raise exc.with_traceback(tb)
---> 67 raise exc
68
69 else:
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/local.py in execute_task(key, task_info, dumps, loads, get_id, pack_exception)
288 try:
289 task, data = loads(task_info)
--> 290 result = _execute_task(task, data)
291 id = get_id()
292 result = dumps((result, id))
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/local.py in _execute_task(arg, cache, dsk)
269 func, args = arg[0], arg[1:]
270 args2 = [_execute_task(a, cache) for a in args]
--> 271 return func(*args2)
272 elif not ishashable(arg):
273 return arg
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/dataframe/io/parquet.py in _read_parquet_row_group(fs, fn, index, columns, rg, series, categories, schema, cs, dt, scheme, storage_name_mapping, *args)
364 for name in index]
365 df.columns = [storage_name_mapping.get(col, col)
--> 366 for col in columns
367 if col != index]
368
~/andre/yakushima-3env/lib/python3.5/site-packages/pandas/core/generic.py in __setattr__(self, name, value)
3625 try:
3626 object.__getattribute__(self, name)
-> 3627 return object.__setattr__(self, name, value)
3628 except AttributeError:
3629 pass
pandas/_libs/properties.pyx in pandas._libs.properties.AxisProperty.__set__()
~/andre/yakushima-3env/lib/python3.5/site-packages/pandas/core/generic.py in _set_axis(self, axis, labels)
557
558 def _set_axis(self, axis, labels):
--> 559 self._data.set_axis(axis, labels)
560 self._clear_item_cache()
561
~/andre/yakushima-3env/lib/python3.5/site-packages/pandas/core/internals.py in set_axis(self, axis, new_labels)
3072 raise ValueError('Length mismatch: Expected axis has %d elements, '
3073 'new values have %d elements' %
-> 3074 (old_len, new_len))
3075
3076 self.axes[axis] = new_labels
ValueError: Length mismatch: Expected axis has 2 elements, new values have 1 elements
I also noticed that I am not able to load one column (even without filtering):
df_partitioned[['fake_categorical1']].compute()---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-34-a68c843edc71> in <module>()
----> 1 df_partitioned[['fake_categorical1']].compute()
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/base.py in compute(self, **kwargs)
141 dask.base.compute
142 """
--> 143 (result,) = compute(self, traverse=False, **kwargs)
144 return result
145
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/base.py in compute(*args, **kwargs)
390 postcomputes = [a.__dask_postcompute__() if is_dask_collection(a)
391 else (None, a) for a in args]
--> 392 results = get(dsk, keys, **kwargs)
393 results_iter = iter(results)
394 return tuple(a if f is None else f(next(results_iter), *a)
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/threaded.py in get(dsk, result, cache, num_workers, **kwargs)
73 results = get_async(pool.apply_async, len(pool._pool), dsk, result,
74 cache=cache, get_id=_thread_get_id,
---> 75 pack_exception=pack_exception, **kwargs)
76
77 # Cleanup pools associated to dead threads
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/local.py in get_async(apply_async, num_workers, dsk, result, cache, get_id, rerun_exceptions_locally, pack_exception, raise_exception, callbacks, dumps, loads, **kwargs)
519 _execute_task(task, data) # Re-execute locally
520 else:
--> 521 raise_exception(exc, tb)
522 res, worker_id = loads(res_info)
523 state['cache'][key] = res
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/compatibility.py in reraise(exc, tb)
65 if exc.__traceback__ is not tb:
66 raise exc.with_traceback(tb)
---> 67 raise exc
68
69 else:
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/local.py in execute_task(key, task_info, dumps, loads, get_id, pack_exception)
288 try:
289 task, data = loads(task_info)
--> 290 result = _execute_task(task, data)
291 id = get_id()
292 result = dumps((result, id))
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/local.py in _execute_task(arg, cache, dsk)
269 func, args = arg[0], arg[1:]
270 args2 = [_execute_task(a, cache) for a in args]
--> 271 return func(*args2)
272 elif not ishashable(arg):
273 return arg
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/dataframe/io/parquet.py in _read_parquet_row_group(fs, fn, index, columns, rg, series, categories, schema, cs, dt, scheme, storage_name_mapping, *args)
364 for name in index]
365 df.columns = [storage_name_mapping.get(col, col)
--> 366 for col in columns
367 if col != index]
368
~/andre/yakushima-3env/lib/python3.5/site-packages/pandas/core/generic.py in __setattr__(self, name, value)
3625 try:
3626 object.__getattribute__(self, name)
-> 3627 return object.__setattr__(self, name, value)
3628 except AttributeError:
3629 pass
pandas/_libs/properties.pyx in pandas._libs.properties.AxisProperty.__set__()
~/andre/yakushima-3env/lib/python3.5/site-packages/pandas/core/generic.py in _set_axis(self, axis, labels)
557
558 def _set_axis(self, axis, labels):
--> 559 self._data.set_axis(axis, labels)
560 self._clear_item_cache()
561
~/andre/yakushima-3env/lib/python3.5/site-packages/pandas/core/internals.py in set_axis(self, axis, new_labels)
3072 raise ValueError('Length mismatch: Expected axis has %d elements, '
3073 'new values have %d elements' %
-> 3074 (old_len, new_len))
3075
3076 self.axes[axis] = new_labels
ValueError: Length mismatch: Expected axis has 2 elements, new values have 1 elements
I also noticed that if I choose both columns that were used to do the partitioning it works! But I'm still not able to filter them before computing :(
df_partitioned[['fake_categorical1', 'fake_categorical2']].compute().head()| fake_categorical1 | fake_categorical2 | |
|---|---|---|
| 0 | A | D |
| 1 | A | D |
| 2 | A | D |
| 3 | A | D |
| 4 | A | D |
Questions
- Is this a bug?
- If not, Am I loading and filtering the partitioned parquet correctly? How can I filter on a value of a specific column before mamking
compute()?
Thank you in advance!
Activity
erdnaavlis commentedon Feb 15, 2018
I found a way of how to do it. If I read the partitioned parquet as:
I am able to get a DataFrame that only contains
fake_categorical1=='A'. But still this is not very usable or practical. Because after if I want to run, for example, the following:I get the same error.
Is there a way to do it with
.locor other way similar to pandas?martindurant commentedon Feb 15, 2018
Thank you for a well-specified problem. This sounds like a bug, possibly in fastparquet.
Note that using
filters=is a different mechanism altogether: you do not read the partitions that don't have any rows matching your criteria at all; whereas filtering with[..]does consider every row of every partition.I suspect that the error is coming from the selection of one of the columns - there is a special optimization under the hood to only load those columns from the underlying data; but in this case, the special column does not correspond to any data, only directory names, so maybe nothing is being loaded.
One immediate workaround you could try:
which, if it works, will I think will prove what the problem is.
erdnaavlis commentedon Feb 15, 2018
@martindurant Thank you for your reply.
I understand that
filters=only causes the read of the relevant partitions. But it seems a bit surprising that filtering with[...]would always cause the read of all partitions.I think it would make sense that when filtering on the column which the parquet file was partitioned on, the lib (Dask or Fastparquet, not sure which one should have such abstraction) should be clever enough to know the DataFrame was coming from a partitioned parquet file and to only 'touch' the relevant partitions.
For example:
Should only read the partitions where
fake_categorical2 =='A'and then filter those partitions onvariable=='signal1'.Do you agree?
As for your workaround I'll only be able to test it tomorrow. But if am not mistaken, if it works, it will cause the load of all partitions, even if I am filtering on the column that was used to create the partitions, right?