簡體   English   中英

strptime() 參數 1 必須是 str,而不是 Series

[英]strptime() argument 1 must be str, not Series

我想將歷史 ['startTime'] 列中的每一行轉換為日期,但它顯示錯誤。

我究竟做錯了什么?

import pandas as pd
import requests
from datetime import datetime

historical = requests.get('https://ftx.com/api/markets/usdt-perp/candles?resolution=14400&start_time=1309062800').json()

historical = pd.DataFrame(historical['result'])
historical['startTime'] = datetime.strptime(historical['startTime'], '%d/%m/%y %H')

您正在字符串中獲取startTime ,因此讓我們首先將其轉換為 datetime 對象。

import pandas as pd
import requests

historical = requests.get(
    'https://ftx.com/api/markets/usdt-perp/candles?resolution=14400&start_time=1309062800').json()
historical = pd.DataFrame(historical['result'])

historical['startTime'] = pd.to_datetime(
    historical['startTime']).dt.strftime('%d/%m/%y %H')


print(historical)


這是一種根本不需要日期時間庫的方法

historical['startTime'] = list([pd.to_datetime(x, format='%d/%m/%y %H') for x in historical['startTime'].to_list()])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM