concept Timestamp in category pandas

appears as: Timestamp
Pandas in Action

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

In this chapter, we'll review Python's built-in solutions for datetimes and how Pandas improves upon them with the Timestamp and Timedelta objects. We'll see how we can use the library to convert strings to dates, add and subtract offsets of time, calculate durations, and more. Let's dive in.

Pandas adopts a similar mindset. It introduces the Timestamp object as a replacement for Python's datetime object. The two can be thought of as siblings; they are often interchangeable in the pandas ecosystem, such as when being passed as an argument into a method. Much like the Series expands upon the functionalities of a Python list, the Timestamp adds additional features to the more primitive datetime object. We'll see some of these bells and whistles as we progress through the chapter.

A Timestamp object is instantiated with the same parameters as a datetime object. Day-related parameters like year, month and day parameters are required. Time-related parameters are optional and default to 0.

In  [17] pd.Timestamp(1991, 4, 12) # is the same as
         pd.Timestamp(year = 1991, month = 4, day = 12)
 
Out [17] Timestamp('1991-04-12 00:00:00')

Pandas considers a Timestamp to be equal to a date / datetime if the two objects store the same information.

In  [18] (pd.Timestamp(year = 1991, month = 4, day = 12)
            == dt.date(year = 1991, month = 4, day = 12))
 
Out [18] True
 
In  [19] (pd.Timestamp(year = 1991, month = 4, day = 12, minute = 2)
            == dt.datetime(year = 1991, month = 4, day = 12, minute = 2))
 
Out [19] True

Any variation in date or time values will make the two objects unequal. In the next example, we pass a minute value of 2 to the Timestamp. Lacking an explicit argument for the minute parameter, the datetime falls back to a minute value of 0. The two objects are deemed unequal.

In  [20] (pd.Timestamp(year = 1991, month = 4, day = 12, minute = 2)
            == dt.datetime(year = 1991, month = 4, day = 12))
 
Out [20] False

The Timestamp constructor accepts a variety of inputs. Here's where we can start to see some of the flexibility of the object. The next example passes a string into the constructor instead of a sequence of integers. The string stores a date in the common YYYY-MM-DD format (4-digit year, 2-digit month, 2-digit day).