day_name python 中的西班牙语区域设置不适用于 windows

day_name spanish locale in python does not work for windows

有一个 pandas 数据框,其中包含我要提取工作日的日期:

    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

只需要在开头加上两行:

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

并删除您使用的所有语言环境功能。当您打印 Serie 时,它​​将以西班牙语显示。