concept datetime in category pandas

appears as: datetimes, datetime
Pandas in Action

This is an excerpt from Manning's book Pandas in Action.

A Series is composed of or constructed from several smaller objects, each of which is delegated a specific responsibility. We can think of these as objects as puzzle pieces that join together to make the greater whole. A Series is amalgamation of objects like the numpy library’s ndarray (which stores the values) and the pandas' library's Index object (which stores the index). Pandas ships with more than a dozen index objects to support indices of  various data types like integers, intervals, or datetimes.

The syntax for importing a module is identical for native modules and for external packages like pandas. Write the import keyword followed by the module or package's name. Let's import Python’s datetime module for working with dates and times.

A literal is a shorthand piece of syntax that creates an object. Two examples we've encountered so far are square brackets ([]) for a list and curly braces ({}) for a dictionary. For more complex data structures like a datetime object, we need to follow a more explicit creation process.

The syntax for creating an instance from a class is similar to that of invoking a function. Write the class name followed by a pair of parentheses populated with arguments. The first three arguments for the datetime constructor are mandatory and represent the year, month, and day the object will represent. The example below creates a datetime object for Leonardo Da Vinci's birthday on April 15th, 1452.

In [155] da_vinci_birthday = dt.datetime(1452, 4, 15)

Like all objects, datetime objects have their own set of attributes and methods.

In [156] da_vinci_birthday.year
Out [156] 1452

One example of a method on a datetime object is weekday, which returns the day of the week represented as an integer. 0 represents Sunday and 6 represents Saturday. It looks like Da Vinci was born on a Wednesday!