0

I got a datetime indexed dataframe with 1 entry per hour of the year (format is "2019-01-01 00:00:00" for exemple).

I created a program which will plot every weeks, but some of the plots I obtain are weird

good plot

Weird plot

I was thinking that it may be a continuity problem in my dataframe, some data that would'nt be indexed at the good place, but I don't know how to check this.

If someone got a clue, it'll help me a lot!

Have a nice day all

Edit : I'll try to provide you some code First of all I can't provide you the exact data i'm using since it's professionnal, but I'll try to adapt my code to a randomly generate dataframe

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import os

mpl.rc('figure', max_open_warning = 0)

df = pd.DataFrame({'datetime': pd.date_range('2019-01-01', '2020-12-31',freq='1H', closed='left')})
df['datetime']=pd.to_datetime(df['datetime'])
df['week'] = df['datetime'].dt.isocalendar().week
df['month'] = df['datetime'].dt.month
df=df.set_index(['datetime'])
df=df[['data1','data2','data3','data4','week','month']]

df19=df.loc['2019-01':'2019-12']
df20=df.loc['2020-01':'2020-12']

if not os.path.exists('mypath/Programmes/Semaines/2019'):
    os.mkdir('mypath/Programmes/Semaines/2019')

def graph(a): #Creating the function that will generate all the data I need for 2019 and place them in the good folder, skipping the 1st week of the year cause it's buggued
    for i in range (2,53):
        if not os.path.exists('mypath/Programmes/Semaines/2019/'+str(a)):
            os.mkdir('mypath/Programmes/Semaines/2019/'+str(a))
        folder='mypath/Programmes/Semaines/2019/'+str(a)
        plt.figure(figsize=[20,20])
        x=df19[[a]][(df19["week"]==i)]
        plt.plot(x)
        name=str(a)+"_"+str(i)
        plt.savefig(os.path.join(folder,name))
        
    return

for j in df19.columns :
    graph(j)

Hoping this can help even if i'm not providing the datas directly :/

SonnePer
  • 47
  • 5
  • Use `df.info()` to get a general idea of the data frame first. – r-beginners Apr 26 '21 at 08:28
  • I did it, the non null count is ok on every columns (except 3 missing values in 2 of them), but 10 of my 14 columns are in float64 type, 3 are in int64 and 1 is in UInt32, can this be important? – SonnePer Apr 26 '21 at 08:47
  • If the x-axis is a time series, we need to convert it to, for example, `df['date']=pd.to_datetime(df['date'])`. – r-beginners Apr 26 '21 at 08:50
  • I encourage you to provide a [reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) so that we may help you find a solution that works for your particular problem. It should contain a [small sample of data](https://stackoverflow.com/q/20109391/14148248) and some code showing how you create the plot. Ideally, it should be possible to copy-paste it and run it without any extra effort, as illustrated in the answers I have posted [here](https://stackoverflow.com/a/65579573/14148248) and [here](https://stackoverflow.com/a/66198319/14148248). – Patrick FitzGerald Apr 26 '21 at 14:09
  • Here I provided the code i'm using, hope it can help even If I can't directly provide the data i'm working on – SonnePer Apr 27 '21 at 07:43
  • Maybe the datetime index of the data you are using is not sorted chronologically. Have you tried plotting with pandas directly from the dataframe with `df19[[a]][(df19["week"]==i)].plot()` ? – Patrick FitzGerald May 09 '21 at 12:27

0 Answers0