Home > Software design >  Change date format of data : unconverted data remains: GMT-0400
Change date format of data : unconverted data remains: GMT-0400

Time:03-23

I want to change the date format of my data but it doesn't seem to work.

here is the error that it give me when I try to run my code " unconverted data remains: GMT-0400 "

here my code:

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

CodePudding user response:

Change this line :

data.Date = pd.to_datetime(data.Date, format='%d.%m.%Y %H:%M:%S.%f')

to :

data.Date = pd.to_datetime(data.Date, format='%d%b%Y:%H:%M:%S.%f')

CodePudding user response:

If you want to define a specific format, you'll have to remove GMT since pd.to_datetime won't parse both that and a UTC offset:

>>> 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)')

or make "GMT" part of the parsing directive:

>>> pd.to_datetime('14.03.2022 00:00:00.000 GMT-0400', 
                   format='%d.%m.%Y %H:%M:%S.%f GMT%z')

Alternatively, you can let the parser auto-infer the format (use with caution):

>>> 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)')
  • Related