plotly / plotly/plotly.py

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

Aperta
#2,764 8 commenti 6 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature P3
Lingua principale
Python
Stelle
18.8k
Fork
2.8k
Merge medio
16h 26m
PR unite (30g)
21

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Esegui prima la riproduzione fornita con pandas e Plotly, quindi segui il percorso di PeriodIndex fino alla serializzazione JSON e confrontalo con gli indici di date e stringhe supportati. Il lavoro è completato quando l’esempio viene renderizzato senza richiedere astype(str) e il comportamento di PeriodIndex è coperto da un test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
pandas, python
Ambito
data-visualization
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.