Repository navigation
Description
If I am reading a partitioned parquet file from wich I just want to load (or work with) a few partitions, and I don't know the possible values of the partitions, I think there is not a way of finding out the partitions possible values from Dask.DataFrame.
What I currently do in my workflow is:
- read
ParquetFilewithfastparquet - get the list of values for each partitons using
.cats. - And read again the file with
Daskfiltering on the partitions that I wish.
This is not very clean... As far as I understand, it makes sense for Dask to provide a similar method under a DataFrame that was created from a partitioned parquet file.
I'm including full code for reproducibility and clarity. Please jump to section Read partitioned parquet, if you wish.
Versions
- numpy==1.14.0
- pandas==0.22.0
- dask==0.17.0
- fastparquet==0.1.4
Imports and Initialization
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.head().dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| dates | fake_categorical1 | fake_categorical2 | variable | value | |
|---|---|---|---|---|---|
| 0 | 1980-01-01 00:00:00 | C | F | signal1 | 50.363210 |
| 1 | 1980-01-01 00:01:00 | B | E | signal1 | 50.723882 |
| 2 | 1980-01-01 00:02:00 | C | D | signal1 | 50.170057 |
| 3 | 1980-01-01 00:03:00 | A | F | signal1 | 50.387092 |
| 4 | 1980-01-01 00:04:00 | A | F | signal1 | 50.450416 |
Write partitioned parquet
fastparquet.write('file_partitioned', small_df, compression='snappy',
file_scheme='hive', write_index=False,
partition_on=['fake_categorical1','fake_categorical2'])Read partitioned parquet
# Read partitioned parquet
fasparquet: I can get the list of partitions values before loading
Read it with Fastparquet:
df_fp_file = fastparquet.ParquetFile('file_partitioned')List partitions possible values:
df_fp_file.catsOrderedDict([('fake_categorical1', ['B', 'A', 'C']),
('fake_categorical2', ['F', 'E', 'D'])])
With this info I could do ParquetFile.to_pandas( filters=[('fake_categorical1', '==', 'A') ..... ]) by selecting the partitions of interest without loading all of them, increasing performance
Dask: afaik I cannot get the list of partitions values before loading :(
Read it with dask:
df_dd_file = dd.read_parquet('file_partitioned').
**Can't list partitions in advance :( **
.
df_dd_file.cats---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-21-6974741a7af4> in <module>()
----> 1 df_dd_file.cats
~/andre/yakushima-3env/lib/python3.5/site-packages/dask/dataframe/core.py in __getattr__(self, key)
2349 return new_dd_object(merge(self.dask, dsk), name,
2350 meta, self.divisions)
-> 2351 raise AttributeError("'DataFrame' object has no attribute %r" % key)
2352
2353 def __dir__(self):
AttributeError: 'DataFrame' object has no attribute 'cats'
Questions
- Is there such functionality under a different method and I am missing it?
- If not, can it be implemented?
Thanks!
Activity
martindurant commentedon Feb 21, 2018
No, this does not seem to exist, but I think it should: the type of a column made from partitioning should be known categories with the values filled in from the directories.
TomAugspurger commentedon Aug 13, 2019
I think this was closed by #3216?