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 DataFrame | to_timestamp method

schedule Aug 10, 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!

Pandas DataFrame.to_timestamp(~) method converts the row index or column index to DatetimeIndex.

Parameters

1. freqlink | string | optional

Defaults to the frequency used in PeriodIndex.

2. howlink | string | optional

Whether to use the starting or ending time of PeriodIndex:

Value

Description

"s" or "start"

Use the starting time of the period.

"e" or "end"

Use the ending time of the period.

By default, how="start".

3. axis | string | optional

Whether to convert the index or column labels:

Axis

Description

0 or "index"

Converts index to DatetimeIndex.

1 or "columns"

Converts column labels to DatetimeIndex.

By default, axis=0.

4. copy | boolean | optional

  • If True, then a new DataFrame is returned. Modifying this DataFrame will not mutate the source DataFrame, and vice versa.

  • If False, then no new DataFrame is created - modifying the returned DataFrame will mutate the source DataFrame, and vice versa.

By default, copy=True.

Return Value

A DataFrame with the index or column label converted to DatetimeIndex.

Examples

Basic usage

Consider the following DataFrame:

index_period = pd.PeriodIndex(["2020-12-25"], freq="D")
df = pd.DataFrame({"A":[2]}, index=index_period)
df
A
2020-12-25 2

Here, the index of df is of type PeriodIndex:

df.index
PeriodIndex(['2020-12-25'], dtype='period[D]', freq='D')

To convert the index from PeriodIndex to DatetimeIndex:

df_converted = df.to_timestamp()
df_converted
A
2020-12-25 2

Now, checking the type of the converted index:

df_converted.index
DatetimeIndex(['2020-12-25'], dtype='datetime64[ns]', freq=None)

We see that index of df_converted is now DatetimeIndex.

Specifying freq

Consider the following DataFrame with a PeriodIndex:

index_period = pd.PeriodIndex(["2020-12-25","2020-12-26","2020-12-27"], freq="D")
df = pd.DataFrame({"A":[2,3,4]}, index=index_period)
df
A
2020-12-25 2
2020-12-26 3
2020-12-27 4

Suppose we wanted to strip away information about the day unit. We can do so using the freq parameter like so:

df_converted = df.to_timestamp(freq="M")
df_converted
A
2020-12-31 2
2020-12-31 3
2020-12-31 4

Now, all the day units are set to the end of the month (31).

Just as reference, here's the index of df_converted:

df_converted.index
DatetimeIndex(['2020-12-31', '2020-12-31', '2020-12-31'], dtype='datetime64[ns]', freq=None)

Specifying how

Consider the following DataFrame:

index_period = pd.PeriodIndex(["2020-12-25"], freq="D")
df = pd.DataFrame({"A":[2]}, index=index_period)
df
A
2020-12-25 2

By default, how="s", which means that the start of the period is used:

df.to_timestamp(how="s")
A
2020-12-25 2

To use the end of the period, set how="e":

df.to_timestamp(how="e")
A
2020-12-25 23:59:59.999999999 2
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
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!