<jupyter_start><jupyter_text>Working on Time Series data with Pandas <EMAIL> [@gyleodhis](https://www.twitter.com/gyleodhis) ![@gyleodhis](../img/gyle.jpg) Licence:You can use this code for anything you may wish only leave this page: AS IS; HOW IS, WHERE IS Pandas was developed in the context of financial modeling, so as you might expect, itcontains a fairly extensive set of tools for working with dates, times, and timeindexed data. Date and time data comes in a few flavors as shown below:      Time Stamps: For refering a particular moment in time.      Time Intervals and period: For referring to length of time.      Time Deltas or durations refering to an exact legnth of time  Dates and Times in Python Native Python dates and times: datetime and dateutilPython’s basic objects for working with dates and times reside in the built-in datetime module. Along with the third-party dateutil module<jupyter_code>import numpy as np
import pandas as pd
from datetime import datetime
datetime(year = 1994, month=12, day=2)
# When using dateutil module, we can parse dates from a variet of string formats.
from dateutil import parser
date = parser.parse("2nd December, 1994")
date
date.strftime('%A') # Once we have the date object we can do operations like find which day it was.
# Using Numpy datatime64 to encode dates in 64-bits integers.
date = np.array('2015-07-04', dtype=np.datetime64)
date
# Once we have this we can now perform some vector operations to it:
date + np.arange(10) # prints 10 dates from 2015-07-04.<jupyter_output><empty_output><jupyter_text>Notice that the time zone is automatically set to the local time on the computer exe‐cuting the code. You can force any desired fundamental unit using one of many for‐mat codes; for example, here we’ll force a nanosecond-based time:<jupyter_code>np.datetime64('2015-07-04 12:59:59.50', 'ns')<jupyter_output><empty_output><jupyter_text>Pandas Time Series: Indexing by Time<jupyter_code># Lets construct a series object that has time indexed data:
index = pd.DatetimeIndex(['2014-07-04', '2014-08-04','2015-07-04', '2015-08-04'])
data = pd.Series([0, 1, 2, 3], index=index)
data
# We can now perform any Series operation on it ie slicing:
data['2014-07-04':'2015-07-04']
# We can also pass a year to obtain a slice of all the dates in that year:
data['2014']<jupyter_output><empty_output><jupyter_text>Pandas Time Series Data StructuresWhile the above objects can be invoked directly, it is more common to use the 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:<jupyter_code>dates = pd.to_datetime([datetime(2015, 7, 3), '4th of July, 2015','2015-Jul-6', '07-07-2015', '20150708'])
dates<jupyter_output><empty_output><jupyter_text>Any DatetimeIndex can be converted to a PeriodIndex with the to_period() function with the addition of a frequency code; here we’ll use 'D' to indicate daily frequency:<jupyter_code>dates.to_period('D') # You can also use Y for year, M for month etc
# A TimedeltaIndex is created, for example, when one date is subtracted from another:
dates-dates[1]<jupyter_output><empty_output><jupyter_text>Regular sequences: To make the creation of regular date sequences more convenient, Pandas offers a fewfunctions for this purpose: pd.date_range() for timestamps, pd.period_range() forperiods, and pd.timedelta_range() for time deltas. pd.date_range()pd.date_range() accepts a start date, an end date, and an optional frequency code to create a regular sequence of dates. By default, the frequency is one day:<jupyter_code>pd.date_range('2015-07-03', '2015-07-10')<jupyter_output><empty_output><jupyter_text>Alternatively, the date range can be specified not with a start- and endpoint, but with a startpoint and a number of periods:<jupyter_code>pd.date_range('2015-07-03', periods=10)<jupyter_output><empty_output><jupyter_text>You can modify the spacing by altering the freq argument, which defaults to D. Forexample, here we will construct a range of hourly timestamps:<jupyter_code>pd.date_range('2015-07-03', periods=8, freq='H')<jupyter_output><empty_output><jupyter_text>To create regular sequences of period or time delta values, the very similar pd.period_range() and pd.timedelta_range() functions are useful. Here are some monthly periods:<jupyter_code>pd.period_range('2015-07', periods=10, freq='M')
# We can also create a sequence of durations increasing by an hour:
 pd.timedelta_range(0, periods=10, freq='H')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08801742919389978, 'fraction_numerical': 0.04161220043572985, 'mean_word_length': 5.663280116110305, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '660677', 'license': 'Apache-2.0', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1360, 'n_words': 569, 'path': 'Pandas/Pandas_On_Time_Series.ipynb', 'repo_name': 'gyleodhis/My-Data-Science-Devops', 'size': '4604'}