如何检查日期时间索引数据帧的连续性

时间:2021-04-26 08:20:58

标签: python pandas matplotlib

我得到了一个日期时间索引的数据框,一年中每小时有 1 个条目(例如,格式为“2019-01-01 00:00:00”)。

我创建了一个程序,它会每周绘制一次,但我得到的一些绘图很奇怪

good plot

Weird plot

我在想这可能是我的数据框中的连续性问题,有些数据不会在合适的位置编入索引,但我不知道如何检查。

如果有人有线索,对我很有帮助!

祝大家有个美好的一天

编辑:我会尝试为您提供一些代码 首先,我无法为您提供我正在使用的确切数据,因为它是专业的,但我会尝试将我的代码调整为随机生成的数据框

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)

即使我不直接提供数据,也希望这能有所帮助:/

0 个答案:

没有答案
相关问题