Python 3.x 如何删除数据框中的日期和时间?
我正在使用fxcmpy从FXCM下载数据,数据如下所示: 在索引栏中,我只想知道没有日期的时间,这是怎么做到的 代码如下:Python 3.x 如何删除数据框中的日期和时间?,python-3.x,pandas,Python 3.x,Pandas,我正在使用fxcmpy从FXCM下载数据,数据如下所示: 在索引栏中,我只想知道没有日期的时间,这是怎么做到的 代码如下: 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_connec
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()
假设您的索引已经是
DatetimeIndex
,只需从索引中选择时间部分:
data.index = data.index.time
如果不是(比如说,它是一个字符串),首先将其转换为DatetimeIndex
:
data.index = pd.DatetimeIndex(data.index)
您必须确保df['Index'].dtype的类型为datetime type
dtype(“一种方法是将对象转换为datetime,然后从中提取年份
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())
输出
13:10:00
请您检查一下我的解决方案,让我知道这是否对您有帮助?谢谢。
13:10:00