top of page
  • Writer's pictureBharath Reddy

Creating Datetime objects and time Series in Python

Updated: Aug 20, 2020


Time series data is an important form of structured data in many different fields, such as finance, economics, ecology, neuroscience, and physics. Anything that is observed or measured at many points in time forms

a time series.

In part 1 of the Time Series in Python i would cover

  1. Datetime and time delta objects

  2. Converting a string to these objects and vice versa

  3. Generating Date Ranges of various types in Python.

Datetime and time delta objects

datetime stores both the date and time down to the microsecond. timedelta represents the temporal difference between two datetime objects. You can add or subtract a timedelta or a multiple of timedelta object to yield a new shifted object.

Converting between String and Datetime

we can use datetime.strptime to convert a string to dates format

There is an excellent package dateutil which can be used to automatically parse most human-intelligible date representations.

Generating Date Ranges of various types in Python

pd.date_range can be used to generate a DatetimeIndex with length controlled in three ways:

  • Pasing start and end periods as parameters to pd.date_range

  • Passing a number of periods to generate

  • Passing start date and end date with frequency specified. A complete list of frequencies would be too long & is easily available in the function doc screen but some useful ones are :

In addition to predefined frequencies you can define custom frequency by adding hours or minutes or any period to existing frequencies ex

freq = 1h30min or 4h creates a frequency of 4 hour interval.

52 views0 comments

Recent Posts

See All
bottom of page