Convert months to German acronym

Question:

I am looking for a solution to convert English names for "Jan, Feb, Mar, Apr, May" to "Jan, Feb, Mär, Apr, Mai". I already tried to include "locale". I also have a column with a Timestamp-Series, may this helps

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
Asked By: Alex

||

Answers:

Try using locale.setlocale(locale.LC_TIME, 'de_DE') instead of locale.setlocale(locale.LC_TIME, 'deu_deu') (deu is not a valid locale name)

Answered By: TheEagle

may somebody has a more elegant solution.

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]
Answered By: Alex
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.