使用区域设置将西班牙语日期转换为python pandas datetime对象 [英] Converting spanish date into python pandas datetime object with locale setting

查看:186
本文介绍了使用区域设置将西班牙语日期转换为python pandas datetime对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个问题:

  1. 如何使用 pandas 将西班牙日期时间"ago122010"转换为2010-08-12. strptime 中使用的格式正确吗?
  1. How can I convert a Spanish datetime 'ago122010' into 2010-08-12 using pandas. Is the format used in strptime correct?

我尝试了以下操作:

import locale
locale.setlocale(locale.LC_ALL, 'es_ES.utf8')

from datetime import datetime as dt

df['date'] = dt.strptime(df['date'], '%b%d%Y')
df['date'] = df['date'].strftime('%Y-%m-%d')

但出现以下错误:

Error: unsupported locale setting

  1. 如何使用 pandas 将此'20100812'转换为日期时间.
  1. how can I convert this '20100812' to a datetime using pandas.

推荐答案

认为问题在于所使用的语言环境.另外,请检查您的格式.

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

输出:

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

让我知道这是否可以解决您的问题.

Let me know if this solves your problem.

尝试了更多示例.

# 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"))

输出:

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

输出

2010-08-12

2010-08-12

这篇关于使用区域设置将西班牙语日期转换为python pandas datetime对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆