plotly / plotly/plotly.py

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

Ouverte
#2,764 8 commentaires 6 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

feature P3
Langage dominant
Python
Étoiles
18.8k
Forks
2.8k
Merge moyen
16 h 26 min
PR mergées (30 j)
21

Description

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()

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Exécutez d’abord la reproduction fournie avec pandas et Plotly, puis suivez le chemin de PeriodIndex jusqu’à la sérialisation JSON et comparez-le aux index de dates et de chaînes pris en charge. Le travail est terminé lorsque l’exemple se rend sans nécessiter astype(str) et que le comportement de PeriodIndex est couvert par un test.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
pandas, python
Domaine
data-visualization
Type d'issue
Fonctionnalité
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.