Dealing with missing data

In the previous example, the rolling_sum column has Nan values, so we can use that data to demonstrate how to deal with missing data.  

Null values appear as NaN in Data Frame when a CSV file contains null values. Fillna() handles and lets the user replace NaN values with their own values, similar to how the pandas dropna() function maintains and removes Null values from a data frame. Filling the missing values in the dataframe in a backward manner is accomplished by passing backfill as the method argument value in fillna(). Fillna() fills the missing values in the dataframe in a forward direction by passing ffill as the method parameter value.

Python3




# importing pandas
import pandas as pd
from datetime import datetime
  
# reading csv file
data = pd.read_csv('covid_data.csv')
  
# converting string data to datetime
data['ObservationDate'] = pd.to_datetime(data['ObservationDate'])
data['Last Update'] = pd.to_datetime(data['Last Update'])
  
# setting index
data = data.set_index('ObservationDate')
  
data = data[['Last Update', 'Confirmed']]
data['rolling_sum'] = data.rolling(5).sum()
print(data.head())
  
# dealing with missing data
data['rolling_backfilled'] = data['rolling_sum'].fillna(method='backfill')
print(data.head(5))


Output:

                        Last Update  Confirmed  rolling_sum
ObservationDate                                            
2020-01-22      2020-01-22 17:00:00        1.0          NaN
2020-01-22      2020-01-22 17:00:00       14.0          NaN
2020-01-22      2020-01-22 17:00:00        6.0          NaN
2020-01-22      2020-01-22 17:00:00        1.0          NaN
2020-01-22      2020-01-22 17:00:00        0.0         22.0
                        Last Update  Confirmed  rolling_sum  rolling_backfilled
ObservationDate                                                                
2020-01-22      2020-01-22 17:00:00        1.0          NaN                22.0
2020-01-22      2020-01-22 17:00:00       14.0          NaN                22.0
2020-01-22      2020-01-22 17:00:00        6.0          NaN                22.0
2020-01-22      2020-01-22 17:00:00        1.0          NaN                22.0
2020-01-22      2020-01-22 17:00:00        0.0         22.0                22.0

Manipulating Time Series Data in Python

A collection of observations (activity) for a single subject (entity) at various time intervals is known as time-series data. In the case of metrics, time series are equally spaced and in the case of events, time series are unequally spaced. We may add the date and time for each record in this Pandas module, as well as fetch dataframe records and discover data inside a specific date and time range. 

Similar Reads

Generate a date range:

Pandas package is imported. pd.date_range() method is used to create a date range, the date range has a monthly frequency....

Operations on timestamp data:

...

Convert data from a string to a timestamp:

The date range is converted into a dataframe with the help of pd.DataFrame() method. The column is converted to DateTime using to_datetime() method. info() method gives information about the dataframe if there are any null values and the datatype of the columns....

Slicing and indexing time series data:

...

Resampling time series data for various aggregates/summary statistics for different time periods:

if we have a list of string data that resembles DateTime, we can first convert it to a dataframe using pd.DataFrame() method and convert it to DateTime column using pd.to_datetime() method....

Calculate a rolling statistic like a rolling average:

...

Dealing with missing data:

...

Fundamentals of Unix/epoch time:

CSV file is imported in this example and a column with string data is converted into DateTime using pd.to_timestamp() method. That particular column is set as an index which helps us slice and index data accordingly. data. loc[‘2020-01-22’][:10]  indexes data on the day ‘2020-01-22’ and the result is further sliced to return the first 10 observations on that day....