How to utilise timeseries in pandas?
PANDAS CHEATSHEET DATA CLEANING PYTHON DATA MUNGING MACHINE LEARNING RECIPES     ALL TAGS

How to utilise timeseries in pandas?

How to utilise timeseries in pandas?

This recipe helps you utilise timeseries in pandas

0
This data science python source code does the following: 1. Creating your own data dictionary and converting it to dataframe. 2. Timestamping of the categorical features(Dates). 3. Loads the timestamped data and performings basic EDA.
In [2]:
## How to utilise timeseries in pandas
def Snippet_112():
    print()
    print(format('How to utilise timeseries in pandas','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    from datetime import datetime
    import pandas as pd

    # %matplotlib inline
    import matplotlib.pyplot as pyplot

    # Create a dataframe
    data = {'date': ['2014-05-01 18:47:05.069722', '2014-05-01 18:47:05.119994',
                     '2014-05-02 18:47:05.178768', '2014-05-02 18:47:05.230071',
                     '2014-05-02 18:47:05.230071', '2014-05-02 18:47:05.280592',
                     '2014-05-03 18:47:05.332662', '2014-05-03 18:47:05.385109',
                     '2014-05-04 18:47:05.436523', '2014-05-04 18:47:05.486877'],
            'car_sales': [34, 25, 26, 15, 15, 14, 26, 25, 62, 41]}
    df = pd.DataFrame(data, columns = ['date', 'car_sales'])
    print(df)

    # Convert df['date'] from string to datetime
    df['date'] = pd.to_datetime(df['date'])

    # Set df['date'] as the index and delete the column
    df.index = df['date']
    del df['date']
    print(); print(df)

    # View all observations that occured in 2014
    print(); print(df['2014'])

    # View all observations that occured in May 2014
    print(); print(df['2014-05'])

    # Observations after May 3rd, 2014
    print(); print(df[datetime(2014, 5, 3):])

    # Observations between May 3rd and May 4th
    print(); print(df['5/3/2014':'5/4/2014'])

    # Truncation observations after May 2nd 2014
    print(); print(df.truncate(after='5/3/2014'))

    # Observations of May 2014
    print(); print(df['5-2014'])

    # Count the number of observations per timestamp
    print(); print(df.groupby(level=0).count())

    # Mean value of car_sales per day
    print(); print(df.resample('D').mean())

    # Total value of car_sales per day
    print(); print(df.resample('D').sum())

    # Plot of the total car_sales per day
    df.resample('D').sum().plot(); pyplot.show()

Snippet_112()
***********************How to utilise timeseries in pandas************************
                         date  car_sales
0  2014-05-01 18:47:05.069722         34
1  2014-05-01 18:47:05.119994         25
2  2014-05-02 18:47:05.178768         26
3  2014-05-02 18:47:05.230071         15
4  2014-05-02 18:47:05.230071         15
5  2014-05-02 18:47:05.280592         14
6  2014-05-03 18:47:05.332662         26
7  2014-05-03 18:47:05.385109         25
8  2014-05-04 18:47:05.436523         62
9  2014-05-04 18:47:05.486877         41

                            car_sales
date
2014-05-01 18:47:05.069722         34
2014-05-01 18:47:05.119994         25
2014-05-02 18:47:05.178768         26
2014-05-02 18:47:05.230071         15
2014-05-02 18:47:05.230071         15
2014-05-02 18:47:05.280592         14
2014-05-03 18:47:05.332662         26
2014-05-03 18:47:05.385109         25
2014-05-04 18:47:05.436523         62
2014-05-04 18:47:05.486877         41

                            car_sales
date
2014-05-01 18:47:05.069722         34
2014-05-01 18:47:05.119994         25
2014-05-02 18:47:05.178768         26
2014-05-02 18:47:05.230071         15
2014-05-02 18:47:05.230071         15
2014-05-02 18:47:05.280592         14
2014-05-03 18:47:05.332662         26
2014-05-03 18:47:05.385109         25
2014-05-04 18:47:05.436523         62
2014-05-04 18:47:05.486877         41

                            car_sales
date
2014-05-01 18:47:05.069722         34
2014-05-01 18:47:05.119994         25
2014-05-02 18:47:05.178768         26
2014-05-02 18:47:05.230071         15
2014-05-02 18:47:05.230071         15
2014-05-02 18:47:05.280592         14
2014-05-03 18:47:05.332662         26
2014-05-03 18:47:05.385109         25
2014-05-04 18:47:05.436523         62
2014-05-04 18:47:05.486877         41

                            car_sales
date
2014-05-03 18:47:05.332662         26
2014-05-03 18:47:05.385109         25
2014-05-04 18:47:05.436523         62
2014-05-04 18:47:05.486877         41

                            car_sales
date
2014-05-03 18:47:05.332662         26
2014-05-03 18:47:05.385109         25
2014-05-04 18:47:05.436523         62
2014-05-04 18:47:05.486877         41

                            car_sales
date
2014-05-01 18:47:05.069722         34
2014-05-01 18:47:05.119994         25
2014-05-02 18:47:05.178768         26
2014-05-02 18:47:05.230071         15
2014-05-02 18:47:05.230071         15
2014-05-02 18:47:05.280592         14

                            car_sales
date
2014-05-01 18:47:05.069722         34
2014-05-01 18:47:05.119994         25
2014-05-02 18:47:05.178768         26
2014-05-02 18:47:05.230071         15
2014-05-02 18:47:05.230071         15
2014-05-02 18:47:05.280592         14
2014-05-03 18:47:05.332662         26
2014-05-03 18:47:05.385109         25
2014-05-04 18:47:05.436523         62
2014-05-04 18:47:05.486877         41

                            car_sales
date
2014-05-01 18:47:05.069722          1
2014-05-01 18:47:05.119994          1
2014-05-02 18:47:05.178768          1
2014-05-02 18:47:05.230071          2
2014-05-02 18:47:05.280592          1
2014-05-03 18:47:05.332662          1
2014-05-03 18:47:05.385109          1
2014-05-04 18:47:05.436523          1
2014-05-04 18:47:05.486877          1

            car_sales
date
2014-05-01       29.5
2014-05-02       17.5
2014-05-03       25.5
2014-05-04       51.5

            car_sales
date
2014-05-01         59
2014-05-02         70
2014-05-03         51
2014-05-04        103
In [ ]:

Relevant Projects

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.