You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Arrow解析Pandas DataFrame列中的日期数据?

使用Arrow解析Pandas DataFrame的日期列

arrow.get() 仅支持单个字符串/日期输入,无法直接处理Pandas Series对象,因此需要对列中的每个元素单独调用解析方法,以下是两种可行方案:

方案1:使用Series.apply()逐元素处理

通过apply()将arrow.get()应用到列的每个元素上,生成包含Arrow日期对象的新列:

import pandas as pd
import arrow

data=['2015', '2016','2017', '2108']
df= pd.DataFrame(data,columns=['time'])

# 解析为Arrow日期对象
df['arrow_time'] = df['time'].apply(arrow.get)

方案2:列表推导式(大数据量下更高效)

直接遍历列元素并批量解析,性能比apply()更优:

df['arrow_time'] = [arrow.get(date_str) for date_str in df['time']]

额外说明

如果需要将Arrow对象转换为Pandas原生的datetime类型以兼容Pandas日期操作,可以在解析时提取.datetime属性:

df['pandas_datetime'] = df['time'].apply(lambda x: arrow.get(x).datetime)

内容的提问来源于stack exchange,提问作者user17743486

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 06:30:51