如何修复 pandas AttributeError: module pandas has no attribute timedelta

问题:

运行 Python 代码或 Jupyter notebook 时,你看到类似这样的错误消息

pandas_timedelta_error.txt
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_99995/3617538487.py in <module>
      1 for date in df.index:
----> 2     print(date - pd.timedelta(5, 'years'))

/usr/local/lib/python3.9/dist-packages/pandas/__init__.py in __getattr__(name)
    259         return _SparseArray
    260
--> 261     raise AttributeError(f"module 'pandas' has no attribute '{name}'")
    262
    263

AttributeError: module 'pandas' has no attribute 'timedelta'

解决方案

pandas Timedelta 拼写为大写 TTimedelta 而不是 timedelta


Check out similar posts by category: Pandas, Python