我有一个从 Excel 读取的 DataFrame,其中包含 DateTime 类型的列之一。
sales_data=pandas.read_excel(r'Sample Sales Data.xlsx')
我能够使用 str.extract/lambda 函数从其他列中提取子字符串。但我无法处理“订单日期”列
命令sales_data['Order Date']
给出以下输出
正如推荐的other堆栈溢出问题,我尝试过
sales_data['Order Date'].apply(lambda x:x.str.slice())
我收到错误: AttributeError: 'datetime.datetime' 对象没有属性 'str'
为了检查订单日期列的类型,我尝试了
sales_data['Order Date'].apply(lambda x:type(x))
I got the type datetime.datetime
But when I tried the datetime operation
sales_data['Order Date'].apply(lambda x:x.strftime("m"))
我收到错误: AttributeError: 'int' 对象没有属性 'strftime'
我收到了类似的命令错误
sales_data['Order Date'].apply(lambda x:x.dt.month)
请建议一种方法,将日期时间对象中的月份提取到另一列中,而无需迭代数据帧。我无法对此列使用 datetime 或 int 函数,因为它同时充当 datetime 和 int 列。