#!/usr/bin/env python # coding: utf-8 # # Simple Timeseries Plots in `pandas` # # Using a dummy dataset... # First, let's create a function to generate a random datetime between two datetimes: # In[30]: #https://stackoverflow.com/a/553448/454773 from random import randrange from datetime import timedelta from datetime import datetime def random_datetime(start=datetime.strptime('1/1/2019 1:30 PM','%d/%m/%Y %I:%M %p'), end=datetime.strptime('31/1/2019 4:50 AM', '%d/%m/%Y %I:%M %p')): """ This function will return a random datetime between two datetime objects. """ delta = end - start int_delta = (delta.days * 24 * 60 * 60) + delta.seconds random_second = randrange(int_delta) return start + timedelta(seconds=random_second) random_datetime() # Now we can createa dummy dataframe with a couple of columns of random data: # In[63]: import numpy as np numsamples = 1000 df=pd.DataFrame({'val1':np.random.rand(numsamples)}) df['datetime']= [random_datetime() for i in range(numsamples)] #Add some periodicity to the second column of random numbers... df['val2']=df.apply(lambda x: x['val1']*(1+np.sin(2*np.pi*(x['datetime'].hour-6)/24)), axis=1) df # In[64]: # median value by hour df.groupby([df['datetime'].dt.hour]).median() # In[67]: ax = df.groupby([df['datetime'].dt.hour]).median().reset_index().plot(kind='scatter', x='datetime',y='val1') df.groupby([df['datetime'].dt.hour]).median().reset_index().plot(kind='scatter', x='datetime',y='val2', color='red', ax=ax)