Assigning date indexes and subsetting in time series data

Let's read them and concatenate them to make a single file:

import pandas as pd 
import os os.chdir(' ')
ts1=pd.read_csv('datatraining.txt')
ts2=pd.read_csv('datatest.txt')
ts3=pd.read_csv('datatest2.txt')
ts=pd.concat([ts1,ts2,ts3]

Before using the date column as an index, we will convert it to a datetime format and drop the actual date column:

ts['datetime'] = pd.to_datetime(ts['date'])
ts = ts.set_index('datetime')
ts.drop(['date'], axis=1, inplace=True)

Once the new datetime column is set to an index, it can be used for subsetting. For example, for filtering all the records for a particular day, we can just enclose the data inside the subsetting (square, []) brackets:

ts['2015-02-05']

The output is similar to the following screenshot:

Filtering all records for a particular day

To filter all the records for a particular hour across all days, the following snippet will do the job:

   ts[ts.index.hour==4]

The following is the output:

Filtering all records for a particular hour

We can also filter out all the records between two timestamps by using the following snippet: 

     ts['2015-02-05':'2015-02-06']

The following is the output:

Filtering all records between two timestamps
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset