将月份转换为德语首字母缩略词

时间:2021-03-15 13:01:50

标签: python

我正在寻找将“Jan、Feb、Mar、Apr、May”的英文名称转换为“Jan、Feb、Mär、Apr、Mai”的解决方案。我已经尝试包括“语言环境”。我还有一个带有时间戳系列的专栏,这可能会有所帮助

import locale
import pandas as pd

locale.getlocale()
locale.setlocale(locale.LC_TIME, 'deu_deu')

d = {'en_month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
     'Datetimeindex':['2021-01-31', '2021-02-28', '2021-03-31', '2021-04-30','2021-05-31' ]}
df = pd.DataFrame(data=d)
df['Datetimeindex'] =  pd.to_datetime(df['Datetimeindex'],  format= '%Y/%m/%d')

#df['german'] = df['Datetimeindex'].strftime('%B') #< not the solution

2 个答案:

答案 0 :(得分:1)

尝试使用 locale.setlocale(locale.LC_TIME, 'de_DE') 而不是 locale.setlocale(locale.LC_TIME, 'deu_deu')(deu 不是有效的语言环境名称)

答案 1 :(得分:0)

也许有人有更优雅的解决方案。

import locale
import pandas as pd
locale.getlocale()
locale.setlocale(locale.LC_TIME, 'de_DE')

d = {'en_month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
     'Datetimeindex':['2021-01-31', '2021-02-28', '2021-03-31', '2021-04-30','2021-05-31' ]}
df = pd.DataFrame(data=d)
df['Datetimeindex'] =  pd.to_datetime(df['Datetimeindex'],  format= '%Y/%m/%d')

df['new'] = df['Datetimeindex'].dt.strftime('%B')
df['new'] = df['new'].str[:3]