Change encoding in pd.to_datetime

Asked

Viewed 608 times

1

wondered if there is the possibility to change the encoding of an element returned by the function to_datetime pandas. I need to switch 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 function read_csv pandas.

Obs 2.: I needed to define the encoding ISO-8859-1 at the time of loading the files with read_csv, because it was giving error with the standard encoding (utf-8, I believe).

formatting the date entered by the user:

Obs.: I tried using the format parameter, but no effect.

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)
  • Have you tried comparing: data1.isoformat()==data2.isoformat()? or convert the two dates into tuples and compare the elements? The idea would be that you put something else in the question, part of the file, for example or how the user makes the inclusion.

  • Managed to resolve that issue?

1 answer

2


The .to_datetime pandas seems to accept dates in various formats, so I believe you could read both the date of your TXT and any other, 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'))

Upshot: resultado de comparação de datas

Browser other questions tagged

You are not signed in. Login or sign up in order to post.