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

DataFrame调用pct_change()报错:str与str不支持/运算,求原因

Fixing "unsupported operand type(s) for /: 'str' and 'str'" in pandas pct_change()

Hey there, let's get to the bottom of why your DataFrame cells are being treated as strings instead of numbers—and how to fix this properly so you can use pct_change(), mean(), and other pandas functions directly like your instructor did.

Why are your values stored as strings?

There are a few common culprits here:

  • Import-time formatting issues: If your source data (like a CSV) has values with non-numeric characters—think currency symbols (€, $), thousand separators (,), or non-standard missing value markers (like "NA" instead of NaN)—pandas will default to reading those columns as object (string) type instead of numeric.
  • Uncleaned scraped data: If you pulled this data from a website, there might be hidden text, comments, or special characters mixed in with the numbers that throw off pandas' type detection.
  • Accidental manual edits: If you tweaked any cell values manually (even just one!), pandas might convert the entire column to object type to accommodate the string entry.

A better fix than your dictionary workaround

Instead of manually handling each column via a dictionary, let's convert your numeric columns to the right type in bulk:

  1. First, confirm the problem
    Run this to check which columns are stored as strings (object type):

    print(frame.dtypes)
    

    You'll likely see PG and BEI.DE listed as object instead of float64 or int64.

  2. Convert columns to numeric types
    Use pandas' built-in pd.to_numeric() function to convert the problematic columns. The errors='coerce' argument will turn any unconvertible values (like stray text) into NaN, which you can handle next:

    # Convert all columns at once
    frame = frame.apply(pd.to_numeric, errors='coerce')
    
    # Or convert specific columns if others are non-numeric
    # frame[['PG', 'BEI.DE']] = frame[['PG', 'BEI.DE']].apply(pd.to_numeric, errors='coerce')
    
  3. Handle missing values
    Now that you might have NaN values from unconvertible entries, decide how to clean them based on your data:

    • Drop rows with missing values:
      frame = frame.dropna()
      
    • Fill missing values with the previous row's value (forward fill):
      frame = frame.fillna(method='ffill')
      
  4. Test the fix
    Now you should be able to run the exact code from your tutorial without errors:

    returns = frame.pct_change()
    # You can also run other functions directly now
    mean_returns = returns.mean()
    cov_matrix = returns.cov()
    

This approach is way more scalable than your dictionary workaround—no need to repeat code for every column, and it keeps your workflow aligned with standard pandas practices like your instructor demonstrated.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:22:25