plotly / plotly/plotly.py

add support for Pandas Time spans as index col (PeriodIndex)

未关闭
#2,764 8 条评论 6 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

feature P3
主要语言
Python
星标
18.8k
派生
2.8k
平均合并
16 小时 26 分钟
30 天内合并 PR
21

描述

Hello,

I'd like to use the pandas PeriodIndex date format to work with and calculate things in a time series way.

The manual says that Plotly auto-sets the axis type to a date format when the corresponding data are either ISO-formatted date strings or if they're a date pandas column or datetime NumPy array. (https://plotly.com/python/time-series/)

So I guess, since a PeriodIndex isn't of the concept "date" but of the concept "Time spans", Plotl.ly isn't able to auto-set this axis type? The error I get is a TypeError: Object of type Period is not JSON serializable

I know I can solve this with converting to another format (#df.index = df.index.astype(str)), but it would be great to have a native solution inside plot.ly that can understand PeriodIndex.

Here is my example code:

import pandas as pd
import plotly.express as px


### READ IN DATA ###
d = {
    'one':
    pd.Series([1., 2.],
              index=[
                  'Jan/2020', 'Feb/2020', 
              ])
}

df = pd.DataFrame(d)

print(f'\n# this is what I get from my datasource\n')
print(df.index)
print(df)

### CONVERT DATA ###
idx = df.index
df.index = pd.to_datetime(idx, format="%b/%Y").to_period(freq='M')

print(f'\n# after conversion\n')
print(df.index)

df.index.name = 'Monat'
print(df)

df = df.sort_index()

###THIS FIXES IT, BUT :/ ###
#df.index = df.index.astype(str)

fig = px.line(df, x=df.index, y=('one'), title='Testplot')
fig.show()

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先运行提供的 pandas 和 Plotly 复现代码,然后跟踪 PeriodIndex 如何进入 JSON 序列化流程,并将其与受支持的日期索引和字符串索引进行比较。完成标准是示例无需 astype(str) 即可渲染,并且 PeriodIndex 的行为有测试覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
pandas, python
领域
data-visualization
Issue 类型
功能
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。