Converting spanish date into python pandas datetime object with locale setting

后端 未结 1 1551
梦谈多话
梦谈多话 2021-01-12 03:07

I\'ve got 2 questions:

  1. How can I convert a Spanish datetime \'ago122010\' into 2010-08-12 using pandas. Is the format used in strptime
1条回答
  •  北海茫月
    2021-01-12 04:03

    Think the issue is with the locale used. Also, check your formatting.

    import locale
    locale.setlocale(locale.LC_ALL,'es_ES.UTF-8')
    
    from datetime import datetime as dt
    datetime_object = dt.strptime('20100812', '%Y%m%d')
    datetime_object
    

    Output:

    datetime.datetime(2010, 8, 12, 0, 0)
    

    Let me know if this solves your problem.

    Tried few more examples.

    # read a spanish datetime format
    datetime_object = dt.strptime('martes 12 julio 2016', '%A %d %B %Y')
    print(datetime_object.strftime("%B"))
    print(datetime_object)
    
    # Change the locale to swede and print the month
    locale.setlocale(locale.LC_TIME, "sv_SE")  
    print(datetime_object.strftime("%B"))
    

    Output:

    julio
    2016-07-12 00:00:00
    Juli
    

    Edited to incorporate the specific details you wanted:

    import locale
    locale.setlocale(locale.LC_ALL,'es_ES.UTF-8')
    
    from datetime import datetime as dt
    
    datetime_object = dt.strptime('ago122010', '%b%d%Y')
    locale.setlocale(locale.LC_ALL,'en_US.UTF-8')
    print(datetime_object.strftime("%Y-%m-%d"))
    

    Output

    2010-08-12

    0 讨论(0)
提交回复
热议问题