user13419531
user13419531

Reputation:

plot if datapoint time is missing python dataframe

I have a dataframe that contain weekly data.sample is attached below.

enter image description here

What I want to do is, plot a chart to indicate if any seconds' missing.Here 2020-06-19 11:48:35 is missing. So I want to indicate missing time points in the whole dataset in the plot.

My plan is to create another dataframe with a column called "type" and put 0's available timepoints and 1's for non-available timepoints. Then plot a line chart. But I have no idea how to create this dataframe including non-available points.

Upvotes: 1

Views: 44

Answers (1)

r-beginners
r-beginners

Reputation: 35275

Missing data updates the original data frame by creating time series data at the frequency required by the minimum and maximum time series values of the original data frame. df.reindex() is used.

import pandas as pd
import numpy as np
import io

data = '''
time WindSpeed
"2020-06-19 11:48:32" 3.11
"2020-06-19 11:48:33" 3.37
"2020-06-19 11:48:34" 4.28
"2020-06-19 11:48:36" 5.34
"2020-06-19 11:48:37" 5.87
"2020-06-19 11:48:39" 6.03
'''

df = pd.read_csv(io.StringIO(data), sep='\s+')

df['time'] = pd.to_datetime(df['time'])
df.set_index('time', inplace=True)
df = df.reindex(pd.date_range(df.index.min(), df.index.max(), freq='1s'))
df.plot(kind='line', lw=2, color='b', marker='o', grid=True)

enter image description here

Upvotes: 1

Related Questions