我想调用一个任意的 Dask 系列,如果它包含不可散列的类型.value_counts()
,我想将该系列转换为类型字符串。如果不需要,我不想将系列转换为字符串。我也不想在打电话.compute()
之前打电话.value_counts()
。我努力了
df = pd.DataFrame({"a":[[1], ["foo"], ["foo", "bar"]]})
df = dd.from_pandas(df, npartitions=1)
srs = df["a"]
try:
val_counts = srs.value_counts()
except TypeError:
srs = srs.astype(str)
val_counts = srs.value_counts()
val_counts.compute()
这给出了错误
类型错误:不可散列类型:“列表”
和
df = pd.DataFrame({"a":[[1], ["foo"], ["foo", "bar"]]})
df = dd.from_pandas(df, npartitions=1)
srs = df["a"]
def func(srs):
try:
val_counts = srs.value_counts()
except TypeError:
srs = srs.astype(str)
val_counts = srs.value_counts()
return val_counts
val_counts = dask.compute(func(srs))
这给出了同样的错误。
我也试过
df = pd.DataFrame({"a":[[1], ["foo"], ["foo", "bar"]]})
df = dd.from_pandas(df, npartitions=1)
srs = df["a"]
if srs.apply(lambda y: isinstance(y, list), meta=srs).any():
srs = srs.astype(str)
srs.value_counts().compute()
这给出了错误
TypeError: 试图将 dd.Scalar<series-..., type=str> 转换为布尔值。