Home > Enterprise >  strptime() argument 1 must be str, not Series
strptime() argument 1 must be str, not Series

Time:01-28

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

CodePudding user response:

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)


CodePudding user response:

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()])
  •  Tags:  
  • Related