可以更改 Altair 或 Pandas 上的日期语言吗?

It is possible to change the date language on Altair or Pandas?

我不知道这个任务是否可以从 Altair 或 Pandas 完成,但我正在寻找文档来更改我的图形的 date 语言。

这是我的代码:

import pandas as pd
import altair as alt
from datetime import datetime, timedelta

url = 'https://raw.githubusercontent.com/mariorz/covid19-mx-time-series/master/data/covid19_confirmed_mx.csv'
df = pd.read_csv(url, index_col=0)
#df = pd.read_csv(url)

df = df.loc['Colima','18-03-2020':'18-11-2020']
df = pd.DataFrame(df)
df.index = pd.to_datetime(df.index, format='%d-%m-%Y')

%run urban_theme.py

alt.Chart(df.reset_index()).mark_line().encode(
    alt.X('index:T', title = " "),
    alt.Y('Colima:Q', title = " "),
).properties(
    title = "Casos acumulados",
)

输出:

目前没有很好的文档,但在中有一些相关信息。

您可以像这样为您的图表设置西班牙时间格式区域设置:

import pandas as pd
import altair as alt
from datetime import datetime, timedelta
from urllib import request
import json

# fetch & enable a Spanish timeFormat locale.
with request.urlopen('https://raw.githubusercontent.com/d3/d3-time-format/master/locale/es-ES.json') as f:
  es_time_format = json.load(f)
alt.renderers.set_embed_options(timeFormatLocale=es_time_format)

url = 'https://raw.githubusercontent.com/mariorz/covid19-mx-time-series/master/data/covid19_confirmed_mx.csv'
df = pd.read_csv(url, index_col=0)
#df = pd.read_csv(url)

df = df.loc['Colima','18-03-2020':'18-11-2020']
df = pd.DataFrame(df)
df.index = pd.to_datetime(df.index, format='%d-%m-%Y')

alt.Chart(df.reset_index()).mark_line().encode(
    alt.X('index:T', title = " "),
    alt.Y('Colima:Q', title = " "),
).properties(
    title = "Casos acumulados",
    width = 800
)