【问题标题】:Cannot create 3rd lagged columns with dask-cudf无法使用 dask-cudf 创建第三个滞后列
【发布时间】:2022-07-02 08:06:17
【问题描述】:

我有以下dask_cudf.core.DataFrame:-

import pandas as pd
import numpy as np
import dask_cudf
import cudf


data = {"x":range(1,21), "nor":np.random.normal(2, 4, 20), "unif":np.random.uniform(size = 20)}
df = cudf.DataFrame(data)
ddf = dask_cudf.from_cudf(df, npartitions = 2)
ddf.compute()

我想为 norunif 列创建第 1 到第 5 个滞后值。但是,我通过以下方式创建它们:-

colz = ["nor", "unif"]
ddf[[s + "_" + str(1) for s in colz]] = ddf[colz].shift(1)
ddf[[s + "_" + str(2) for s in colz]] = ddf[colz].shift(2)

我可以创建第一个和第二个滞后值,但不能超过这个值。当我以大于 2 的值运行 shift 时,我收到以下错误::-

    /usr/local/lib/python3.7/site-packages/dask/dataframe/utils.py in raise_on_meta_error(funcname, udf)
    175     try:
--> 176         yield
    177     except Exception as e:

16 frames
cudf/_lib/copying.pyx in cudf._lib.copying.shift()

RuntimeError: parallel_for failed: cudaErrorInvalidConfiguration: invalid configuration argument

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.7/site-packages/dask/dataframe/utils.py in raise_on_meta_error(funcname, udf)
    195         )
    196         msg = msg.format(f" in `{funcname}`" if funcname else "", repr(e), tb)
--> 197         raise ValueError(msg) from e
    198
    199

ValueError: Metadata inference failed in `shift`.

Original error is below:
------------------------
RuntimeError('parallel_for failed: cudaErrorInvalidConfiguration: invalid configuration argument')

Traceback:
---------
  File "/usr/local/lib/python3.7/site-packages/dask/dataframe/utils.py", line 176, in raise_on_meta_error
    yield
  File "/usr/local/lib/python3.7/site-packages/dask/dataframe/core.py", line 5833, in _emulate
    return func(*_extract_meta(args, True), **_extract_meta(kwargs, True))
  File "/usr/local/lib/python3.7/site-packages/dask/utils.py", line 1021, in __call__
    return getattr(__obj, self.method)(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/cudf/core/frame.py", line 1788, in shift
    return self._shift(periods)
  File "/usr/local/lib/python3.7/site-packages/cudf/core/frame.py", line 1793, in _shift
    zip(self._column_names, data_columns), self._index
  File "/usr/local/lib/python3.7/site-packages/cudf/core/dataframe.py", line 818, in _from_data
    out = super()._from_data(data, index)
  File "/usr/local/lib/python3.7/site-packages/cudf/core/frame.py", line 140, in _from_data
    Frame.__init__(obj, data, index)
  File "/usr/local/lib/python3.7/site-packages/cudf/core/frame.py", line 78, in __init__
    self._data = cudf.core.column_accessor.ColumnAccessor(data)
  File "/usr/local/lib/python3.7/site-packages/cudf/core/column_accessor.py", line 121, in __init__
    data = dict(data)
  File "/usr/local/lib/python3.7/site-packages/cudf/core/frame.py", line 1791, in <genexpr>
    data_columns = (col.shift(offset, fill_value) for col in self._columns)
  File "/usr/local/lib/python3.7/site-packages/cudf/core/column/column.py", line 391, in shift
    return libcudf.copying.shift(self, offset, fill_value)
  File "cudf/_lib/copying.pyx", line 633, in cudf._lib.copying.shift

我似乎无法理解为什么会这样。

【问题讨论】:

标签: python dask rapids cudf


【解决方案1】:

感谢您的最小复制;只需稍作改动即可正常工作。不要.compute() dask 太早。如果您需要做某事并继续在 dask/dask_cudf 中处理,请使用.persist()

import pandas as pd
import numpy as np
import dask_cudf
import cudf


data = {"x":range(1,21), "nor":np.random.normal(2, 4, 20), "unif":np.random.uniform(size = 20)}
df = cudf.DataFrame(data)
ddf = dask_cudf.from_cudf(df, npartitions = 2)
colz = ["nor", "unif"]
ddf[[s + "_" + str(1) for s in colz]] = ddf[colz].shift(1)
ddf[[s + "_" + str(2) for s in colz]] = ddf[colz].shift(2)
ddf[[s + "_" + str(3) for s in colz]] = ddf[colz].shift(3)
ddf.compute()

输出

    x   nor unif    nor_1   unif_1  nor_2   unif_2  nor_3   unif_3
0   1   -0.548635   0.758489    <NA>    <NA>    <NA>    <NA>    <NA>    <NA>
1   2   1.897041    0.512556    -0.548635482    0.758489432 <NA>    <NA>    <NA>    <NA>
2   3   1.723991    0.911938    1.897040905 0.512555874 -0.548635482    0.758489432 <NA>    <NA>
3   4   -2.646267   0.663379    1.72399069  0.911937643 1.897040905 0.512555874 -0.548635482    0.758489432
4   5   -0.061475   0.816525    -2.646267389    0.663378839 1.72399069  0.911937643 1.897040905 0.512555874
5   6   -6.134814   0.668500    -0.061475431    0.816524729 -2.646267389    0.663378839 1.72399069  0.911937643
6   7   7.111698    0.267380    -6.134814101    0.668499979 -0.061475431    0.816524729 -2.646267389    0.663378839
7   8   0.539380    0.525843    7.111698317 0.267379669 -6.134814101    0.668499979 -0.061475431    0.816524729
8   9   -2.891848   0.702126    0.539380293 0.525842526 7.111698317 0.267379669 -6.134814101    0.668499979
9   10  3.916832    0.845603    -2.891847536    0.702126359 0.539380293 0.525842526 7.111698317 0.267379669
10  11  3.495712    0.598698    3.916832341 0.84560299  -2.891847536    0.702126359 0.539380293 0.525842526
11  12  9.499137    0.943415    3.495712197 0.598698299 3.916832341 0.84560299  -2.891847536    0.702126359
12  13  0.045835    0.493098    9.499136506 0.943415216 3.495712197 0.598698299 3.916832341 0.84560299
13  14  9.384824    0.165153    0.045834927 0.493097786 9.499136506 0.943415216 3.495712197 0.598698299
14  15  5.031779    0.269262    9.384823504 0.165153326 0.045834927 0.493097786 9.499136506 0.943415216
15  16  -0.898455   0.791966    5.031778877 0.269261569 9.384823504 0.165153326 0.045834927 0.493097786
16  17  -3.615537   0.973498    -0.898454611    0.791966135 5.031778877 0.269261569 9.384823504 0.165153326
17  18  -3.004841   0.790925    -3.615536805    0.973497929 -0.898454611    0.791966135 5.031778877 0.269261569
18  19  8.266728    0.241130    -3.004840794    0.790924685 -3.615536805    0.973497929 -0.898454611    0.791966135
19  20  6.881456    0.841148    8.26672818  0.241129615 -3.004840794    0.790924685 -3.615536805    0.973497929

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多