Is there a way to easily convert a DataFrame of numeric values into an Array? Similar to values
with a pandas DataFrame. I can't seem to find any way to do this with the provided API, but I'd assume it's a common operation.
That's where Dask arrays provide much more flexibility than Numpy. They enable you to work with larger-than-memory objects, and computation time is significantly faster due to parallelization.
set_index syntaxCreate a pandas DataFrame with two columns of data, and a 2-partition Dask DataFrame from it. Print the DataFrame and see that it has one index column that was created by default by pandas and two columns with data. Take a look at the divisions of ddf. ddf has two divisions.
Dask runs faster than pandas for this query, even when the most inefficient column type is used, because it parallelizes the computations. pandas only uses 1 CPU core to run the query. My computer has 4 cores and Dask uses all the cores to run the computation.
You can use the .values
property
x = df.values
At the moment there is no trivial way to do this. This is because dask.array needs to know the length of all of its chunks and dask.dataframe doesn't know this length. This can not be a completely lazy operation.
That being said, you can accomplish it using dask.delayed as follows:
import dask.array as da
from dask import compute
def to_dask_array(df):
partitions = df.to_delayed()
shapes = [part.values.shape for part in partitions]
dtype = partitions[0].dtype
results = compute(dtype, *shapes) # trigger computation to find shape
dtype, shapes = results[0], results[1:]
chunks = [da.from_delayed(part.values, shape, dtype)
for part, shape in zip(partitions, shapes)]
return da.concatenate(chunks, axis=0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With