Repository navigation
Closed
Description
I have the following program that currently works using pandas/numpy:
import pandas as pd
import numpy as np
import dask.dataframe as dd
import dask.array as da
from io import StringIO
test_data = '''id,transaction_dt,units,measures
1,2018-01-01,4,30.5
1,2018-01-03,4,26.3
2,2018-01-01,3,12.7
2,2018-01-03,3,8.8'''
df_test = pd.read_csv(StringIO(test_data), sep=',')
df_test['transaction_dt'] = pd.to_datetime(df_test['transaction_dt'])
df_test = df_test.loc[np.repeat(df_test.index, df_test['units'])]
df_test['transaction_dt'] += pd.to_timedelta(df_test.groupby(level=0).cumcount(), unit='d')
df_test = df_test.reset_index(drop=True)
Expected results:
id,transaction_dt,measures
1,2018-01-01,30.5
1,2018-01-02,30.5
1,2018-01-03,30.5
1,2018-01-04,30.5
1,2018-01-03,26.3
1,2018-01-04,26.3
1,2018-01-05,26.3
1,2018-01-06,26.3
2,2018-01-01,12.7
2,2018-01-02,12.7
2,2018-01-03,12.7
2,2018-01-03,8.8
2,2018-01-04,8.8
2,2018-01-05,8.8
I have rewritten this for dask dataframe/array:
dd_test = dd.from_pandas(df_test, npartitions=3)
dd_test['helper'] = 1
dd_test = dd_test.loc[da.repeat(dd_test.index, dd_test['units'])]
dd_test['transaction_dt'] = dd_test['transaction_dt'] + (dd.test.groupby('id')['helper'].cumsum()).astype('timedelta64[D]')
dd_test = dd_test.reset_index(drop=True)
However, I am encountering the following error:
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-5-87e1c9adedb3> in <module>()
2 dd_test['helper'] = 1
3
----> 4 dd_test = dd_test.loc[da.repeat(dd_test.index, dd_test['units'])]
5 dd_test['transaction_dt'] = dd_test['transaction_dt'] + (dd.test.groupby('id')['helper'].cumsum()).astype('timedelta64[D]')
6 dd_test = dd_test.reset_index(drop=True)
/Users/jr/anaconda/lib/python2.7/site-packages/dask/array/creation.pyc in repeat(a, repeats, axis)
632
633 if not isinstance(repeats, Integral):
--> 634 raise NotImplementedError("Only integer valued repeats supported")
635
636 if -a.ndim <= axis < 0:
NotImplementedError: Only integer valued repeats supported
I have also converted the index to int column or array and still encounter the error, so there must be a deeper level to my misunderstanding of the behavior. Any advice most welcome.
Activity
jakirkham commentedon Feb 19, 2018
It means that
dd_test['units'](from line 3 in you second code snippet) is not anint. Looking atdd_test['units']suggests it is some sort of Dask Series object. As I don't really spend time with Dask Dataframes, I can't really advise you further other than to say thatda.repeatrequires a single integer for the number of repeats.smrtslckr commentedon Feb 19, 2018
That is interesting and good to know. I was under the impression from the documentation that da.repeat would also accept an array of ints from the api docs:
http://dask.pydata.org/en/latest/array-api.html#dask.array.repeat
So it would not be possible to access the underlying dask array with dd_test[‘units’].values ? This does not seem to work.
smrtslckr commentedon Feb 22, 2018
@jakirkham Thanks for openingthe issues for my other inquiry on the conditional array. I did just want to see if you had any other comments on the array integer repeat? Specifically, the int v. array of int.
jakirkham commentedon Feb 22, 2018
The fact that
repeatdoesn't support arrays of integers is a known issue ( #2946 ). Nothing more to add here that isn't already there. Is it ok if we close this and just track in the other issue?smrtslckr commentedon Feb 22, 2018
Thanks for your prompt feedback that was what I needed to confirm. Absolutely, feel free to close these two issues.
jakirkham commentedon Feb 22, 2018
Thanks @Erstwild.
Closing as duplicate of issue ( #2946 ).