我想更改数据的日期格式,但似乎不起作用。
这是当我尝试运行我的代码时它给我的错误";未转换的数据仍然存在:GMT-0400";
这是我的代码:
import pandas as pd
import numpy as np
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt
# import our historical data
data = pd.read_csv('Data/EURUSD.csv')
data.columns = ['Date', 'open', 'high', 'low', 'close', 'vol']
data = data.drop_duplicates(keep=False)
data.Date = pd.to_datetime(data.Date, format='%d.%m.%Y %H:%M:%S.%f')
data = data.set_index(data.Date)
data = data['open','high','low','close','vol']
price = data.close.iloc[:100]
# find our relative extrema
max_idx = argrelextrema(price.values, np.greater, order=1)
min_idx = argrelextrema(price.values, np.less, order=1)
print(max_idx)
print(min_idx)
EURUSD.CSV
Local time,Open,High,Low,Close,Volume
14.03.2022 00:00:00.000 GMT-0400,1.09049,1.09177,1.09015,1.09175,6011.13
14.03.2022 01:00:00.000 GMT-0400,1.09175,1.09186,1.09089,1.09116,7262.199999999998
14.03.2022 02:00:00.000 GMT-0400,1.09114,1.09231,1.09093,1.09178,5828.28
14.03.2022 03:00:00.000 GMT-0400,1.09177,1.09407,1.09109,1.09320,12058.280000000004
14.03.2022 04:00:00.000 GMT-0400,1.09321,1.09601,1.09303,1.09567,14770.299999999996
更改此行:
data.Date = pd.to_datetime(data.Date, format='%d.%m.%Y %H:%M:%S.%f')
至:
data.Date = pd.to_datetime(data.Date, format='%d%b%Y:%H:%M:%S.%f')
如果你想定义一个特定的格式,你必须删除GMT
,因为pd.to_datetime不会同时解析和UTC偏移量:
>>> pd.to_datetime('14.03.2022 00:00:00.000 GMT-0400'.replace('GMT', ''),
format='%d.%m.%Y %H:%M:%S.%f %z')
Timestamp('2022-03-14 00:00:00-0400', tz='pytz.FixedOffset(-240)')
或制作";GMT";解析指令的一部分:
>>> pd.to_datetime('14.03.2022 00:00:00.000 GMT-0400',
format='%d.%m.%Y %H:%M:%S.%f GMT%z')
或者,您可以让解析器自动推断格式(谨慎使用(:
>>> pd.to_datetime('14.03.2022 00:00:00.000 GMT-0400', dayfirst=True)
Timestamp('2022-03-14 00:00:00+0400', tz='pytz.FixedOffset(240)')