강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của gusrud4168129
gusrud4168129

câu hỏi đã được viết

Dự án thực hành phân tích trực quan hóa dữ liệu Python

Xử lý dữ liệu Marathon 2

to_timedelta 에러에 관련해서 질문드립니다.

Viết

·

501

0

안녕하세요 강의 잘 듣고 있습니다. 

마라톤 데이터 가공 2 강의를 듣던 중 Step 5단계에서 오류가 발생합니다. 사용하고 있는 파이썬 버전은 3.8.12이고 pandas 버전은 1.3.4입니다. 

 

강의 내용대로 순차적으로 실습 진행 중 Step 5단계에서 오류가 발생하여 Step 5단계의 다른 내용을 모두 주석 후에 

 

marathon_2015_2017['5K'] = pd.to_timedelta(marathon_2015_2017['5K'])

 

이 코드만 실행해 보았는데요 그럼에도 오류가 발생합니다. 혹시나 해서 csv파일을 열어 보았는데 시간데이터 중 '-' 로 표시된 부분이 종종 있었습니다. 제 생각에는 '-'라는 문자가 있으니 null도 아니고 시간데이터도 아니다 보니 문제가 발생하는 것 같은 예감입니다. 

 

아래에 오류 내용을 그대로 첨부하겠습니다.  도움 주시면 감사하겠습니다.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/opt/homebrew/Caskroom/miniforge/base/envs/DV/lib/python3.8/site-packages/pandas/_libs/tslibs/timedeltas.pyx in pandas._libs.tslibs.timedeltas.array_to_timedelta64()

/opt/homebrew/Caskroom/miniforge/base/envs/DV/lib/python3.8/site-packages/pandas/_libs/tslibs/timedeltas.pyx in pandas._libs.tslibs.timedeltas.parse_timedelta_string()

ValueError: symbols w/o a number

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
/var/folders/zl/7hy5k6z55zz2b286tv4bbk740000gn/T/ipykernel_48790/1493613945.py in <module>
      6 
      7 # Convert using pandas to_timedelta method
----> 8 marathon_2015_2017['5K'] = pd.to_timedelta(marathon_2015_2017['5K'])
      9 # marathon_2015_2017['10K'] = pd.to_timedelta(marathon_2015_2017['10K'])
     10 # marathon_2015_2017['15K'] = pd.to_timedelta(marathon_2015_2017['15K'])

/opt/homebrew/Caskroom/miniforge/base/envs/DV/lib/python3.8/site-packages/pandas/core/tools/timedeltas.py in to_timedelta(arg, unit, errors)
    122         return arg
    123     elif isinstance(arg, ABCSeries):
--> 124         values = _convert_listlike(arg._values, unit=unit, errors=errors)
    125         return arg._constructor(values, index=arg.index, name=arg.name)
    126     elif isinstance(arg, ABCIndex):

/opt/homebrew/Caskroom/miniforge/base/envs/DV/lib/python3.8/site-packages/pandas/core/tools/timedeltas.py in _convert_listlike(arg, unit, errors, name)
    171 
    172     try:
--> 173         td64arr = sequence_to_td64ns(arg, unit=unit, errors=errors, copy=False)[0]
    174     except ValueError:
    175         if errors == "ignore":

/opt/homebrew/Caskroom/miniforge/base/envs/DV/lib/python3.8/site-packages/pandas/core/arrays/timedeltas.py in sequence_to_td64ns(data, copy, unit, errors)
    989     if is_object_dtype(data.dtype) or is_string_dtype(data.dtype):
    990         # no need to make a copy, need to convert if string-dtyped
--> 991         data = objects_to_td64ns(data, unit=unit, errors=errors)
    992         copy = False
    993 

/opt/homebrew/Caskroom/miniforge/base/envs/DV/lib/python3.8/site-packages/pandas/core/arrays/timedeltas.py in objects_to_td64ns(data, unit, errors)
   1098     values = np.array(data, dtype=np.object_, copy=False)
   1099 
-> 1100     result = array_to_timedelta64(values, unit=unit, errors=errors)
   1101     return result.view("timedelta64[ns]")
   1102 

/opt/homebrew/Caskroom/miniforge/base/envs/DV/lib/python3.8/site-packages/pandas/_libs/tslibs/timedeltas.pyx in pandas._libs.tslibs.timedeltas.array_to_timedelta64()

/opt/homebrew/Caskroom/miniforge/base/envs/DV/lib/python3.8/site-packages/pandas/_libs/tslibs/timedeltas.pyx in pandas._libs.tslibs.timedeltas.array_to_timedelta64()

/opt/homebrew/Caskroom/miniforge/base/envs/DV/lib/python3.8/site-packages/pandas/_libs/tslibs/timedeltas.pyx in pandas._libs.tslibs.timedeltas.convert_to_timedelta64()

/opt/homebrew/Caskroom/miniforge/base/envs/DV/lib/python3.8/site-packages/pandas/_libs/tslibs/timedeltas.pyx in pandas._libs.tslibs.timedeltas.parse_timedelta_string()

ValueError: symbols w/o a number
to_timedeltapythonpandas

Câu trả lời 2

0

전 이렇게 했습니다

for col in df.columns[1:]:
    df[col] = pd.to_timedelta(df[col], errors='coerce').astype('m8[s]').astype(np.int64)

0

저도 지금 강의 듣고 있는데, 똑 같은 에러가 발생했습니다. 혹시 해결 하셨는지요?

 

Hình ảnh hồ sơ của gusrud4168129
gusrud4168129

câu hỏi đã được viết

Đặt câu hỏi