将转换后的日期时间 str 分配回 dask df

发布于 2025-01-10 04:12:12 字数 995 浏览 1 评论 0原文

我将 dask 数据框字符串日期列转换为 pandas 日期时间,并创建了一个日期时间索引。 行将其分配回源 dask 数据

当我尝试使用ddf.assign(date=date_parsed)

帧时,我收到一个ValueError: Length of Values (1000000) does not match length of index ( 2)

我最初认为 create datetimeindex 具有正确的长度,但源只有 2 个 indeces。我尝试将 datetimeindex 转换为 pd.dataframe,它成功转换,但我无法将 pd.df 添加到 daskdf 中。我也尝试将其转换回系列,但仍然无法附加/分配。

我想做的是将 datetimeindex 分配回源 dask df。

从 pd 转换而来的 dask df 示例。所有值都是字符串数据类型。

df=pd.DataFrame({'fname': ['dwayne','peter','dead','wonder'], 
                 'lname': ['rock','pan','pool','boy'], 
                 'entrydate':['31DEC2021', '22JAN2022', NaN, '15DEC2025']})

ddf = dd.from_pandas(df) 

我做了什么:(1)解析输入日期值并转换为日期时间。它给了我以下内容:

DatetimeIndex(['2021-12-31', '2022-01-22', 'NaT', '2025-12-15'], dtype='datetime64[ns]', length=4, freq=None)

(2)我使用 drop 函数删除了“entrydate”列。 (3) 当我尝试分配函数时,我收到 ValueError...

I got my dask dataframe string date column converted to a pandas datetime and it created a datetimeindex. When I try assigning it back to the source dask dataframe using

ddf.assign(date=date_parsed) line, I get a

ValueError: Length of values (1000000) does not match length of index (2).

I initially thought the create datetimeindex have the correct length but the source have only 2 indeces. I tried converting the datetimeindex into a pd.dataframe, which successfully converted but I cannot add that pd.df into the daskdf. I also tried converting it back to a series, but still not able to append/assign.

What I would like to do is to assign the datetimeindex back to the source dask df.

sample dask df converted from pd. all values are string datatype.

df=pd.DataFrame({'fname': ['dwayne','peter','dead','wonder'], 
                 'lname': ['rock','pan','pool','boy'], 
                 'entrydate':['31DEC2021', '22JAN2022', NaN, '15DEC2025']})

ddf = dd.from_pandas(df) 

what I did: (1) parsed the entrydate values and converted to datetime. it gave me the following:

DatetimeIndex(['2021-12-31', '2022-01-22', 'NaT', '2025-12-15'], dtype='datetime64[ns]', length=4, freq=None)

(2) I dropped the 'entrydate' column using the drop function.
(3) When I tried the assign function, I get the ValueError...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烙印 2025-01-17 04:12:12

无需使用分配创建新列。 Dask dataframe 支持 pandas API,因此以下内容有效:

import dask.dataframe as dd
import pandas as pd

df=pd.DataFrame({'fname': ['dwayne','peter','dead','wonder'], 
                 'lname': ['rock','pan','pool','boy'], 
                 'entrydate':['31DEC2021', '22JAN2022', NaN, '15DEC2025']})

ddf = dd.from_pandas(df, npartitions=2)

# roughly same as ddf.assign(date=date_parsed)
ddf["date"] = dd.to_datetime(ddf["entrydate"])

另请参阅此答案< /a>.

There is no need to create new column using assign. Dask dataframe supports pandas API, so the following works:

import dask.dataframe as dd
import pandas as pd

df=pd.DataFrame({'fname': ['dwayne','peter','dead','wonder'], 
                 'lname': ['rock','pan','pool','boy'], 
                 'entrydate':['31DEC2021', '22JAN2022', NaN, '15DEC2025']})

ddf = dd.from_pandas(df, npartitions=2)

# roughly same as ddf.assign(date=date_parsed)
ddf["date"] = dd.to_datetime(ddf["entrydate"])

See also this answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文