Pandas | Timestamp constructor
Start your free 7-days trial now!
Timestamp is the Pandas' implementation of Python's Datetime, and their functionality and properties are nearly identical. Timestamp is the underlying data structure used for DatetimeIndex.
Parameters
1. ts_input | datetime-like or string or int or numeric
The value used to create the Timestamp.
2. freqlink | string or DateOffset | optional
The interval of the Timestamp.
3. tzlink | string or pytz.timezone or dateutil.tz.tzfile | optional
The timezone of the Timestamp.
4. unitlink | string | optional
The unit used for the timestamp:
"D", "h", "m", "s", "ms", "us" and "ns"
5. yearlink | int | optional
The year unit.
6. monthlink | int | optional
The month unit.
7. daylink | int | optional
The day unit.
You can initialise Timestamp in two different ways:
specify
ts_inputthat includes information about the year, month and day (e.g."2020-01-15").specify
year,monthandday.
8. hour | int | optional
By default, hour=0.
9. minute | int | optional
By default, minute=0.
10. second | int | optional
By default, second=0.
11. microsecond | int | optional
By default, microsecond=0.
Return Value
A Timestamp object.
Examples
Basic usage
To create a Timestamp object from a date string:
pd.Timestamp("2020-01-25")
Timestamp('2020-01-25 00:00:00')
To create a Timestamp with time units:
pd.Timestamp("2020-01-25 22:15:25")
Timestamp('2020-01-25 22:15:25')
Specifying freq
To create a Timestamp with a frequency of a day:
pd.Timestamp("2020-01-25", freq="D")
Timestamp('2020-01-25 00:00:00', freq='D')
The parameter freq is rarely used for individual timestamps. They are commonly used in methods such as date_range(~) that creates a sequence of timestamps.
Specifying tz
To create a Timestamp that is timezone-aware:
pd.Timestamp("2020-01-25", tz="Asia/Tokyo")
Timestamp('2020-01-25 00:00:00+0900', tz='Asia/Tokyo')
Specifying unit
To create a Timestamp using UNIX timestamp:
pd.Timestamp(1608854400, unit="s")
Timestamp('2020-12-25 00:00:00')
Here, 1608854400 represents the seconds passed since January 1st, 1970 (UTC).
Specifying year, month and day
To create a Timestamp using individual time parameters:
pd.Timestamp(year=2020, month=1, day=25)
Timestamp('2020-01-25 00:00:00')
Note that you must specify all three of these parameters, otherwise an error will be thrown.