search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas | Timestamp constructor

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
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.

NOTE

You can initialise Timestamp in two different ways:

  • specify ts_input that includes information about the year, month and day (e.g. "2020-01-15").

  • specify year, month and day.

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.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!