Indexing using DatetimeIndex

The core of the time-series functionality in pandas revolves around the use of specialized indexes that represent measurements of data at one or more timestamps. These indexes in pandas are referred to as DatetimeIndex objects. These are incredibly powerful objects, and they allow us to automatically align data based on dates and times.

There are several ways to create DatetimeIndex objects in pandas. The following creates a DateTimeindex by passing a list of datetime objects to a Series:

This Series has taken the datetime objects and constructed a DatetimeIndex from the date values. Each value of that index is a Timestamp object.

The following verifies the type of the index and the types of the labels in the index:

It is not required that you pass datetime objects in the list to create a time-series. The Series object is smart enough to recognize that a string represents datetime and does the conversion for you. The following is equivalent to the previous example:

pandas provides a utility function in pd.to_datetime() which takes a sequence of similar- or mixed-type objects which pandas attempts to convert into Timestamp objects and those into a DatetimeIndex. If an object in the sequence cannot be converted, then pandas will create a NaT value which means not-a-time:

Be careful, as the pd.to_datetime() function will throw an exception if it cannot convert a value to a Timestamp:

To force the function to convert to dates instead of throwing an exception, you can use the errors="coerce" parameter. Values that cannot be converted will then assign NaT in the resulting index:

A range of timestamps with a specific frequency can be easily created using the pd.date_range() function. The following creates a Series object from DatetimeIndex of 10 consecutive days:

A DatetimeIndex can be used for various index operations such as data alignment, selection, and slicing. The following demonstrates slicing based by position:

To demonstrate alignment, we will use the following Series created with the index of the subset we just created:

When we add s2 and date_series, alignment will be performed, returning NaN where items do not align. The value at each index label will be the sum of the values found at the same labels:

Items in a Series with a DatetimeIndex can be retrieved using a string representing a date instead of having to specify a datetime object:

DatetimeIndex can also be sliced using a string representing dates:

Another convenient feature of pandas is that DatetimeIndex can be sliced using partial date specifications. As an example, the following code creates a Series object with dates spanning two years, and then selects only those items of the year 2013:

It is also possible to select items in a specific year and month. The following selects the items in August 2014:

This also works with slices. The following returns items in August and September, 2014:

..................Content has been hidden....................

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