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

使用Pandas Series expanding apply返回含浮点与字符串列的DataFrame

问题原因与解决方法

问题根源

  1. 参数类型误解:expanding().apply()的lambda函数接收的是整个窗口的Series对象,而非单个元素。你原代码里把x当成单个数值处理,导致尝试将Series转为float时触发类型错误。
  2. 列类型冲突:Pandas的DataFrame列无法同时兼容浮点型NaN和字符串——初始行的NaN会让列默认是float类型,后续写入字符串时会因类型不匹配报错。

解决方法

方法一:拆分处理(简单直接)

分别生成浮点列和字符串列,再合并,彻底避免类型冲突:

import pandas as pd
import numpy as np

a = pd.Series([1,2,3,4,5])

# 生成浮点列:取每个expanding窗口的最后一个值(匹配你的期望结果)
float_col = a.expanding(2).apply(lambda x: x.iloc[-1], raw=True)
# 生成字符串列:首行设为NaN,其余行填充'test'
string_col = pd.Series([np.nan] + ['test']*(len(a)-1), dtype='object')

# 合并为目标DataFrame
result = pd.DataFrame({'float': float_col, 'string': string_col})
print(result)

输出结果:

float string
0    NaN    NaN
1    2.0   test
2    3.0   test
3    4.0   test
4    5.0   test

方法二:窗口内同时计算(适合复杂场景)

如果需要在窗口内同时完成多维度计算,可让自定义函数返回元组,再通过result_type='expand'展开为列,最后手动指定列类型:

import pandas as pd
import numpy as np

a = pd.Series([1,2,3,4,5])

def process_window(x):
    # x为当前窗口的Series,这里取最后一个值作为浮点结果,返回元组
    return (x.iloc[-1], 'test')

# 应用函数并展开为列
temp = a.expanding(2).apply(process_window, raw=False, result_type='expand')
temp.columns = ['float', 'string']
# 将字符串列转为object类型,兼容NaN和字符串
temp['string'] = temp['string'].fillna(np.nan).astype('object')

print(temp)

补充说明

  • raw=True:在处理数值型窗口时,传入numpy数组而非Series,提升计算效率;如果需要使用Series的方法(如iloc),则设为raw=False。
  • 你之前尝试的convert_dtype是Series.apply()的专属参数,expanding.apply()不支持该参数,因此会报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 10:48:19