2017-06-17 60 views
2

嗨,我從OANDA收到我的數據,並通過Django的在charts.js我如何可以重新格式化日期

class ChartData(APIView): 
    def get(request, *args, **kwargs): 


data = oanda.get_history(instrument='EUR_USD', # our instrument 
          start='2016-10-01' , # start data 
          end=time.strftime("%Y-%m-%d"), # end date 
          granularity='D') # minute bars # 7 
    df = pd.DataFrame(data['candles']) # 8 

    data50 = oanda.get_history(instrument='EUR_USD', # our instrument 
          start='2016-10-01', # start data 
          end='2017-01-01', # end date 
          granularity='D') # minute bars # 7 
    df50 = pd.DataFrame(data50['candles']) # 8 

    data200 = oanda.get_history(instrument='EUR_USD', # our instrument 
          start='2016-6-01', # start data 
          end='2017-01-01', # end date 
          granularity='D') # minute bars # 7 
    df200 = pd.DataFrame(data200['candles']) # 8 

    history = df['openBid'] 
    date_labels = df['time'] 


    history = df['openBid'] 
    date_labels = df['time'] 

    data = { 
      "history": history, 
      "date_labels": date_labels, 
     } 

    return Response(data) 

現在的日期格式,你可以在截圖中看到繪製。 我該如何格式化日期格式爲DD/MM/JJJJ?

在此先感謝

Screenshot

回答

2

你傳遞的響應數據到您的模板之前,你可以格式化你的數據框,爲oanda將與格式2014-02-12T14:50:25Z返回time,你可以用fomat格式化df['time']DD/MM/YY

df['time']=pd.to_datetime(df['time']).dt.strftime('%d/%m/%Y') 

格式化後,2014-02-12T14:50:25Z將成爲02/07/2014

+0

完美答案thx – Mars

+0

@Mars你可以請把它標記爲接受答案,如果有幫助,謝謝:) –