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()
Mahmoud Abdel-Rahman
  • 497
  • 2
  • 10
  • 27

3 Answers3

4

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
  • 55,249
  • 10
  • 64
  • 93
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
  • 349
  • 3
  • 18
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
  • 345
  • 1
  • 9