【问题标题】:pandas pct_change show unsupported operand type str and strpandas pct_change 显示不受支持的操作数类型 str 和 str
【发布时间】:2015-04-17 07:51:10
【问题描述】:
print type (analysis['SMA_22'])
<class 'pandas.core.series.Series'>


analysis['SMA_22'].fillna('0').pct_change()

TypeError: unsupported operand type(s) for /: 'str' and 'str'

analysis.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 619 entries, 2012-12-03 00:00:00 to 2015-04-16 00:00:00
Data columns (total 3 columns):
SMA_22    598 non-null float64
dtypes: float64(2), object(1)

有人知道为什么吗?

Edit1:按要求显示 .info()

【问题讨论】:

  • “SMA_22”的数据类型是什么?你能显示来自analysis.info()的输出吗?
  • analysis.info() DatetimeIndex:619 个条目,2012-12-03 00:00:00 到 2015-04-16 00:00: 00 数据列(共 3 列):SMA_22 598 非空 float64 dtypes:float64(2), object(1)
  • 请在您的问题中编辑附加信息,谢谢
  • 错误很明显,它认为该列的 dtype 是字符串,您可以尝试将其转换为浮点数然后再试一次:analysis[SMA_22'] = analysis[SMA_22'].astype(np.float64)
  • SMA_22 598 non-null float64, analysis['SMA_22'].fillna('0').pct_change() 仍然失败并出现同样的错误。

标签: python python-2.7 pandas


【解决方案1】:

我看到了你的问题:

analysis['SMA_22'].fillna('0').pct_change()

这会将NaN 替换为字符值'0' 我想你想要这个:

analysis['SMA_22'].fillna(0).pct_change()

NaN 替换为整数/浮点值0

【讨论】:

  • 分析['SMA_22'].fillna(0).pct_change(): InvalidOperation: 涉及NaN的比较
  • 您必须发布完整的原始数据,因为我刚刚尝试填充 0 和 NaN 值,但它对我有用
  • 还有你的 python、numpy 和 pandas 版本是什么?我正在运行 python 3.4 64 位、numpy 1.9.1 和 pandas 0.16.0
  • 对于原始数据,我应该使用 analysis.values() 来显示数据吗?这是你想要的吗?
  • 不,我想要原始的 csv、txt 文件和你用来加载数据的代码
猜你喜欢
  • 2019-01-03
  • 2019-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-20
相关资源
最近更新 更多