1

I am downloading data from FXCM with fxcmpy, this is what the data looks like:

data downloaded

In the index column I would like only to have the time without the date how can this be done.

This is the code:

import fxcmpy
import pandas as pd
import matplotlib.pyplot as plt

con = fxcmpy.fxcmpy(config_file='fxcm.cfg', server='demo')

# To check if the connection is established
if(con.is_connected):
    print('Connection is established')
else:
    print('Erro in connecting to the server')

data = con.get_candles('USD/JPY', period='m5', number=500)

con.close()

3 Answers3

3

Assuming that your index is already a DatetimeIndex, simply choose the time part from the index:

data.index = data.index.time

If it is not (say, it is a string), convert it to DatetimeIndex first:

data.index = pd.DatetimeIndex(data.index)
DYZ
  • 51,549
  • 10
  • 60
  • 87
0

You have to make sure your df['Index'].dtype has type pandas datetime type dtype('<M8[ns]'). Then you use the following format to extract time. Refer to this answer

 df['Index'].dt.strftime('%H:%m:%S')
kha
  • 308
  • 2
  • 16
0

one way is converting object to datetime then extract year from it.

from datetime import datetime as dt
date="2019-11-21 13:10:00"
fmt="%Y-%m-%d %H:%M:%S"
print(dt.strptime(date,fmt).time())

output

13:10:00
SRG
  • 335
  • 1
  • 9