简体   繁体   中英

strptime() argument 1 must be str, not Series

I want to convert every row form the historical['startTime'] column to dates but it is showing an error.

What am I doing wrong?

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')

you are getting startTime in string, so lets first convert that to datetime objects.

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)


Here is an approach which doesn't require the datetime library at all

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM