2

I am trying to use the date from the "fechas" column to make a analysis with pandas, can someone explain how to give the date format to that column.

                                               LEY         Unnamed: 3  \
0    Por medio de la cual se regula el uso del Desf...              Salud   
1    Por medio de la cual se adopta la estrategia S...              Salud   
2    Por medio de la cual se incentiva la adecuada ...              Salud   
3    Por la cual se decreta el presupuesto de renta...              Salud   
4     Por medio de la cual se modifican la Ley 73 d...              Salud   
5    Por medio de la cual se prohíben los procedimi...              Salud    


                 FECHA  
0        3 de agosto, 2017  
1       1 de febrero, 2017  
2       1 de febrero, 2017  
3       8 de febrero, 2017  
4       1 de febrero, 2017  
5       1 de febrero, 2017  

AÑO                            NOMBRE  \
0    2017.0                  Ley 1831 de 2017   
1    2017.0   Ley 1823 del 4 de enero de 2017   
2    2017.0   Ley 1822 del 4 de enero de 2017   
3    2016.0                  Ley 1815 de 2016   
4    2016.0  Ley 1805 del 4 de agosto de 2016   
5    2016.0   Ley 1799 del 25 de julio de2016   

1 Answer 1

4

The dateparser module is capable of handling numerous languages including french, russian, spanish, dutch and over 20 more. It also can recognize stuff like time zone abbreviations, etc.

import dateparser
dateparser.parse('3 de agosto, 2017')

# output - datetime.datetime(2017, 8, 3, 0, 0)

Now use the dateparser module with pandas.apply() to get what you want -

df['FECHA'] = df['FECHA'].apply(lambda x: dateparser.parse(x))

Or simply -

df['FECHA'].apply(dateparser.parse)

Or thanks to @jpp -

df = pd.read_csv('file.csv', parse_dates=['FECHA'], date_parser=dateparser.parse)

Output

0   2017-08-03
1   2017-02-01
2   2017-02-01
3   2017-02-08
4   2017-02-01
5   2017-02-01
Name: FECHA, dtype: datetime64[ns]
Sign up to request clarification or add additional context in comments.

7 Comments

I am working in Jupyter with Anaconda, and the module don't works, do you know some alternative ?
@SebastiánRamírez i just tested in Jupyter and it works fine
@jpp makes sense. Added it to ans
@SebastiánRamírez do a pip install dateparser to install the module and it should be fine
@SebastiánRamírez Make sure your file is actually utf-8 encoded and not ANSI or something else
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.