plotly / plotly/plotly.py

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

Offen
#2,764 8 Kommentare 6 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature P3
Vorherrschende Sprache
Python
Sterne
18.8k
Forks
2.8k
Ø Merge
16 Std. 26 Min.
Gemergte PRs (30 T.)
21

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Führe zuerst die bereitgestellte pandas- und Plotly-Reproduktion aus und verfolge dann, wie der PeriodIndex die JSON-Serialisierung erreicht, und vergleiche ihn mit unterstützten Datums- und String-Indizes. Als abgeschlossen gilt die Aufgabe, wenn das Beispiel ohne astype(str) gerendert wird und das Verhalten von PeriodIndex durch einen Test abgedeckt ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
pandas, python
Bereich
data-visualization
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.