0% found this document useful (0 votes)
30 views5 pages

Time Series - Jupyter Notebook 2

The document discusses working with time series data using Python's Pandas library, highlighting tools for handling dates and times. It covers various data types, such as datetime, PeriodIndex, and TimedeltaIndex, along with examples of indexing and creating date ranges. Additionally, it explains how to specify frequencies and offsets for time series data.

Uploaded by

kruthiksai34882
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

Time Series - Jupyter Notebook 2

The document discusses working with time series data using Python's Pandas library, highlighting tools for handling dates and times. It covers various data types, such as datetime, PeriodIndex, and TimedeltaIndex, along with examples of indexing and creating date ranges. Additionally, it explains how to specify frequencies and offsets for time series data.

Uploaded by

kruthiksai34882
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

2/22/25, 10:03 AM Time Series - Jupyter Notebook

Working with Time Series


Pandas has an extensive set of tools for working with dates, times, and timeindexed data.

Date and time data comes in a few flavors,


which we will discuss here:

Dates and Times in Python

Native Python dates and times: datetime and


dateutil
Python’s basic objects for working with dates and times reside in the built-in date time module.

Along with the third-party dateutil module, you can use it to quickly perform a host of useful
functionalities on dates and times.

For example, you can manually build a date using the datetime type:

In [1]:  from datetime import datetime


datetime(year=2015, month=7, day=4)

Out[1]: datetime.datetime(2015, 7, 4, 0, 0)

In [2]:  #using the dateutil module, you can parse dates from a variety of string fo

In [2]:  from dateutil import parser


date = parser.parse("4th of July, 2015")
date

Out[2]: datetime.datetime(2015, 7, 4, 0, 0)

localhost:8888/notebooks/Time Series.ipynb# 1/5


2/22/25, 10:03 AM Time Series - Jupyter Notebook

In [4]:  #Once you have a datetime object, you can do things like printing the day o
#%A- day, %w- Weekday as no..., %b-month short

In [9]:  date.strftime('%w')

Out[9]: '6'

In [6]:  #one of the standard string format codes for printing dates ("%A"),
#which can read in the strftime section of Python’s datetime documentation.

Pandas Time Series: Indexing by Time


In [7]:  #Pandas time series tools really become useful when you begin to index data
#For example, we can construct a Series object that has timeindexed data:

In [9]:  import pandas as pd


import numpy as np

In [10]:  index = pd.DatetimeIndex(['2014-07-04', '2014-08-04', '2015-07-04', '2015-0


data = pd.Series([0, 1, 2, 3], index=index)
data

Out[10]: 2014-07-04 0
2014-08-04 1
2015-07-04 2
2015-08-04 3
dtype: int64

In [11]:  #Series indexing patterns

In [12]:  data['2014-07-04':'2015-07-04']

Out[12]: 2014-07-04 0
2014-08-04 1
2015-07-04 2
dtype: int64

In [13]:  #date-only indexing operations, such as passing a year to


#obtain a slice of all data from that year:

In [14]:  data['2015']

Out[14]: 2015-07-04 2
2015-08-04 3
dtype: int64

Pandas Time Series Data Structures


localhost:8888/notebooks/Time Series.ipynb# 2/5
2/22/25, 10:03 AM Time Series - Jupyter Notebook

Data structures for working with time series data:

In [15]:  #pd.to_datetime() function, which can parse a wide variety of formats.


#Passing a single date to pd.to_datetime() yields a Timestamp;
#Passing a series of dates by default yields a DatetimeIndex:

In [16]:  dates = pd.to_datetime([datetime(2015, 7, 3), '4th of July, 2015',


'2015-Jul-6', '07-07-2015', '20150708'])
dates

Out[16]: DatetimeIndex(['2015-07-03', '2015-07-04', '2015-07-06', '2015-07-07',


'2015-07-08'],
dtype='datetime64[ns]', freq=None)

In [17]:  #DatetimeIndex can be converted to a PeriodIndex with the to_period() funct


#with the addition of a frequency code; here we’ll use 'D' to indicate dail

In [41]:  dates.to_period('D')

Out[41]: PeriodIndex(['2015-07-03', '2015-07-04', '2015-07-06', '2015-07-07',


'2015-07-08'],
dtype='period[D]')

In [19]:  #TimedeltaIndex is created, for example, when one date is subtracted from a

In [20]:  dates - dates[0]

Out[20]: TimedeltaIndex(['0 days', '1 days', '3 days', '4 days', '5 days'], dtype
='timedelta64[ns]', freq=None)

In [21]:  #Regular sequences: pd.date_range()


#pd.date_range() for timestamps,
#pd.period_range() for periods
#pd.timedelta_range() for time deltas.

In [22]:  pd.date_range('2015-07-03', '2015-07-10')

Out[22]: DatetimeIndex(['2015-07-03', '2015-07-04', '2015-07-05', '2015-07-06',


'2015-07-07', '2015-07-08', '2015-07-09', '2015-07-10'],
dtype='datetime64[ns]', freq='D')

In [25]:  pd.date_range('2015-07-03', periods=8)

Out[25]: DatetimeIndex(['2015-07-03', '2015-07-04', '2015-07-05', '2015-07-06',


'2015-07-07', '2015-07-08', '2015-07-09', '2015-07-10'],
dtype='datetime64[ns]', freq='D')

localhost:8888/notebooks/Time Series.ipynb# 3/5


2/22/25, 10:03 AM Time Series - Jupyter Notebook

In [26]:  pd.date_range('2015-07-03', periods=8, freq='H')

Out[26]: DatetimeIndex(['2015-07-03 00:00:00', '2015-07-03 01:00:00',


'2015-07-03 02:00:00', '2015-07-03 03:00:00',
'2015-07-03 04:00:00', '2015-07-03 05:00:00',
'2015-07-03 06:00:00', '2015-07-03 07:00:00'],
dtype='datetime64[ns]', freq='H')

In [27]:  #monthly periods:

In [28]:  pd.period_range('2015-07', periods=8, freq='M')

Out[28]: PeriodIndex(['2015-07', '2015-08', '2015-09', '2015-10', '2015-11', '2015


-12',
'2016-01', '2016-02'],
dtype='period[M]')

In [29]:  #sequence of durations increasing by an hour:

In [30]:  pd.timedelta_range(0, periods=10, freq='H')

Out[30]: TimedeltaIndex(['0 days 00:00:00', '0 days 01:00:00', '0 days 02:00:00',
'0 days 03:00:00', '0 days 04:00:00', '0 days 05:00:00',
'0 days 06:00:00', '0 days 07:00:00', '0 days 08:00:00',
'0 days 09:00:00'],
dtype='timedelta64[ns]', freq='H')

Frequencies and Offsets


Codes to specify any desired frequency spacing.

localhost:8888/notebooks/Time Series.ipynb# 4/5


2/22/25, 10:03 AM Time Series - Jupyter Notebook

In [31]:  pd.timedelta_range(0, periods=10, freq='T')

Out[31]: TimedeltaIndex(['0 days 00:00:00', '0 days 00:01:00', '0 days 00:02:00',
'0 days 00:03:00', '0 days 00:04:00', '0 days 00:05:00',
'0 days 00:06:00', '0 days 00:07:00', '0 days 00:08:00',
'0 days 00:09:00'],
dtype='timedelta64[ns]', freq='T')

#The monthly, quarterly, and annual frequencies are all marked at the end of the specified
period. Adding an S suffix to any of these marks it instead at the beginning

In [32]:  #For a frequency of 2 hours 30 minutes,


#we can combine the hour (H) and minute (T) codes as follows:

In [33]:  pd.timedelta_range(0, periods=9, freq="2H30T")

Out[33]: TimedeltaIndex(['0 days 00:00:00', '0 days 02:30:00', '0 days 05:00:00',
'0 days 07:30:00', '0 days 10:00:00', '0 days 12:30:00',
'0 days 15:00:00', '0 days 17:30:00', '0 days 20:00:00'],
dtype='timedelta64[ns]', freq='150T')

In [34]:  #we can create a business day offset directly as follows:

In [35]:  from pandas.tseries.offsets import BDay


pd.date_range('2015-07-01', periods=5, freq=BDay())

Out[35]: DatetimeIndex(['2015-07-01', '2015-07-02', '2015-07-03', '2015-07-06',


'2015-07-07'],
dtype='datetime64[ns]', freq='B')

In [ ]:  ​

localhost:8888/notebooks/Time Series.ipynb# 5/5

You might also like