小哲不吃辣 2022-06-27 18:45 采纳率: 100%
浏览 63
已结题

处理豆瓣电影数据 转换上映日期的时间格式时报错 错误提示ValueError: offset must be a timedelta strictly between

处理豆瓣电影数据 转换上映日期的时间格式时报错 错误提示ValueError: offset must be a timedelta strictly between -timedelta(hours=24) and timedelta(hours=24).

import pandas as pd
df=pd.read_excel("../621/data/豆瓣电影数据.xlsx")
df['上映时间']=pd.to_datetime(df['上映时间'],errors='coerce')
```python


img





```bash
#####错误显示
ValueError                                Traceback (most recent call last)
Input In [16], in <cell line: 3>()
      1 #object ->datetime
----> 3 df['上映时间']=pd.to_datetime(df['上映时间'],errors='coerce')

File D:\lenovo.bigData\pythonProject\venv\lib\site-packages\pandas\core\tools\datetimes.py:1047, in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
   1045             result = arg.tz_localize(tz)
   1046 elif isinstance(arg, ABCSeries):
-> 1047     cache_array = _maybe_cache(arg, format, cache, convert_listlike)
   1048     if not cache_array.empty:
   1049         result = arg.map(cache_array)

File D:\lenovo.bigData\pythonProject\venv\lib\site-packages\pandas\core\tools\datetimes.py:192, in _maybe_cache(arg, format, cache, convert_listlike)
    188 cache_array = Series(dtype=object)
    190 if cache:
    191     # Perform a quicker unique check
--> 192     if not should_cache(arg):
    193         return cache_array
    195     unique_dates = unique(arg)

File D:\lenovo.bigData\pythonProject\venv\lib\site-packages\pandas\core\tools\datetimes.py:154, in should_cache(arg, unique_share, check_count)
    150 assert 0 < unique_share < 1, "unique_share must be in next bounds: (0; 1)"
    152 try:
    153     # We can't cache if the items are not hashable.
--> 154     unique_elements = set(islice(arg, check_count))
    155 except TypeError:
    156     return False

ValueError: offset must be a timedelta strictly between -timedelta(hours=24) and timedelta(hours=24).



  • 写回答

1条回答 默认 最新

  • Hann Yang 优质创作者: 编程框架技术领域 2022-06-27 19:12
    关注

    df['上映时间']读出的时间格式没法被pd.to_datetime()转化,不符合它的格式要求
    列一些时间串出来,帮你看看怎么修改才能被转

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 7月6日
  • 已采纳回答 6月28日
  • 创建了问题 6月27日