我想将日期时间字符串转换为 dask cudf 中的时间戳,然后按此列对数据帧进行排序。
示例:
import dask_cudf as ddf
import pandas as pd
# Sample data (replace with your actual data)
cdf = cudf.DataFrame({
'city': ['Dallas', 'Bogota', 'Chicago', 'Juarez'],
'timestamp': ['2019-12-29 14:15:08 UTC', '2019-12-30 10:30:15 UTC', '2019-12-31 18:45:30 UTC', '2020-01-01 03:20:45 UTC']
})
# Create a Dask-cuDF DataFrame
dask_df = ddf.from_cudf(cdf, npartitions=2)
def to_timestamp(x):
import time
import datetime
element = datetime.datetime.strptime(x,"%Y-%m-%d %H:%M:%S UTC")
return datetime.datetime.timestamp(element)
dask_df['timestamp'] = dask_df['timestamp'].map_partitions(to_timestamp, meta=("timestamp", "str"))
dask_df.head()
我遇到错误:
TypeError:strptime() 参数 1 必须是 str,而不是 Series
如何对 dask cudf 上的大型数据框执行此操作?
这个 map_partitions 线程 似乎涵盖了逐行使用
map_partitions
的所有技巧。
此外,您可以稍微重构您的功能。导入语句可以移到函数之外以节省加载时间。您仅在函数中使用
datetime
,因此您可以跳过导入 time
。该函数可能如下所示:
def to_timestamp(x):
datetime_object = datetime.datetime.strptime(x,"%Y-%m-%d %H:%M:%S UTC")
timestamp = datetime.datetime.timestamp(element)
return timestamp