Home > Blockchain >  day_name spanish locale in python does not work for windows
day_name spanish locale in python does not work for windows

Time:05-10

have a pandas dataframe containing dates I want to extract weekday:

    url = 'https://github.com/mattharrison/datasets/raw/master/data/alta-noaa-1980-2019.csv'
    df = pd.read_csv(url)
    
    dates = pd.to_datetime(df.DATE)
    
    dates.dt.day_name()
    
    0          Tuesday
    1        Wednesday
    2         Thursday
    3           Friday
    4         Saturday
               ...    
    14155      Tuesday
    14156    Wednesday
    14157     Thursday
    14158       Friday
    14159     Saturday
    Name: DATE, Length: 14160, dtype: object
    
    
    locale.getlocale()
    
    ('es_ES', 'cp1252')
    
    locale.setlocale(locale.LC_ALL,'')
    
    'Spanish_Spain.1252'
    
    locale.setlocale(locale.LC_ALL,"Spanish_Spain.1252")
    
    'Spanish_Spain.1252'
    
    dates.dt.day_name("Spanish_Spain.1252")
    
    Error: unsupported locale setting

dates.dt.day_name("es_ES")

    Error: unsupported locale setting

CodePudding user response:

You only need to add two lines at the begining:

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

And delete all your locale functions used. When you print the Serie, it will be shown in Spanish.

  • Related