Change coding in pd.to_datetime

0

I wanted to know if there is a possibility to change the encoding of an element returned by the to_datetime function of pandas. I need to change to ISO-8859-1 encoding to compare the date with others that are stored in an external file (TXT).

The return of to_datetime is formatted as follows: 2017-05-22 (YYYY-m-d)
What is stored in the TXT file is as follows: 2017-22-05 (YYYY-d-m)

Note 1: I am loading the TXT files through the read_csv function of pandas.

Note: I had to set the ISO-8859-1 encoding at the time of loading the files with read_csv, as it was giving error with the default encoding (utf-8, I believe).

formatted date entered by user:

Note: I tried to use the format parameter, but it did not work.

lastDate = pd.to_datetime("2017-05-22")

reading the TXT files:

readingTXT = pd.read_csv(self.fileFolder+file,delimiter="\t",usecols=[0,1,21],encoding="ISO-8859-1",tupleize_cols=True)
    
asked by anonymous 22.05.2017 / 19:18

1 answer

2
  

The% of pandas seems to accept dates in several formats, so I believe you could read both your TXT date and any other date, and compare them after that:

import pandas as pd

minha_data = "2017-22-05" # data no formato do TXT
data_panda = "2017-05-22" # data em outro formato
data_outra = "1495411200.0" # unix

print(pd.to_datetime(minha_data, format='%Y-%d-%m')
  == pd.to_datetime(data_panda, format='%Y-%m-%d')
  == pd.to_datetime(data_outra,unit='s'))
  

Result:   

    
04.06.2017 / 06:05